subprojects: add libdrm wrap
[platform/upstream/gstreamer.git] / gst-worktree.py
1 #!/usr/bin/env python3
2
3 import os
4 import glob
5 import argparse
6 import subprocess
7 import configparser
8
9 from scripts.common import git
10 from scripts.common import Colors
11
12
13 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
14 SUBPROJECTS_DIR = os.path.normpath(os.path.join(SCRIPTDIR, "subprojects"))
15
16
17 def repo_has_branch(repo_dir, branch):
18     if not branch:
19         return False
20     try:
21         git("describe", branch, repository_path=repo_dir)
22     except subprocess.CalledProcessError:
23         return False
24     return True
25
26 def parse_wrapfile(wrapf):
27     cgp = configparser.ConfigParser()
28     cgp.read(wrapf)
29     if 'wrap-git' not in cgp:
30         return None
31     section = cgp['wrap-git']
32     return section['directory'], section['revision']
33
34 def get_wrap_subprojects(srcdir, gst_branch):
35     '''
36     Parses wrap files in the subprojects directory for the specified source
37     tree and gets the revisions for all common repos.
38     '''
39     for wrapf in glob.glob(os.path.join(srcdir, 'subprojects', '*.wrap')):
40         entries = parse_wrapfile(wrapf)
41         if not entries:
42             continue
43
44         repo_name, repo_branch = entries
45         parent_repo_dir = os.path.join(SUBPROJECTS_DIR, repo_name)
46         if not os.path.exists(os.path.join(parent_repo_dir, '.git')):
47             continue
48         # If a branch of the same name exists in the gst subproject, use it
49         if repo_name.startswith('gst') and repo_has_branch(parent_repo_dir, gst_branch):
50             repo_branch = gst_branch
51
52         yield repo_name, repo_branch, parent_repo_dir
53
54 def checkout_worktree(repo_name, repo_dir, worktree_dir, branch, new_branch, force=False):
55     print('Checking out worktree for project {!r} into {!r} '
56           '(branch {})'.format(repo_name, worktree_dir, branch))
57     try:
58         args = ["worktree", "add"]
59         if force:
60             args += ["-f", "-f"]
61         args += [worktree_dir, branch]
62         if new_branch:
63             args += ["-b", new_branch]
64         git(*args, repository_path=repo_dir)
65     except subprocess.CalledProcessError as e:
66         out = getattr(e, "output", b"").decode()
67         print("\nCould not checkout worktree %s, please fix and try again."
68               " Error:\n\n%s %s" % (repo_dir, out, e))
69
70         return False
71
72     commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
73     print(u"  -> %s%s%s - %s" % (Colors.HEADER, repo_dir, Colors.ENDC,
74                                     commit_message[4].strip()))
75     return True
76
77 def checkout_subprojects(worktree_dir, branch, new_branch):
78     worktree_subdir = os.path.join(worktree_dir, "subprojects")
79
80     for repo_name, repo_branch, parent_repo_dir in get_wrap_subprojects(worktree_dir, branch):
81         workdir = os.path.normpath(os.path.join(worktree_subdir, repo_name))
82         if not checkout_worktree(repo_name, parent_repo_dir, workdir, repo_branch, new_branch, force=True):
83             return False
84
85     return True
86
87 def remove_worktree(worktree_dir):
88     worktree_subdir = os.path.join(worktree_dir, "subprojects")
89
90     for repo_name, _, parent_repo_dir in get_wrap_subprojects(worktree_dir, None):
91         workdir = os.path.normpath(os.path.join(worktree_subdir, repo_name))
92         if not os.path.exists(workdir):
93             continue
94
95         subprojdir = os.path.normpath(os.path.join(SUBPROJECTS_DIR, repo_name))
96         if not os.path.exists(subprojdir):
97             continue
98
99         print('Removing worktree {!r}'.format(workdir))
100         try:
101             git('worktree', 'remove', '-f', workdir, repository_path=subprojdir)
102         except subprocess.CalledProcessError as e:
103             out = getattr(e, "output", b"").decode()
104             print('Ignoring error while removing worktree {!r}:\n\n{}'.format(workdir, out))
105
106     try:
107         git('worktree', 'remove', '-f', worktree_dir, repository_path=SCRIPTDIR)
108     except subprocess.CalledProcessError:
109         print('Failed to remove worktree {!r}'.format(worktree_dir))
110         return False
111     return True
112
113
114 if __name__ == "__main__":
115     parser = argparse.ArgumentParser(prog="gst-worktree")
116     parser.add_argument("--no-color", default=False, action='store_true',
117                         help="Do not output ANSI colors")
118
119     subparsers = parser.add_subparsers(help='The sub-command to run', dest='command')
120
121     parser_add = subparsers.add_parser('add',
122                                        help='Create a worktree for gst-build and all subprojects')
123     parser_add.add_argument('worktree_dir', type=str,
124                             help='Directory where to create the new worktree')
125     parser_add.add_argument('branch', type=str, default=None,
126                             help='Branch to checkout')
127     parser_add.add_argument('-b', '--new-branch', type=str, default=None,
128                             help='Branch to create')
129
130     parser_rm = subparsers.add_parser('rm',
131                                       help='Remove a gst-build worktree and the subproject worktrees inside it')
132     parser_rm.add_argument('worktree_dir', type=str,
133                            help='Worktree directory to remove')
134
135     options = parser.parse_args()
136
137     if options.no_color or not Colors.can_enable():
138         Colors.disable()
139
140     if not options.command:
141         parser.print_usage()
142         exit(1)
143
144     worktree_dir = os.path.abspath(options.worktree_dir)
145
146     if options.command == 'add':
147         if not checkout_worktree('gst-build', SCRIPTDIR, worktree_dir, options.branch, options.new_branch):
148             exit(1)
149         if not checkout_subprojects(worktree_dir, options.branch, options.new_branch):
150             exit(1)
151     elif options.command == 'rm':
152         if not os.path.exists(worktree_dir):
153             print('Cannot remove worktree directory {!r}, it does not exist'.format(worktree_dir))
154             exit(1)
155         if not remove_worktree(worktree_dir):
156             exit(1)
157     else:
158         # Unreachable code
159         raise AssertionError