5 import xml.etree.ElementTree as ET
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
14 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
17 def manifest_get_commits(manifest):
19 tree = ET.parse(manifest)
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)
29 remote = child.attrib.get('remote')
31 res[path] = [child.attrib["revision"], [os.path.join(remotes[remote], name), child.attrib.get('refname', child.attrib["revision"])]]
33 res[path] = [child.attrib["revision"], []]
38 def get_branch_name(repo_dir):
39 return git('-C', repo_dir, 'rev-parse', '--symbolic-full-name', 'HEAD').strip()
42 def ensure_revision_if_necessary(repo_dir, revision):
44 Makes sure that @revision is set if the current repo is detached.
47 if get_branch_name(repo_dir) == 'HEAD':
48 revision = git('-C', repo_dir, 'rev-parse', 'HEAD').strip()
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')):
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):
66 def repo_status(commit_message):
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)
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")
80 print(u"%s%s%s - %s - %s" % (Colors.HEADER, repo_name, Colors.ENDC,
81 branch_message[0].strip(), repo_status(commit_message)))
84 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0, check_status=False):
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)
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)
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=====================================" % (
109 shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
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)
118 # Result of subshell does not really matter
122 return update_repo(repo_name, repo_dir, revision, no_interaction,
126 print("\nCould not rebase %s, please fix and try again."
127 " Error:\n\n%s %s" % (repo_dir, out, e))
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()))
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):
145 cmd = ['cargo', 'update', '--manifest-path', cargo_toml]
148 ret = subprocess.run(cmd)
149 except FileNotFoundError:
150 # silenty ignore if cargo isn't installed
156 if __name__ == "__main__":
157 parser = argparse.ArgumentParser(prog="git-update")
159 parser.add_argument("--no-color",
162 help="Do not output ansi colors.")
163 parser.add_argument("--builddir",
165 help="Specifies the build directory where to"
166 " invoke ninja after updating.")
167 parser.add_argument("--no-interaction",
170 help="Do not allow interaction with the user.")
171 parser.add_argument("--check-status",
174 help="Check repositories status only.")
175 parser.add_argument("--manifest",
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():
183 if options.no_interaction:
188 targets_s = subprocess.check_output(meson + ['subprojects', 'download'])
189 repos_commits = manifest_get_commits(options.manifest)
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):
196 if not update_subprojects(options.manifest, options.no_interaction, check_status=options.check_status):
198 if not options.check_status:
199 update_cargo(options.builddir)
202 ninja = accept_command(["ninja", "ninja-build"])
204 print("Can't find ninja, other backends are not supported for rebuilding")
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)
210 print("Rebuilding all GStreamer modules.")
211 exit(subprocess.call([ninja, '-C', options.builddir]))