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__))
15 # Force a checkout to happen and throw away local changes
16 FORCE_CHECKOUT = False
19 def manifest_get_commits(manifest):
21 tree = ET.parse(manifest)
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)
31 remote = child.attrib.get('remote')
33 res[path] = [child.attrib["revision"], [os.path.join(remotes[remote], name), child.attrib.get('refname', child.attrib["revision"])]]
35 res[path] = [child.attrib["revision"], []]
40 def get_branch_name(repo_dir):
41 return git('-C', repo_dir, 'rev-parse', '--symbolic-full-name', 'HEAD').strip()
44 def ensure_revision_if_necessary(repo_dir, revision):
46 Makes sure that @revision is set if the current repo is detached.
49 if get_branch_name(repo_dir) == 'HEAD':
50 revision = git('-C', repo_dir, 'rev-parse', 'HEAD').strip()
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')):
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):
68 def repo_status(commit_message):
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)
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")
82 print(u"%s%s%s - %s - %s" % (Colors.HEADER, repo_name, Colors.ENDC,
83 branch_message[0].strip(), repo_status(commit_message)))
86 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0, check_status=False):
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)
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)
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=====================================" % (
113 shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
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)
122 # Result of subshell does not really matter
126 return update_repo(repo_name, repo_dir, revision, no_interaction,
130 print("\nCould not rebase %s, please fix and try again."
131 " Error:\n\n%s %s" % (repo_dir, out, e))
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()))
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):
149 cmd = ['cargo', 'update', '--manifest-path', cargo_toml]
152 ret = subprocess.run(cmd)
153 except FileNotFoundError:
154 # silenty ignore if cargo isn't installed
160 if __name__ == "__main__":
161 parser = argparse.ArgumentParser(prog="git-update")
163 parser.add_argument("--no-color",
166 help="Do not output ansi colors.")
167 parser.add_argument("--builddir",
169 help="Specifies the build directory where to"
170 " invoke ninja after updating.")
171 parser.add_argument("--no-interaction",
174 help="Do not allow interaction with the user.")
175 parser.add_argument("--check-status",
178 help="Check repositories status only.")
179 parser.add_argument("--manifest",
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():
187 if options.no_interaction:
192 targets_s = subprocess.check_output(meson + ['subprojects', 'download'])
193 repos_commits = manifest_get_commits(options.manifest)
194 FORCE_CHECKOUT = True
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):
201 if not update_subprojects(options.manifest, options.no_interaction, check_status=options.check_status):
203 if not options.check_status:
204 update_cargo(options.builddir)
207 ninja = accept_command(["ninja", "ninja-build"])
209 print("Can't find ninja, other backends are not supported for rebuilding")
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)
215 print("Rebuilding all GStreamer modules.")
216 exit(subprocess.call([ninja, '-C', options.builddir]))