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