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