Release 2023.12.30
[yt-dlp.git] / devscripts / utils.py
bloba952c9fae2aed10cc59d9483615b6c39652d3799
1 import argparse
2 import functools
3 import subprocess
6 def read_file(fname):
7 with open(fname, encoding='utf-8') as f:
8 return f.read()
11 def write_file(fname, content, mode='w'):
12 with open(fname, mode, encoding='utf-8') as f:
13 return f.write(content)
16 def read_version(fname='yt_dlp/version.py', varname='__version__'):
17 """Get the version without importing the package"""
18 items = {}
19 exec(compile(read_file(fname), fname, 'exec'), items)
20 return items[varname]
23 def get_filename_args(has_infile=False, default_outfile=None):
24 parser = argparse.ArgumentParser()
25 if has_infile:
26 parser.add_argument('infile', help='Input file')
27 kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {}
28 parser.add_argument('outfile', **kwargs, help='Output file')
30 opts = parser.parse_args()
31 if has_infile:
32 return opts.infile, opts.outfile
33 return opts.outfile
36 def compose_functions(*functions):
37 return lambda x: functools.reduce(lambda y, f: f(y), functions, x)
40 def run_process(*args, **kwargs):
41 kwargs.setdefault('text', True)
42 kwargs.setdefault('check', True)
43 kwargs.setdefault('capture_output', True)
44 if kwargs['text']:
45 kwargs.setdefault('encoding', 'utf-8')
46 kwargs.setdefault('errors', 'replace')
47 return subprocess.run(args, **kwargs)