Add 'update' and `git-update` targets to update git repos
[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 from common import accept_command
10
11
12 SCRIPTDIR = os.path.dirname(__file__)
13
14
15 def manifest_get_commits(manifest):
16     res = {}
17     tree = ET.parse(manifest)
18     root = tree.getroot()
19     for child in root:
20         if child.tag == 'project':
21             res[child.attrib["name"]] = child.attrib["revision"]
22     return res
23
24
25 def update_subprojects(manifest, no_interaction=False):
26     if manifest:
27         repos_commits = manifest_get_commits(manifest)
28     else:
29         repos_commits = {}
30
31     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
32     for repo_name in os.listdir(subprojects_dir):
33         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
34         if not os.path.exists(os.path.join(repo_dir, '.git')):
35             continue
36         revision = repos_commits.get(repo_name)
37         if not update_repo(repo_name, repo_dir, revision, no_interaction):
38             return False
39
40     return True
41
42
43 def update_repo(repo_name, repo_dir, revision, no_interaction, recurse_i=0):
44     print("Updating %s..." % repo_name)
45     try:
46         if revision:
47             git("fetch", repository_path=repo_dir)
48             git("checkout", revision, repository_path=repo_dir)
49         else:
50             git("pull", "--rebase", "--autostash", repository_path=repo_dir)
51     except Exception as e:
52         out = getattr(e, "output", b"").decode()
53         if not no_interaction:
54             print("====================================="
55                   "\n%s\nEntering a shell in %s to fix that"
56                   " just `exit 0` once done, or `exit 255`"
57                   " to skip update for that repository"
58                   "\n=====================================" % (
59                         out, repo_dir))
60             try:
61                 if os.name is 'nt':
62                     shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
63                 else:
64                     shell = os.environ.get("SHELL", os.path.realpath("/bin/sh"))
65                 subprocess.check_call(shell, cwd=repo_dir)
66             except subprocess.CalledProcessError as e:
67                 if e.returncode == 255:
68                     print("Skipping '%s' update" % repo_name)
69                     return True
70             except:
71                 # Result of subshell does not really matter
72                 pass
73
74             if recurse_i < 3:
75                 return update_repo(repo_name, repo_dir, revision, no_interaction,
76                                     recurse_i + 1)
77             return False
78         else:
79             print("\nCould not rebase %s, please fix and try again."
80                     " Error:\n\n%s %s" % (repo_dir, out, e))
81
82             return False
83
84
85     commit_message = git("show", repository_path=repo_dir).split("\n")
86     print(u"  -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
87                                     commit_message[4].strip()))
88
89     return True
90
91
92 if __name__ == "__main__":
93     parser = argparse.ArgumentParser(prog="git-update")
94
95     parser.add_argument("--no-color",
96                         default=False,
97                         action='store_true',
98                         help="Do not output ansi colors.")
99     parser.add_argument("--builddir",
100                         default=None,
101                         help="Specifies the build directory where to"
102                         " invoke ninja after updating.")
103     parser.add_argument("--no-interaction",
104                         default=False,
105                         action='store_true',
106                         help="Do not allow interaction with the user.")
107     parser.add_argument("--manifest",
108                         default=None,
109                         help="Use a android repo manifest to sync repositories"
110                         " Note that it will let all repositories in detached state")
111     options = parser.parse_args()
112     if options.no_color:
113         Colors.disable()
114
115     if not update_repo('gst-build', SCRIPTDIR, None, options.no_interaction):
116         exit(1)
117     if not update_subprojects(options.manifest, options.no_interaction):
118         exit(1)
119
120     if options.builddir:
121         ninja = accept_command(["ninja", "ninja-build"])
122         if not ninja:
123             print("Can't find ninja, other backends are not supported for rebuilding")
124             exit(1)
125
126         if not os.path.exists(os.path.join (options.builddir, 'build.ninja')):
127             print("Can't rebuild in %s as no build.ninja file found." % options.builddir)
128
129         print("Rebuilding all GStreamer modules.")
130         exit(subprocess.call([ninja, '-C', options.builddir]))