2024-02-11 14:17:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Allow execution from anywhere
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
2024-03-29 23:24:40 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-02-11 14:17:08 +00:00
|
|
|
from devscripts.tomlparse import parse_toml
|
|
|
|
from devscripts.utils import read_file
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description='Install dependencies for yt-dlp')
|
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'input', nargs='?', metavar='TOMLFILE', default=Path(__file__).parent.parent / 'pyproject.toml',
|
|
|
|
help='input file (default: %(default)s)')
|
2024-02-11 14:17:08 +00:00
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'-e', '--exclude', metavar='DEPENDENCY', action='append',
|
|
|
|
help='exclude a dependency')
|
2024-02-11 14:17:08 +00:00
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'-i', '--include', metavar='GROUP', action='append',
|
|
|
|
help='include an optional dependency group')
|
2024-02-11 14:17:08 +00:00
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'-o', '--only-optional', action='store_true',
|
|
|
|
help='only install optional dependencies')
|
2024-02-11 14:17:08 +00:00
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'-p', '--print', action='store_true',
|
|
|
|
help='only print requirements to stdout')
|
2024-02-11 14:17:08 +00:00
|
|
|
parser.add_argument(
|
2024-03-29 23:24:40 +00:00
|
|
|
'-u', '--user', action='store_true',
|
|
|
|
help='install with pip as --user')
|
2024-02-11 14:17:08 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
2024-03-04 23:19:37 +00:00
|
|
|
project_table = parse_toml(read_file(args.input))['project']
|
|
|
|
optional_groups = project_table['optional-dependencies']
|
|
|
|
excludes = args.exclude or []
|
|
|
|
|
2024-03-29 23:24:40 +00:00
|
|
|
targets = []
|
2024-03-04 23:19:37 +00:00
|
|
|
if not args.only_optional: # `-o` should exclude 'dependencies' and the 'default' group
|
2024-03-29 23:24:40 +00:00
|
|
|
targets.extend(project_table['dependencies'])
|
2024-03-04 23:19:37 +00:00
|
|
|
if 'default' not in excludes: # `--exclude default` should exclude entire 'default' group
|
2024-03-29 23:24:40 +00:00
|
|
|
targets.extend(optional_groups['default'])
|
2024-03-04 23:19:37 +00:00
|
|
|
|
|
|
|
for include in filter(None, map(optional_groups.get, args.include or [])):
|
2024-03-29 23:24:40 +00:00
|
|
|
targets.extend(include)
|
2024-03-04 23:19:37 +00:00
|
|
|
|
2024-03-29 23:24:40 +00:00
|
|
|
targets = [t for t in targets if re.match(r'[\w-]+', t).group(0).lower() not in excludes]
|
2024-02-11 14:17:08 +00:00
|
|
|
|
|
|
|
if args.print:
|
|
|
|
for target in targets:
|
|
|
|
print(target)
|
|
|
|
return
|
|
|
|
|
|
|
|
pip_args = [sys.executable, '-m', 'pip', 'install', '-U']
|
|
|
|
if args.user:
|
|
|
|
pip_args.append('--user')
|
|
|
|
pip_args.extend(targets)
|
|
|
|
|
|
|
|
return subprocess.call(pip_args)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|