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