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