]>
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 | ||
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='REQUIREMENT', action='append', help='Exclude a required 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 | toml_data = parse_toml(read_file(args.input)) | |
37 | deps = toml_data['project']['dependencies'] | |
38 | targets = deps.copy() if not args.only_optional else [] | |
39 | ||
40 | for exclude in args.exclude or []: | |
41 | for dep in deps: | |
42 | simplified_dep = re.match(r'[\w-]+', dep)[0] | |
43 | if dep in targets and (exclude.lower() == simplified_dep.lower() or exclude == dep): | |
44 | targets.remove(dep) | |
45 | ||
46 | optional_deps = toml_data['project']['optional-dependencies'] | |
47 | for include in args.include or []: | |
48 | group = optional_deps.get(include) | |
49 | if group: | |
50 | targets.extend(group) | |
51 | ||
52 | if args.print: | |
53 | for target in targets: | |
54 | print(target) | |
55 | return | |
56 | ||
57 | pip_args = [sys.executable, '-m', 'pip', 'install', '-U'] | |
58 | if args.user: | |
59 | pip_args.append('--user') | |
60 | pip_args.extend(targets) | |
61 | ||
62 | return subprocess.call(pip_args) | |
63 | ||
64 | ||
65 | if __name__ == '__main__': | |
66 | sys.exit(main()) |