#!/usr/bin/env python3
-from datetime import datetime
+
+# Allow direct execution
+import os
import sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+
+import argparse
+import contextlib
import subprocess
+import sys
+from datetime import datetime
+from devscripts.utils import read_version, write_file
-with open('yt_dlp/version.py') as f:
- exec(compile(f.read(), 'yt_dlp/version.py', 'exec'))
-old_version = locals()['__version__']
-old_version_list = old_version.split('.')
+def get_new_version(version, revision):
+ if not version:
+ version = datetime.utcnow().strftime('%Y.%m.%d')
-old_ver = '.'.join(old_version_list[:3])
-old_rev = old_version_list[3] if len(old_version_list) > 3 else ''
+ 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)
-ver = datetime.utcnow().strftime("%Y.%m.%d")
+ return f'{version}.{revision}' if revision else version
-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 ''
-VERSION = '.'.join((ver, rev)) if rev else ver
+def get_git_head():
+ with contextlib.suppress(Exception):
+ return subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip() or None
-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
-VERSION_FILE = f'''\
+VERSION_TEMPLATE = '''\
# Autogenerated by devscripts/update-version.py
-__version__ = {VERSION!r}
+__version__ = {version!r}
+
+RELEASE_GIT_HEAD = {git_head!r}
+
+VARIANT = None
-RELEASE_GIT_HEAD = {GIT_HEAD!r}
+UPDATE_HINT = None
+
+CHANNEL = {channel!r}
'''
-with open('yt_dlp/version.py', 'wt') as f:
- f.write(VERSION_FILE)
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description='Update the version.py file')
+ parser.add_argument(
+ '-c', '--channel', choices=['stable', 'nightly'], default='stable',
+ help='Select update channel (default: %(default)s)')
+ parser.add_argument(
+ '-o', '--output', default='yt_dlp/version.py',
+ help='The output file to write to (default: %(default)s)')
+ parser.add_argument(
+ 'version', nargs='?', default=None,
+ help='A version or revision to use instead of generating one')
+ args = parser.parse_args()
+
+ git_head = get_git_head()
+ version = (
+ args.version if args.version and '.' in args.version
+ else get_new_version(None, args.version))
+ write_file(args.output, VERSION_TEMPLATE.format(
+ version=version, git_head=git_head, channel=args.channel))
-print('::set-output name=ytdlp_version::' + VERSION)
-print(f'\nVersion = {VERSION}, Git HEAD = {GIT_HEAD}')
+ print(f'version={version} ({args.channel}), head={git_head}')