[cleanup] Misc (#11311)
[yt-dlp3.git] / devscripts / update-version.py
blob2018ba8440c9cf10559d6bf2ceef1c6a8e9fd482
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import sys
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10 import argparse
11 import contextlib
12 import datetime as dt
13 import sys
15 from devscripts.utils import read_version, run_process, write_file
18 def get_new_version(version, revision):
19 if not version:
20 version = dt.datetime.now(dt.timezone.utc).strftime('%Y.%m.%d')
22 if revision:
23 assert revision.isdecimal(), 'Revision must be a number'
24 else:
25 old_version = read_version().split('.')
26 if version.split('.') == old_version[:3]:
27 revision = str(int(([*old_version, 0])[3]) + 1)
29 return f'{version}.{revision}' if revision else version
32 def get_git_head():
33 with contextlib.suppress(Exception):
34 return run_process('git', 'rev-parse', 'HEAD').stdout.strip()
37 VERSION_TEMPLATE = '''\
38 # Autogenerated by devscripts/update-version.py
40 __version__ = {version!r}
42 RELEASE_GIT_HEAD = {git_head!r}
44 VARIANT = None
46 UPDATE_HINT = None
48 CHANNEL = {channel!r}
50 ORIGIN = {origin!r}
52 _pkg_version = {package_version!r}
53 '''
55 if __name__ == '__main__':
56 parser = argparse.ArgumentParser(description='Update the version.py file')
57 parser.add_argument(
58 '-c', '--channel', default='stable',
59 help='Select update channel (default: %(default)s)')
60 parser.add_argument(
61 '-r', '--origin', default='local',
62 help='Select origin/repository (default: %(default)s)')
63 parser.add_argument(
64 '-s', '--suffix', default='',
65 help='Add an alphanumeric suffix to the package version, e.g. "dev"')
66 parser.add_argument(
67 '-o', '--output', default='yt_dlp/version.py',
68 help='The output file to write to (default: %(default)s)')
69 parser.add_argument(
70 'version', nargs='?', default=None,
71 help='A version or revision to use instead of generating one')
72 args = parser.parse_args()
74 git_head = get_git_head()
75 version = (
76 args.version if args.version and '.' in args.version
77 else get_new_version(None, args.version))
78 write_file(args.output, VERSION_TEMPLATE.format(
79 version=version, git_head=git_head, channel=args.channel, origin=args.origin,
80 package_version=f'{version}{args.suffix}'))
82 print(f'version={version} ({args.channel}), head={git_head}')