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