fetch_sources.py 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/python2
# encoding=utf-8
# Copyright © 2018 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Attempt to fetch build_support repository, then fetch additional
repositories.
"""

from __future__ import print_function
import argparse
29 30
import git
import importlib
31 32
import os
import sys
33
import time
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

build_support_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "repos", "mesa_ci"))

def try_clone(repo):
    print('Trying to clone build support from {}'.format(repo))
    git.Repo.clone_from(repo, build_support_dir)


if not os.path.exists(build_support_dir):
    repo_dir = os.path.dirname(build_support_dir)
    if not os.path.exists(repo_dir):
        os.makedirs(repo_dir)

    try:
        try_clone("git://otc-mesa-ci.local/git/mesa_ci")
    except git.exc.GitCommandError:
        try:
            try_clone("git://otc-mesa-ci.jf.intel.com/git/mesa_ci")
        except git.exc.GitCommandError:
            try:
54
                try_clone("https://gitlab.freedesktop.org/Mesa_CI/mesa_ci.git")
55 56 57 58 59 60 61 62 63
            except git.exc.GitCommandError:
                print("ERROR: could not clone sources")
                sys.exit(1)

sys.path.insert(0, build_support_dir)
import build_support as bs

def main():
    parser = argparse.ArgumentParser(description="checks out branches and commits")
64
    parser.add_argument('--branch', type=str, default="",
65
                        help="The branch to base the checkout on. (default: %(default)s)")
66 67
    parser.add_argument('--project', type=str, default="",
                        help="Limit commits to repos required by the project. (default: none)")
68 69 70 71 72
    parser.add_argument('commits', metavar='commits', type=str, nargs='*',
                        help='commits to check out, in repo=sha format')
    args = parser.parse_args()

    repos = bs.RepoSet()
73
    repos.clone()
74
    spec = bs.BuildSpecification(repo_set=repos)
75
    
76 77 78
    limit_to_repos = {}
    for c in args.commits:
        limit_to_repos[c.split("=")[0]] = c.split("=")[1]
79 80 81 82 83 84 85 86 87
    project = args.project
    branch = args.branch
    branchspec = None
    if branch:
        branchspec = spec.branch_specification(branch)

    if not project and branchspec:
        # we can infer the project from the --branch parameter
        project = branchspec.project
88 89

    deps = []
90 91
    if project:
        # only fetch sources that are required for the project
92 93
        deps = bs.DependencyGraph(project,
                                  bs.Options(args = [sys.argv[0]]),
94 95 96 97
                                  repo_set=repos).all_sources()
    for repo in deps:
        if repo not in limit_to_repos:
            limit_to_repos[repo] = None
98 99 100 101 102

    if branchspec:
        # use the branch specification to configure any sources that remain indeterminate
        branchspec.set_revisions(limit_to_repos)

103
    fail = True
104
    for i in range(15):
105
        repos.fetch(limit_to_repos)
106
        try:
107
            print("Checking out specified commit (try {}/15)".format(i+1))
108
            repos.checkout(limit_to_repos)
109
            fail = False
110
        except git.GitCommandError:
111 112
            print("Unable to checkout specified commit, retrying in 15s..")
            time.sleep(15)
113
        else:
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            break
    if fail:
        raise Exception("ERROR: Unable to checkout specified commit.")

    # fetch (but do not check out) all revisions that are required by
    # externals in the target projects.  They need to be in the cached
    # repo so that the component can check out the required commit
    # within its source tree.
    s_root = bs.ProjectMap().source_root()
    sys.path.append(s_root)
    external_revisions = {}
    for project in deps:
        # check for get_external_revisions in each build.py
        build_dir = s_root + "/" + project
        if not os.path.exists(build_dir + "/build.py"):
            continue
        try:
            build_module = importlib.import_module(project + ".build")
            build_module.get_external_revisions(external_revisions)
        except:
            continue
    for project, tags in external_revisions.items():
        if type(tags) != type([]):
            repos.fetch({project : tags})
            continue
        for tag in tags:
            repos.fetch({project : tag})
141 142 143

if __name__ == '__main__':
    main()