cross-files/android: Update paths and args for latest NDK
[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(manifest, no_interaction=False, check_status=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, check_status=check_status):
62           return False
63
64     return True
65
66 def repo_status(commit_message):
67     status = "clean"
68     for message in commit_message:
69       if message.startswith('??'):
70         status = "%sclean but untracked files%s" % (Colors.WARNING,Colors.ENDC)
71       elif message.startswith(' M'):
72         status = "%shas local modificationss%s" % (Colors.WARNING,Colors.ENDC)
73         break;
74     return status
75
76 def check_repo_status(repo_name, worktree_dir):
77     branch_message = git("status", repository_path=worktree_dir).split("\n")
78     commit_message = git("status", "--porcelain", repository_path=worktree_dir).split("\n")
79
80     print(u"%s%s%s - %s - %s" % (Colors.HEADER, repo_name, Colors.ENDC,
81                                     branch_message[0].strip(), repo_status(commit_message)))
82     return True
83
84 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0, check_status=False):
85     if check_status:
86       return check_repo_status(repo_name, repo_dir)
87     revision = ensure_revision_if_necessary(repo_dir, revision)
88     git("config", "rebase.autoStash", "true", repository_path=repo_dir)
89     try:
90         if revision:
91             print("Checking out %s in %s" % (revision, repo_name))
92             git("fetch", *fetch_args, repository_path=repo_dir)
93             git("checkout", "--detach", revision, repository_path=repo_dir)
94         else:
95             print("Updating branch %s in %s" % (get_branch_name(repo_dir), repo_name))
96             git("pull", "--rebase", repository_path=repo_dir)
97         git("submodule", "update", repository_path=repo_dir)
98     except Exception as e:
99         out = getattr(e, "output", b"").decode()
100         if not no_interaction:
101             print("====================================="
102                   "\n%s\nEntering a shell in %s to fix that"
103                   " just `exit 0` once done, or `exit 255`"
104                   " to skip update for that repository"
105                   "\n=====================================" % (
106                         out, repo_dir))
107             try:
108                 if os.name == 'nt':
109                     shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
110                 else:
111                     shell = os.environ.get("SHELL", os.path.realpath("/bin/sh"))
112                 subprocess.check_call(shell, cwd=repo_dir)
113             except subprocess.CalledProcessError as e:
114                 if e.returncode == 255:
115                     print("Skipping '%s' update" % repo_name)
116                     return True
117             except:
118                 # Result of subshell does not really matter
119                 pass
120
121             if recurse_i < 3:
122                 return update_repo(repo_name, repo_dir, revision, no_interaction,
123                                     recurse_i + 1)
124             return False
125         else:
126             print("\nCould not rebase %s, please fix and try again."
127                     " Error:\n\n%s %s" % (repo_dir, out, e))
128
129             return False
130
131
132     commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
133     print(u"  -> %s%s%s - %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
134                                     commit_message[4].strip()))
135
136     return True
137
138
139 # Update gst-plugins-rs dependencies
140 def update_cargo(build_dir):
141     cargo_toml = os.path.join('subprojects', 'gst-plugins-rs', 'Cargo.toml')
142     if not os.path.exists(cargo_toml):
143         return True
144
145     cmd = ['cargo', 'update', '--manifest-path', cargo_toml]
146
147     try:
148         ret = subprocess.run(cmd)
149     except FileNotFoundError:
150         # silenty ignore if cargo isn't installed
151         return False
152
153     return ret == 0
154
155
156 if __name__ == "__main__":
157     parser = argparse.ArgumentParser(prog="git-update")
158
159     parser.add_argument("--no-color",
160                         default=False,
161                         action='store_true',
162                         help="Do not output ansi colors.")
163     parser.add_argument("--builddir",
164                         default=None,
165                         help="Specifies the build directory where to"
166                         " invoke ninja after updating.")
167     parser.add_argument("--no-interaction",
168                         default=False,
169                         action='store_true',
170                         help="Do not allow interaction with the user.")
171     parser.add_argument("--check-status",
172                         default=False,
173                         action='store_true',
174                         help="Check repositories status only.")
175     parser.add_argument("--manifest",
176                         default=None,
177                         help="Use a android repo manifest to sync repositories"
178                         " Note that it will let all repositories in detached state")
179     options = parser.parse_args()
180     if options.no_color or not Colors.can_enable():
181         Colors.disable()
182
183     if options.no_interaction:
184         sys.stdin.close()
185
186     if options.manifest:
187         meson = get_meson()
188         targets_s = subprocess.check_output(meson + ['subprojects', 'download'])
189         repos_commits = manifest_get_commits(options.manifest)
190     else:
191         repos_commits = {}
192
193     revision, args = repos_commits.get('gst-build', [None, []])
194     if not update_repo('gst-build', SCRIPTDIR, None, options.no_interaction, args, check_status=options.check_status):
195         exit(1)
196     if not update_subprojects(options.manifest, options.no_interaction, check_status=options.check_status):
197         exit(1)
198     if not options.check_status:
199       update_cargo(options.builddir)
200
201     if options.builddir:
202         ninja = accept_command(["ninja", "ninja-build"])
203         if not ninja:
204             print("Can't find ninja, other backends are not supported for rebuilding")
205             exit(1)
206
207         if not os.path.exists(os.path.join (options.builddir, 'build.ninja')):
208             print("Can't rebuild in %s as no build.ninja file found." % options.builddir)
209
210         print("Rebuilding all GStreamer modules.")
211         exit(subprocess.call([ninja, '-C', options.builddir]))