2022-08-08 21:38:47 +02:00
|
|
|
import argparse
|
|
|
|
import functools
|
2023-03-04 18:10:08 +01:00
|
|
|
import subprocess
|
2022-08-08 21:38:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def read_file(fname):
|
|
|
|
with open(fname, encoding='utf-8') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
2022-10-21 15:26:00 +02:00
|
|
|
def write_file(fname, content, mode='w'):
|
|
|
|
with open(fname, mode, encoding='utf-8') as f:
|
2022-08-08 21:38:47 +02:00
|
|
|
return f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def read_version(fname='yt_dlp/version.py'):
|
2023-03-04 18:10:08 +01:00
|
|
|
"""Get the version without importing the package"""
|
2022-08-08 21:38:47 +02:00
|
|
|
exec(compile(read_file(fname), fname, 'exec'))
|
|
|
|
return locals()['__version__']
|
|
|
|
|
|
|
|
|
|
|
|
def get_filename_args(has_infile=False, default_outfile=None):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
if has_infile:
|
|
|
|
parser.add_argument('infile', help='Input file')
|
|
|
|
kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {}
|
|
|
|
parser.add_argument('outfile', **kwargs, help='Output file')
|
|
|
|
|
|
|
|
opts = parser.parse_args()
|
|
|
|
if has_infile:
|
|
|
|
return opts.infile, opts.outfile
|
|
|
|
return opts.outfile
|
|
|
|
|
|
|
|
|
|
|
|
def compose_functions(*functions):
|
|
|
|
return lambda x: functools.reduce(lambda y, f: f(y), functions, x)
|
2023-03-04 18:10:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run_process(*args, **kwargs):
|
|
|
|
kwargs.setdefault('text', True)
|
|
|
|
kwargs.setdefault('check', True)
|
|
|
|
kwargs.setdefault('capture_output', True)
|
|
|
|
if kwargs['text']:
|
|
|
|
kwargs.setdefault('encoding', 'utf-8')
|
|
|
|
kwargs.setdefault('errors', 'replace')
|
|
|
|
return subprocess.run(args, **kwargs)
|