aboutsummaryrefslogtreecommitdiff
path: root/devscripts/utils.py
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2023-07-24 23:43:36 +0100
committerdirkf <fieldhouse@gmx.net>2023-07-25 13:19:43 +0100
commitaac33155e40af3da96a2467dd05faea201815989 (patch)
tree6bee082eabdf6ddf5b3322ad8282b02c1256cc6b /devscripts/utils.py
parent2b7dd3b2a2d7c6e228a42d1000a6f3296739ff1c (diff)
downloadyoutube-dl-aac33155e40af3da96a2467dd05faea201815989.tar.xz
[build] Add and use `devscripts/utils`
Diffstat (limited to 'devscripts/utils.py')
-rw-r--r--devscripts/utils.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/devscripts/utils.py b/devscripts/utils.py
new file mode 100644
index 000000000..2d072d2e0
--- /dev/null
+++ b/devscripts/utils.py
@@ -0,0 +1,62 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+import argparse
+import functools
+import os.path
+import subprocess
+import sys
+
+dirn = os.path.dirname
+
+sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
+
+from youtube_dl.compat import (
+ compat_kwargs,
+ compat_open as open,
+)
+
+
+def read_file(fname):
+ with open(fname, encoding='utf-8') as f:
+ return f.read()
+
+
+def write_file(fname, content, mode='w'):
+ with open(fname, mode, encoding='utf-8') as f:
+ return f.write(content)
+
+
+def read_version(fname='youtube_dl/version.py'):
+ """Get the version without importing the package"""
+ 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 {}
+ kwargs['help'] = 'Output file'
+ parser.add_argument('outfile', **compat_kwargs(kwargs))
+
+ 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)
+
+
+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')
+ kwargs = compat_kwargs(kwargs)
+ return subprocess.run(args, **kwargs)