aboutsummaryrefslogtreecommitdiffstats
path: root/devscripts
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2022-08-09 01:08:47 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2022-08-09 01:08:47 +0530
commit115add43876964956917bf596c1d0b148c5b3c26 (patch)
tree3274c61d8d9440b414394c34b1ac76a8cb300116 /devscripts
parentc4b6c5c7c9eb0aa448d03c1540580cdd92737aa8 (diff)
downloadhypervideo-pre-115add43876964956917bf596c1d0b148c5b3c26.tar.lz
hypervideo-pre-115add43876964956917bf596c1d0b148c5b3c26.tar.xz
hypervideo-pre-115add43876964956917bf596c1d0b148c5b3c26.zip
[devscripts] Create `utils` and refactor
Diffstat (limited to 'devscripts')
-rw-r--r--devscripts/make_issue_template.py40
-rw-r--r--devscripts/make_lazy_extractors.py16
-rwxr-xr-xdevscripts/make_readme.py23
-rw-r--r--devscripts/make_supportedsites.py12
-rw-r--r--devscripts/prepare_manpage.py41
-rw-r--r--devscripts/update-formulae.py14
-rw-r--r--devscripts/update-version.py41
-rw-r--r--devscripts/utils.py35
8 files changed, 125 insertions, 97 deletions
diff --git a/devscripts/make_issue_template.py b/devscripts/make_issue_template.py
index 90e7e0b43..fd964c6c6 100644
--- a/devscripts/make_issue_template.py
+++ b/devscripts/make_issue_template.py
@@ -7,20 +7,14 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-import optparse
import re
-
-def read(fname):
- with open(fname, encoding='utf-8') as f:
- return f.read()
-
-
-# Get the version without importing the package
-def read_version(fname):
- exec(compile(read(fname), fname, 'exec'))
- return locals()['__version__']
-
+from devscripts.utils import (
+ get_filename_args,
+ read_file,
+ read_version,
+ write_file,
+)
VERBOSE_TMPL = '''
- type: checkboxes
@@ -58,20 +52,24 @@ VERBOSE_TMPL = '''
required: true
'''.strip()
+NO_SKIP = '''
+ - type: checkboxes
+ attributes:
+ label: DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE
+ description: Fill all fields even if you think it is irrelevant for the issue
+ options:
+ - label: I understand that I will be **blocked** if I remove or skip any mandatory\\* field
+ required: true
+'''.strip()
-def main():
- parser = optparse.OptionParser(usage='%prog INFILE OUTFILE')
- _, args = parser.parse_args()
- if len(args) != 2:
- parser.error('Expected an input and an output filename')
- fields = {'version': read_version('yt_dlp/version.py')}
+def main():
+ fields = {'version': read_version(), 'no_skip': NO_SKIP}
fields['verbose'] = VERBOSE_TMPL % fields
fields['verbose_optional'] = re.sub(r'(\n\s+validations:)?\n\s+required: true', '', fields['verbose'])
- infile, outfile = args
- with open(outfile, 'w', encoding='utf-8') as outf:
- outf.write(read(infile) % fields)
+ infile, outfile = get_filename_args(has_infile=True)
+ write_file(outfile, read_file(infile) % fields)
if __name__ == '__main__':
diff --git a/devscripts/make_lazy_extractors.py b/devscripts/make_lazy_extractors.py
index c9fdfb562..01bd88ae6 100644
--- a/devscripts/make_lazy_extractors.py
+++ b/devscripts/make_lazy_extractors.py
@@ -7,9 +7,10 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-import optparse
from inspect import getsource
+from devscripts.utils import get_filename_args, read_file, write_file
+
NO_ATTR = object()
STATIC_CLASS_PROPERTIES = ['IE_NAME', 'IE_DESC', 'SEARCH_KEY', '_VALID_URL', '_WORKING', '_NETRC_MACHINE', 'age_limit']
CLASS_METHODS = [
@@ -19,17 +20,11 @@ IE_TEMPLATE = '''
class {name}({bases}):
_module = {module!r}
'''
-with open('devscripts/lazy_load_template.py', encoding='utf-8') as f:
- MODULE_TEMPLATE = f.read()
+MODULE_TEMPLATE = read_file('devscripts/lazy_load_template.py')
def main():
- parser = optparse.OptionParser(usage='%prog [OUTFILE.py]')
- args = parser.parse_args()[1] or ['yt_dlp/extractor/lazy_extractors.py']
- if len(args) != 1:
- parser.error('Expected only an output filename')
-
- lazy_extractors_filename = args[0]
+ lazy_extractors_filename = get_filename_args(default_outfile='yt_dlp/extractor/lazy_extractors.py')
if os.path.exists(lazy_extractors_filename):
os.remove(lazy_extractors_filename)
@@ -46,8 +41,7 @@ def main():
*build_ies(_ALL_CLASSES, (InfoExtractor, SearchInfoExtractor), DummyInfoExtractor),
))
- with open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
- f.write(f'{module_src}\n')
+ write_file(lazy_extractors_filename, f'{module_src}\n')
def get_all_ies():
diff --git a/devscripts/make_readme.py b/devscripts/make_readme.py
index f2e08d7c6..767ea5409 100755
--- a/devscripts/make_readme.py
+++ b/devscripts/make_readme.py
@@ -5,10 +5,17 @@ yt-dlp --help | make_readme.py
This must be run in a console of correct width
"""
+# Allow direct execution
+import os
+import sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
import functools
import re
-import sys
+
+from devscripts.utils import read_file, write_file
README_FILE = 'README.md'
@@ -63,12 +70,10 @@ PATCHES = (
),
)
-with open(README_FILE, encoding='utf-8') as f:
- readme = f.read()
+readme = read_file(README_FILE)
-with open(README_FILE, 'w', encoding='utf-8') as f:
- f.write(''.join((
- take_section(readme, end=f'## {OPTIONS_START}'),
- functools.reduce(apply_patch, PATCHES, options),
- take_section(readme, f'# {OPTIONS_END}'),
- )))
+write_file(README_FILE, ''.join((
+ take_section(readme, end=f'## {OPTIONS_START}'),
+ functools.reduce(apply_patch, PATCHES, options),
+ take_section(readme, f'# {OPTIONS_END}'),
+)))
diff --git a/devscripts/make_supportedsites.py b/devscripts/make_supportedsites.py
index e46f7af56..01548ef97 100644
--- a/devscripts/make_supportedsites.py
+++ b/devscripts/make_supportedsites.py
@@ -7,21 +7,13 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-import optparse
-
+from devscripts.utils import get_filename_args, write_file
from yt_dlp.extractor import list_extractor_classes
def main():
- parser = optparse.OptionParser(usage='%prog OUTFILE.md')
- _, args = parser.parse_args()
- if len(args) != 1:
- parser.error('Expected an output filename')
-
out = '\n'.join(ie.description() for ie in list_extractor_classes() if ie.IE_DESC is not False)
-
- with open(args[0], 'w', encoding='utf-8') as outf:
- outf.write(f'# Supported sites\n{out}\n')
+ write_file(get_filename_args(), f'# Supported sites\n{out}\n')
if __name__ == '__main__':
diff --git a/devscripts/prepare_manpage.py b/devscripts/prepare_manpage.py
index cea934949..9b12e71e5 100644
--- a/devscripts/prepare_manpage.py
+++ b/devscripts/prepare_manpage.py
@@ -1,9 +1,22 @@
#!/usr/bin/env python3
-import optparse
+# Allow direct execution
+import os
+import sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+
import os.path
import re
+from devscripts.utils import (
+ compose_functions,
+ get_filename_args,
+ read_file,
+ write_file,
+)
+
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
README_FILE = os.path.join(ROOT_DIR, 'README.md')
@@ -22,25 +35,6 @@ yt\-dlp \- A youtube-dl fork with additional features and patches
'''
-def main():
- parser = optparse.OptionParser(usage='%prog OUTFILE.md')
- _, args = parser.parse_args()
- if len(args) != 1:
- parser.error('Expected an output filename')
-
- outfile, = args
-
- with open(README_FILE, encoding='utf-8') as f:
- readme = f.read()
-
- readme = filter_excluded_sections(readme)
- readme = move_sections(readme)
- readme = filter_options(readme)
-
- with open(outfile, 'w', encoding='utf-8') as outf:
- outf.write(PREFIX + readme)
-
-
def filter_excluded_sections(readme):
EXCLUDED_SECTION_BEGIN_STRING = re.escape('<!-- MANPAGE: BEGIN EXCLUDED SECTION -->')
EXCLUDED_SECTION_END_STRING = re.escape('<!-- MANPAGE: END EXCLUDED SECTION -->')
@@ -92,5 +86,12 @@ def filter_options(readme):
return readme.replace(section, options, 1)
+TRANSFORM = compose_functions(filter_excluded_sections, move_sections, filter_options)
+
+
+def main():
+ write_file(get_filename_args(), PREFIX + TRANSFORM(read_file(README_FILE)))
+
+
if __name__ == '__main__':
main()
diff --git a/devscripts/update-formulae.py b/devscripts/update-formulae.py
index 96b56b932..e79297f53 100644
--- a/devscripts/update-formulae.py
+++ b/devscripts/update-formulae.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python3
+"""
+Usage: python3 ./devscripts/update-formulae.py <path-to-formulae-rb> <version>
+version can be either 0-aligned (yt-dlp version) or normalized (PyPi version)
+"""
+
# Allow direct execution
import os
import sys
@@ -11,8 +16,7 @@ import json
import re
import urllib.request
-# usage: python3 ./devscripts/update-formulae.py <path-to-formulae-rb> <version>
-# version can be either 0-aligned (yt-dlp version) or normalized (PyPl version)
+from devscripts.utils import read_file, write_file
filename, version = sys.argv[1:]
@@ -27,11 +31,9 @@ tarball_file = next(x for x in pypi_release['urls'] if x['filename'].endswith('.
sha256sum = tarball_file['digests']['sha256']
url = tarball_file['url']
-with open(filename) as r:
- formulae_text = r.read()
+formulae_text = read_file(filename)
formulae_text = re.sub(r'sha256 "[0-9a-f]*?"', 'sha256 "%s"' % sha256sum, formulae_text, count=1)
formulae_text = re.sub(r'url "[^"]*?"', 'url "%s"' % url, formulae_text, count=1)
-with open(filename, 'w') as w:
- w.write(formulae_text)
+write_file(filename, formulae_text)
diff --git a/devscripts/update-version.py b/devscripts/update-version.py
index c5bc83de9..c55dd371c 100644
--- a/devscripts/update-version.py
+++ b/devscripts/update-version.py
@@ -7,32 +7,35 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+import contextlib
import subprocess
import sys
from datetime import datetime
-with open('yt_dlp/version.py') as f:
- exec(compile(f.read(), 'yt_dlp/version.py', 'exec'))
-old_version = locals()['__version__']
+from devscripts.utils import read_version, write_file
-old_version_list = old_version.split('.')
-old_ver = '.'.join(old_version_list[:3])
-old_rev = old_version_list[3] if len(old_version_list) > 3 else ''
+def get_new_version(revision):
+ version = datetime.utcnow().strftime('%Y.%m.%d')
-ver = datetime.utcnow().strftime("%Y.%m.%d")
+ if revision:
+ assert revision.isdigit(), 'Revision must be a number'
+ else:
+ old_version = read_version().split('.')
+ if version.split('.') == old_version[:3]:
+ revision = str(int((old_version + [0])[3]) + 1)
-rev = (sys.argv[1:] or [''])[0] # Use first argument, if present as revision number
-if not rev:
- rev = str(int(old_rev or 0) + 1) if old_ver == ver else ''
+ return f'{version}.{revision}' if revision else version
-VERSION = '.'.join((ver, rev)) if rev else ver
-try:
- sp = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE)
- GIT_HEAD = sp.communicate()[0].decode().strip() or None
-except Exception:
- GIT_HEAD = None
+def get_git_head():
+ with contextlib.suppress(Exception):
+ sp = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE)
+ return sp.communicate()[0].decode().strip() or None
+
+
+VERSION = get_new_version((sys.argv + [''])[1])
+GIT_HEAD = get_git_head()
VERSION_FILE = f'''\
# Autogenerated by devscripts/update-version.py
@@ -42,8 +45,6 @@ __version__ = {VERSION!r}
RELEASE_GIT_HEAD = {GIT_HEAD!r}
'''
-with open('yt_dlp/version.py', 'wt') as f:
- f.write(VERSION_FILE)
-
-print('::set-output name=ytdlp_version::' + VERSION)
+write_file('yt_dlp/version.py', VERSION_FILE)
+print(f'::set-output name=ytdlp_version::{VERSION}')
print(f'\nVersion = {VERSION}, Git HEAD = {GIT_HEAD}')
diff --git a/devscripts/utils.py b/devscripts/utils.py
new file mode 100644
index 000000000..aa17a5f7f
--- /dev/null
+++ b/devscripts/utils.py
@@ -0,0 +1,35 @@
+import argparse
+import functools
+
+
+def read_file(fname):
+ with open(fname, encoding='utf-8') as f:
+ return f.read()
+
+
+def write_file(fname, content):
+ with open(fname, 'w', encoding='utf-8') as f:
+ return f.write(content)
+
+
+# Get the version without importing the package
+def read_version(fname='yt_dlp/version.py'):
+ 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)