]> jfr.im git - yt-dlp.git/blobdiff - devscripts/update-version.py
[devscripts] Create `utils` and refactor
[yt-dlp.git] / devscripts / update-version.py
index c5bc83de93c1f83ca5710f1e761765c557b6de30..c55dd371c5196ab1c06085cb664b78912bb37712 100644 (file)
@@ -7,32 +7,35 @@
 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
 
+import contextlib
 import subprocess
 import sys
 from datetime import datetime
 
-with open('yt_dlp/version.py') as f:
-    exec(compile(f.read(), 'yt_dlp/version.py', 'exec'))
-old_version = locals()['__version__']
+from devscripts.utils import read_version, write_file
 
-old_version_list = old_version.split('.')
 
-old_ver = '.'.join(old_version_list[:3])
-old_rev = old_version_list[3] if len(old_version_list) > 3 else ''
+def get_new_version(revision):
+    version = datetime.utcnow().strftime('%Y.%m.%d')
 
-ver = datetime.utcnow().strftime("%Y.%m.%d")
+    if revision:
+        assert revision.isdigit(), 'Revision must be a number'
+    else:
+        old_version = read_version().split('.')
+        if version.split('.') == old_version[:3]:
+            revision = str(int((old_version + [0])[3]) + 1)
 
-rev = (sys.argv[1:] or [''])[0]  # Use first argument, if present as revision number
-if not rev:
-    rev = str(int(old_rev or 0) + 1) if old_ver == ver else ''
+    return f'{version}.{revision}' if revision else version
 
-VERSION = '.'.join((ver, rev)) if rev else ver
 
-try:
-    sp = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE)
-    GIT_HEAD = sp.communicate()[0].decode().strip() or None
-except Exception:
-    GIT_HEAD = None
+def get_git_head():
+    with contextlib.suppress(Exception):
+        sp = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE)
+        return sp.communicate()[0].decode().strip() or None
+
+
+VERSION = get_new_version((sys.argv + [''])[1])
+GIT_HEAD = get_git_head()
 
 VERSION_FILE = f'''\
 # Autogenerated by devscripts/update-version.py
@@ -42,8 +45,6 @@
 RELEASE_GIT_HEAD = {GIT_HEAD!r}
 '''
 
-with open('yt_dlp/version.py', 'wt') as f:
-    f.write(VERSION_FILE)
-
-print('::set-output name=ytdlp_version::' + VERSION)
+write_file('yt_dlp/version.py', VERSION_FILE)
+print(f'::set-output name=ytdlp_version::{VERSION}')
 print(f'\nVersion = {VERSION}, Git HEAD = {GIT_HEAD}')