Add a new subproject 'win-flex-bison-binaries'
[platform/upstream/gstreamer.git] / checkout-branch-worktree
1 #!/usr/bin/env python3
2
3 import argparse
4 import json
5 import os
6 import subprocess
7 import xml.etree.ElementTree as ET
8 import sys
9
10 from common import git
11 from common import Colors
12 from common import get_meson
13 from common import accept_command
14
15
16 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
17
18
19 def checkout_subprojects(worktree_dir, branch):
20     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
21     worktree_subdir = os.path.join(worktree_dir, "subprojects")
22
23     meson = get_meson()
24     installed_s = subprocess.check_output([sys.executable, meson, 'introspect',
25                                             options.builddir, '--projectinfo'])
26     for subproj in json.loads(installed_s.decode())["subprojects"]:
27         repo_name = subproj["name"]
28         if not repo_name.startswith("gst"):
29             continue
30
31         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
32         if not os.path.exists(os.path.join(repo_dir, '.git')):
33             continue
34
35         workdir = os.path.normpath(os.path.join(worktree_subdir, repo_name))
36         if not checkout_worktree(repo_name, repo_dir, workdir, branch):
37             return False
38
39     return True
40
41
42 def checkout_worktree(repo_name, repo_dir, worktree_dir, branch):
43     print("Checking out worktree %s in %s (branch %s)" % (repo_name, worktree_dir, branch))
44     try:
45         git("worktree", "add", worktree_dir, branch, repository_path=repo_dir)
46     except Exception as e:
47         out = getattr(e, "output", b"").decode()
48         print("\nCould not checkout worktree %s, please fix and try again."
49               " Error:\n\n%s %s" % (repo_dir, out, e))
50
51         return False
52
53     commit_message = git("show", repository_path=repo_dir).split("\n")
54     print(u"  -> %s%s%s - %s" % (Colors.HEADER, repo_dir, Colors.ENDC,
55                                     commit_message[4].strip()))
56
57     return True
58
59
60 if __name__ == "__main__":
61     parser = argparse.ArgumentParser(prog="git-update")
62
63
64     parser.add_argument('worktree_dir', metavar='worktree_dir', type=str,
65                         help='The directory where to checkout the new worktree')
66     parser.add_argument('branch', metavar='branch', type=str,
67                         help='The branch to checkout')
68     parser.add_argument("--no-color",
69                         default=False,
70                         action='store_true',
71                         help="Do not output ansi colors.")
72     parser.add_argument("--builddir", '-C',
73                         default=os.path.join(SCRIPTDIR, "build"),
74                         help="The meson build directory")
75     options = parser.parse_args()
76
77     if options.no_color:
78         Colors.disable()
79
80     if not os.path.exists(options.builddir):
81         print("GStreamer not built in %s\n\nBuild it and try again" %
82               options.builddir)
83         exit(1)
84
85     options.worktree_dir = os.path.abspath(options.worktree_dir)
86     if not checkout_worktree('gst-build', SCRIPTDIR, options.worktree_dir, options.branch):
87         exit(1)
88     if not checkout_subprojects(options.worktree_dir, options.branch):
89         exit(1)