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