]> jfr.im git - yt-dlp.git/blame - devscripts/update-version.py
[build] Automated builds and nightly releases (#6220)
[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
36eaf303 12import subprocess
f8271158 13import sys
14from datetime import datetime
915f2a92 15
115add43 16from devscripts.utils import read_version, write_file
915f2a92 17
915f2a92 18
29cb20bd
SS
19def get_new_version(version, revision):
20 if not version:
21 version = datetime.utcnow().strftime('%Y.%m.%d')
915f2a92 22
115add43 23 if revision:
24 assert revision.isdigit(), 'Revision must be a number'
25 else:
26 old_version = read_version().split('.')
27 if version.split('.') == old_version[:3]:
28 revision = str(int((old_version + [0])[3]) + 1)
75b725a7 29
115add43 30 return f'{version}.{revision}' if revision else version
915f2a92 31
915f2a92 32
115add43 33def get_git_head():
34 with contextlib.suppress(Exception):
29cb20bd 35 return subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip() or None
115add43 36
37
29cb20bd 38VERSION_TEMPLATE = '''\
36eaf303 39# Autogenerated by devscripts/update-version.py
915f2a92 40
29cb20bd 41__version__ = {version!r}
5b328c97 42
29cb20bd 43RELEASE_GIT_HEAD = {git_head!r}
70b23409 44
45VARIANT = None
46
47UPDATE_HINT = None
29cb20bd
SS
48
49CHANNEL = "{channel!r}"
b69fd25c 50'''
36eaf303 51
29cb20bd
SS
52if __name__ == '__main__':
53 parser = argparse.ArgumentParser(description='Update the version.py file')
54 parser.add_argument(
55 '-c', '--channel', choices=['stable', 'nightly'], default='stable',
56 help='Select update channel (default: %(default)s)')
57 parser.add_argument(
58 '-o', '--output', default='yt_dlp/version.py',
59 help='The output file to write to (default: %(default)s)')
60 parser.add_argument(
61 'version', nargs='?', default=None,
62 help='A version or revision to use instead of generating one')
63 args = parser.parse_args()
64
65 git_head = get_git_head()
66 version = (
67 args.version if args.version and '.' in args.version
68 else get_new_version(None, args.version))
69 write_file(args.output, VERSION_TEMPLATE.format(
70 version=version, git_head=git_head, channel=args.channel))
71
72 print(f'version={version} ({args.channel}), head={git_head}')