]>
Commit | Line | Data |
---|---|---|
b8a433aa | 1 | #!/usr/bin/env python3 |
2 | ||
3 | # Allow execution from anywhere | |
4 | import os | |
5 | import sys | |
6 | ||
7 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
8 | ||
9 | import argparse | |
10 | import re | |
11 | import subprocess | |
12 | ||
58dd0f8d | 13 | from pathlib import Path |
14 | ||
b8a433aa | 15 | from devscripts.tomlparse import parse_toml |
16 | from devscripts.utils import read_file | |
17 | ||
18 | ||
19 | def parse_args(): | |
20 | parser = argparse.ArgumentParser(description='Install dependencies for yt-dlp') | |
21 | parser.add_argument( | |
58dd0f8d | 22 | 'input', nargs='?', metavar='TOMLFILE', default=Path(__file__).parent.parent / 'pyproject.toml', |
23 | help='input file (default: %(default)s)') | |
b8a433aa | 24 | parser.add_argument( |
58dd0f8d | 25 | '-e', '--exclude', metavar='DEPENDENCY', action='append', |
26 | help='exclude a dependency') | |
b8a433aa | 27 | parser.add_argument( |
58dd0f8d | 28 | '-i', '--include', metavar='GROUP', action='append', |
29 | help='include an optional dependency group') | |
b8a433aa | 30 | parser.add_argument( |
58dd0f8d | 31 | '-o', '--only-optional', action='store_true', |
32 | help='only install optional dependencies') | |
b8a433aa | 33 | parser.add_argument( |
58dd0f8d | 34 | '-p', '--print', action='store_true', |
35 | help='only print requirements to stdout') | |
b8a433aa | 36 | parser.add_argument( |
58dd0f8d | 37 | '-u', '--user', action='store_true', |
38 | help='install with pip as --user') | |
b8a433aa | 39 | return parser.parse_args() |
40 | ||
41 | ||
42 | def main(): | |
43 | args = parse_args() | |
cf91400a | 44 | project_table = parse_toml(read_file(args.input))['project'] |
e897bd82 | 45 | recursive_pattern = re.compile(rf'{project_table["name"]}\[(?P<group_name>[\w-]+)\]') |
cf91400a | 46 | optional_groups = project_table['optional-dependencies'] |
47 | excludes = args.exclude or [] | |
48 | ||
e897bd82 SS |
49 | def yield_deps(group): |
50 | for dep in group: | |
51 | if mobj := recursive_pattern.fullmatch(dep): | |
52 | yield from optional_groups.get(mobj.group('group_name'), []) | |
53 | else: | |
54 | yield dep | |
55 | ||
58dd0f8d | 56 | targets = [] |
cf91400a | 57 | if not args.only_optional: # `-o` should exclude 'dependencies' and the 'default' group |
58dd0f8d | 58 | targets.extend(project_table['dependencies']) |
cf91400a | 59 | if 'default' not in excludes: # `--exclude default` should exclude entire 'default' group |
e897bd82 | 60 | targets.extend(yield_deps(optional_groups['default'])) |
cf91400a | 61 | |
62 | for include in filter(None, map(optional_groups.get, args.include or [])): | |
e897bd82 | 63 | targets.extend(yield_deps(include)) |
cf91400a | 64 | |
58dd0f8d | 65 | targets = [t for t in targets if re.match(r'[\w-]+', t).group(0).lower() not in excludes] |
b8a433aa | 66 | |
67 | if args.print: | |
68 | for target in targets: | |
69 | print(target) | |
70 | return | |
71 | ||
72 | pip_args = [sys.executable, '-m', 'pip', 'install', '-U'] | |
73 | if args.user: | |
74 | pip_args.append('--user') | |
75 | pip_args.extend(targets) | |
76 | ||
77 | return subprocess.call(pip_args) | |
78 | ||
79 | ||
80 | if __name__ == '__main__': | |
81 | sys.exit(main()) |