-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_new_version.py
More file actions
87 lines (71 loc) · 2.52 KB
/
Copy pathget_new_version.py
File metadata and controls
87 lines (71 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Description:
- Print the calculated new version.
Usage:
- python3 update_version.py --current-version <version> (--stage)
Version Scheme:
- <year>.<month>.<release count>(a<prerelease count>)
example:
- case 1:
- given:
- current version: 2025.1.1
- today : YEAR = 2025, MONTH = 1
- then:
- if stage is false:
- new version: 2025.1.2
- if stage is true:
- new version: 2025.1.2a0
- case 2:
- given:
- current version: 2025.1.2a0
- today : YEAR = 2025, MONTH = 1
- then:
- if stage is false:
- new version: 2025.1.2
- if stage is true:
- new version: 2025.1.2a1
- case 3:
- given:
- current version: 2025.1.1
- today : YEAR = 2025, MONTH = 2
- then:
- if stage is false:
- new version: 2025.2.0
- if stage is true:
- new version: 2025.2.0a0
"""
import argparse
import datetime
import typing
import packaging.version
PreType = tuple[typing.Literal["a", "b", "rc"], int] | None
class ArgumentNamespace(argparse.Namespace):
current: str
stage: bool = False
def increment_version_count(version: packaging.version.Version, is_stage: bool) -> str:
if (current_pre := version.pre) and current_pre[0] != "a":
raise ValueError(f"Unsupported pre-release version: {current_pre[0]}")
# Get the current date
today: datetime.date = datetime.date.today()
# Calculate the new version
new_count: int = 0
if version.major == today.year and version.minor == today.month:
if current_pre:
# If the current version is a pre-release, do not increment the count
new_count = version.micro
else:
# Same month, increment the count
new_count = version.micro + 1
else:
# Different month, reset the count
new_count = 1
current_pre = None
new_pre: PreType = ((current_pre[0], current_pre[1] + 1) if current_pre else ("a", 0)) if is_stage else None
new_pre_str = f"{new_pre[0]}{new_pre[1]}" if new_pre else ""
return f"{today.year}.{today.month}.{new_count}{new_pre_str}"
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Update version in files.")
parser.add_argument("--current", type=str, required=True)
parser.add_argument("--stage", default=False, action="store_true")
args = parser.parse_args(namespace=ArgumentNamespace())
print(increment_version_count(packaging.version.parse(args.current), args.stage))