3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
15 from devscripts
.utils
import read_version
, run_process
, write_file
18 def get_new_version(version
, revision
):
20 version
= dt
.datetime
.now(dt
.timezone
.utc
).strftime('%Y.%m.%d')
23 assert revision
.isdecimal(), 'Revision must be a number'
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
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}
52 _pkg_version = {package_version!r}
55 if __name__
== '__main__':
56 parser
= argparse
.ArgumentParser(description
='Update the version.py file')
58 '-c', '--channel', default
='stable',
59 help='Select update channel (default: %(default)s)')
61 '-r', '--origin', default
='local',
62 help='Select origin/repository (default: %(default)s)')
64 '-s', '--suffix', default
='',
65 help='Add an alphanumeric suffix to the package version, e.g. "dev"')
67 '-o', '--output', default
='yt_dlp/version.py',
68 help='The output file to write to (default: %(default)s)')
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()
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}')