]>
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'] |
45 | optional_groups = project_table['optional-dependencies'] | |
46 | excludes = args.exclude or [] | |
47 | ||
58dd0f8d | 48 | targets = [] |
cf91400a | 49 | if not args.only_optional: # `-o` should exclude 'dependencies' and the 'default' group |
58dd0f8d | 50 | targets.extend(project_table['dependencies']) |
cf91400a | 51 | if 'default' not in excludes: # `--exclude default` should exclude entire 'default' group |
58dd0f8d | 52 | targets.extend(optional_groups['default']) |
cf91400a | 53 | |
54 | for include in filter(None, map(optional_groups.get, args.include or [])): | |
58dd0f8d | 55 | targets.extend(include) |
cf91400a | 56 | |
58dd0f8d | 57 | targets = [t for t in targets if re.match(r'[\w-]+', t).group(0).lower() not in excludes] |
b8a433aa | 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()) |