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