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