Explain that on Fedora ninja is called ninja-build
[platform/upstream/gstreamer.git] / git-update
1 #!/usr/bin/env python3
2 import argparse
3 import os
4 import subprocess
5 import xml.etree.ElementTree as ET
6
7 from common import git
8 from common import Colors
9
10
11 SCRIPTDIR = os.path.dirname(__file__)
12
13
14 def manifest_get_commits(manifest):
15     res = {}
16     tree = ET.parse(manifest)
17     root = tree.getroot()
18     for child in root:
19         if child.tag == 'project':
20             res[child.attrib["name"]] = child.attrib["revision"]
21     return res
22
23
24 def update_subprojects(manifest, no_interaction=False):
25     if manifest:
26         repos_commits = manifest_get_commits(manifest)
27     else:
28         repos_commits = {}
29
30     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
31     for repo_name in os.listdir(subprojects_dir):
32         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
33         if not os.path.exists(os.path.join(repo_dir, '.git')):
34             continue
35         revision = repos_commits.get(repo_name)
36         if not update_repo(repo_name, repo_dir, revision, no_interaction):
37             return False
38
39     return True
40
41
42 def update_repo(repo_name, repo_dir, revision, no_interaction, recurse_i=0):
43     print("Updating %s..." % repo_name)
44     try:
45         if revision:
46             git("fetch", repository_path=repo_dir)
47             git("checkout", revision, repository_path=repo_dir)
48         else:
49             git("pull", "--rebase", repository_path=repo_dir)
50     except Exception as e:
51         out = getattr(e, "output", b"").decode()
52         if not no_interaction:
53             print("====================================="
54                   "\n%sEntering a shell in %s to fix that"
55                   " just `exit` once done`"
56                   "\n=====================================" % (
57                         out, os.getcwd()))
58             try:
59                 subprocess.check_call(os.environ.get("SHELL", "/bin/sh"),
60                                       cwd=repo_dir)
61             except:
62                 # Result of subshell does not really matter
63                 pass
64
65             if recurse_i < 3:
66                 return update_repo(repo_name, repo_dir, revision, no_interaction,
67                                     recurse_i + 1)
68             return False
69         else:
70             print("\nCould not rebase %s, please fix and try again."
71                     " Error:\n\n%s %s" % (repo_dir, out, e))
72
73             return False
74
75
76     commit_message = git("show", repository_path=repo_dir).split("\n")
77     print(u"  -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
78                                     commit_message[4].strip()))
79
80     return True
81
82
83 if __name__ == "__main__":
84     parser = argparse.ArgumentParser(prog="git-update")
85
86     parser.add_argument("--no-color",
87                         default=False,
88                         action='store_true',
89                         help="Do not output ansi colors.")
90     parser.add_argument("--no-interaction",
91                         default=False,
92                         action='store_true',
93                         help="Do not allow interaction with the user.")
94     parser.add_argument("--manifest",
95                         default=None,
96                         help="Use a android repo manifest to sync repositories"
97                         " Note that it will let all repositories in detached state")
98     options = parser.parse_args()
99     if options.no_color:
100         Colors.disable()
101
102     exit(not update_subprojects(options.manifest,
103                                 options.no_interaction))