]> jfr.im git - yt-dlp.git/blame - devscripts/update-version.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / devscripts / update-version.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
3# Allow direct execution
4import os
5import sys
6
7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9
29cb20bd 10import argparse
115add43 11import contextlib
c305a25c 12import datetime as dt
f8271158 13import sys
915f2a92 14
392389b7 15from devscripts.utils import read_version, run_process, write_file
915f2a92 16
915f2a92 17
29cb20bd
SS
18def get_new_version(version, revision):
19 if not version:
c305a25c 20 version = dt.datetime.now(dt.timezone.utc).strftime('%Y.%m.%d')
915f2a92 21
115add43 22 if revision:
1d03633c 23 assert revision.isdecimal(), 'Revision must be a number'
115add43 24 else:
25 old_version = read_version().split('.')
26 if version.split('.') == old_version[:3]:
add96eb9 27 revision = str(int(([*old_version, 0])[3]) + 1)
75b725a7 28
115add43 29 return f'{version}.{revision}' if revision else version
915f2a92 30
915f2a92 31
115add43 32def get_git_head():
33 with contextlib.suppress(Exception):
392389b7 34 return run_process('git', 'rev-parse', 'HEAD').stdout.strip()
115add43 35
36
29cb20bd 37VERSION_TEMPLATE = '''\
36eaf303 38# Autogenerated by devscripts/update-version.py
915f2a92 39
29cb20bd 40__version__ = {version!r}
5b328c97 41
29cb20bd 42RELEASE_GIT_HEAD = {git_head!r}
70b23409 43
44VARIANT = None
45
46UPDATE_HINT = None
29cb20bd 47
bfc861a9 48CHANNEL = {channel!r}
1d03633c 49
50ORIGIN = {origin!r}
51
52_pkg_version = {package_version!r}
b69fd25c 53'''
36eaf303 54
29cb20bd
SS
55if __name__ == '__main__':
56 parser = argparse.ArgumentParser(description='Update the version.py file')
57 parser.add_argument(
c4efa0ae 58 '-c', '--channel', default='stable',
29cb20bd 59 help='Select update channel (default: %(default)s)')
1d03633c 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"')
29cb20bd
SS
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()
73
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(
1d03633c 79 version=version, git_head=git_head, channel=args.channel, origin=args.origin,
80 package_version=f'{version}{args.suffix}'))
29cb20bd
SS
81
82 print(f'version={version} ({args.channel}), head={git_head}')