]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | from devscripts.tomlparse import parse_toml | |
14 | from devscripts.utils import read_file | |
15 | ||
16 | ||
17 | def parse_args(): | |
18 | parser = argparse.ArgumentParser(description='Install dependencies for yt-dlp') | |
19 | parser.add_argument( | |
20 | 'input', nargs='?', metavar='TOMLFILE', default='pyproject.toml', help='Input file (default: %(default)s)') | |
21 | parser.add_argument( | |
22 | '-e', '--exclude', metavar='DEPENDENCY', action='append', help='Exclude a dependency') | |
23 | parser.add_argument( | |
24 | '-i', '--include', metavar='GROUP', action='append', help='Include an optional dependency group') | |
25 | parser.add_argument( | |
26 | '-o', '--only-optional', action='store_true', help='Only install optional dependencies') | |
27 | parser.add_argument( | |
28 | '-p', '--print', action='store_true', help='Only print a requirements.txt to stdout') | |
29 | parser.add_argument( | |
30 | '-u', '--user', action='store_true', help='Install with pip as --user') | |
31 | return parser.parse_args() | |
32 | ||
33 | ||
34 | def main(): | |
35 | args = parse_args() | |
36 | project_table = parse_toml(read_file(args.input))['project'] | |
37 | optional_groups = project_table['optional-dependencies'] | |
38 | excludes = args.exclude or [] | |
39 | ||
40 | deps = [] | |
41 | if not args.only_optional: # `-o` should exclude 'dependencies' and the 'default' group | |
42 | deps.extend(project_table['dependencies']) | |
43 | if 'default' not in excludes: # `--exclude default` should exclude entire 'default' group | |
44 | deps.extend(optional_groups['default']) | |
45 | ||
46 | def name(dependency): | |
47 | return re.match(r'[\w-]+', dependency)[0].lower() | |
48 | ||
49 | target_map = {name(dep): dep for dep in deps} | |
50 | ||
51 | for include in filter(None, map(optional_groups.get, args.include or [])): | |
52 | target_map.update(zip(map(name, include), include)) | |
53 | ||
54 | for exclude in map(name, excludes): | |
55 | target_map.pop(exclude, None) | |
56 | ||
57 | targets = list(target_map.values()) | |
58 | ||
59 | if args.print: | |
60 | for target in targets: | |
61 | print(target) | |
62 | return | |
63 | ||
64 | pip_args = [sys.executable, '-m', 'pip', 'install', '-U'] | |
65 | if args.user: | |
66 | pip_args.append('--user') | |
67 | pip_args.extend(targets) | |
68 | ||
69 | return subprocess.call(pip_args) | |
70 | ||
71 | ||
72 | if __name__ == '__main__': | |
73 | sys.exit(main()) |