update-version.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. import argparse
  7. import contextlib
  8. import datetime as dt
  9. import sys
  10. from devscripts.utils import read_version, run_process, write_file
  11. def get_new_version(version, revision):
  12. if not version:
  13. version = dt.datetime.now(dt.timezone.utc).strftime('%Y.%m.%d')
  14. if revision:
  15. assert revision.isdecimal(), 'Revision must be a number'
  16. else:
  17. old_version = read_version().split('.')
  18. if version.split('.') == old_version[:3]:
  19. revision = str(int(([*old_version, 0])[3]) + 1)
  20. return f'{version}.{revision}' if revision else version
  21. def get_git_head():
  22. with contextlib.suppress(Exception):
  23. return run_process('git', 'rev-parse', 'HEAD').stdout.strip()
  24. VERSION_TEMPLATE = '''\
  25. # Autogenerated by devscripts/update-version.py
  26. __version__ = {version!r}
  27. RELEASE_GIT_HEAD = {git_head!r}
  28. VARIANT = None
  29. UPDATE_HINT = None
  30. CHANNEL = {channel!r}
  31. ORIGIN = {origin!r}
  32. _pkg_version = {package_version!r}
  33. '''
  34. if __name__ == '__main__':
  35. parser = argparse.ArgumentParser(description='Update the version.py file')
  36. parser.add_argument(
  37. '-c', '--channel', default='stable',
  38. help='Select update channel (default: %(default)s)')
  39. parser.add_argument(
  40. '-r', '--origin', default='local',
  41. help='Select origin/repository (default: %(default)s)')
  42. parser.add_argument(
  43. '-s', '--suffix', default='',
  44. help='Add an alphanumeric suffix to the package version, e.g. "dev"')
  45. parser.add_argument(
  46. '-o', '--output', default='yt_dlp/version.py',
  47. help='The output file to write to (default: %(default)s)')
  48. parser.add_argument(
  49. 'version', nargs='?', default=None,
  50. help='A version or revision to use instead of generating one')
  51. args = parser.parse_args()
  52. git_head = get_git_head()
  53. version = (
  54. args.version if args.version and '.' in args.version
  55. else get_new_version(None, args.version))
  56. write_file(args.output, VERSION_TEMPLATE.format(
  57. version=version, git_head=git_head, channel=args.channel, origin=args.origin,
  58. package_version=f'{version}{args.suffix}'))
  59. print(f'version={version} ({args.channel}), head={git_head}')