-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump_version.py
More file actions
executable file
·161 lines (128 loc) · 4.46 KB
/
bump_version.py
File metadata and controls
executable file
·161 lines (128 loc) · 4.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""Version bumping script for VersionTracker.
Usage:
python bump_version.py [major|minor|patch]
"""
import argparse
import re
import sys
from datetime import datetime
from pathlib import Path
def parse_version(version_str):
"""Parse version string into components."""
match = re.match(r"(\d+)\.(\d+)\.(\d+)", version_str)
if not match:
return None
return tuple(map(int, match.groups()))
def bump_version(version_tuple, bump_type):
"""Bump version based on type."""
major, minor, patch = version_tuple
if bump_type == "major":
return (major + 1, 0, 0)
elif bump_type == "minor":
return (major, minor + 1, 0)
elif bump_type == "patch":
return (major, minor, patch + 1)
return version_tuple
def get_current_version():
"""Get current version from __init__.py file."""
init_file = Path(__file__).parent / "versiontracker" / "__init__.py"
with open(init_file, encoding="utf-8") as f:
content = f.read()
match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
if match:
return match.group(1)
return None
def update_version_in_file(file_path, pattern, version_str, template=None):
"""Update version in a file."""
with open(file_path, encoding="utf-8") as f:
content = f.read()
if template:
replacement = template.format(version=version_str)
else:
replacement = version_str
new_content = re.sub(pattern, replacement, content)
with open(file_path, "w", encoding="utf-8") as f:
f.write(new_content)
def update_changelog(version_str):
"""Update the CHANGELOG.md file with a new version entry."""
today = datetime.now().strftime("%Y-%m-%d")
changelog_path = Path(__file__).parent / "CHANGELOG.md"
with open(changelog_path, encoding="utf-8") as f:
content = f.readlines()
# Find the position to insert the new version
insert_pos = None
for i, line in enumerate(content):
if line.startswith("## ["):
insert_pos = i
break
if insert_pos is None:
print("Error: Could not find where to insert the new version in CHANGELOG.md")
return False
# Create the new version entry
new_entry = [
f"## [{version_str}] - {today}\n",
"\n",
f"### Added ({version_str})\n",
"\n",
"- \n",
"\n",
]
# Insert the new version entry
content = content[:insert_pos] + new_entry + content[insert_pos:]
with open(changelog_path, "w", encoding="utf-8") as f:
f.writelines(content)
return True
def main():
"""Main function to bump version."""
parser = argparse.ArgumentParser(description="Bump version numbers")
parser.add_argument(
"bump_type",
choices=["major", "minor", "patch"],
help="Type of version bump",
)
parser.add_argument(
"--no-changelog",
action="store_true",
help="Skip updating the changelog",
)
args = parser.parse_args()
current_version = get_current_version()
if not current_version:
print("Error: Could not determine current version.")
return 1
print(f"Current version: {current_version}")
version_tuple = parse_version(current_version)
if not version_tuple:
print("Error: Could not parse current version.")
return 1
new_version_tuple = bump_version(version_tuple, args.bump_type)
new_version = ".".join(map(str, new_version_tuple))
print(f"New version: {new_version}")
# Update version in __init__.py
init_file = Path(__file__).parent / "versiontracker" / "__init__.py"
update_version_in_file(
init_file,
r'__version__\s*=\s*["\']([^"\']+)["\']',
f'__version__ = "{new_version}"',
)
print(f"Updated version in {init_file}")
# Update version in README.md
readme_file = Path(__file__).parent / "README.md"
update_version_in_file(
readme_file,
r"(\* Version: )[^\n]*",
f"\\1{new_version}",
)
print(f"Updated version in {readme_file}")
# Update changelog if requested
if not args.no_changelog:
if update_changelog(new_version):
print("Updated CHANGELOG.md with new version")
else:
print("Failed to update CHANGELOG.md")
print("\nRemember to commit these changes with:")
print(f' git commit -m "Bump version to {new_version}"')
return 0
if __name__ == "__main__":
sys.exit(main())