-
Notifications
You must be signed in to change notification settings - Fork 8
/
bump_version.py
68 lines (52 loc) · 2.17 KB
/
bump_version.py
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
import re
from pathlib import Path
def get_version_from_file(version_file):
"""Read the version string from the specified file."""
with open(version_file, 'r') as file:
content = file.read()
match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", content, re.M)
if match:
return match.group(1)
raise RuntimeError("Unable to find version string.")
def update_pyproject_version(pyproject_file, new_version):
"""Update the version string in pyproject.toml."""
with open(pyproject_file, 'r') as file:
content = file.read()
updated_content = re.sub(r'version\s*=\s*".*"', f'version = "{new_version}"', content)
with open(pyproject_file, 'w') as file:
file.write(updated_content)
def update_version(version_file, new_version):
"""Update the version string in pyproject.toml."""
with open(version_file, 'r') as file:
content = file.read()
updated_content = re.sub(r'__version__\s*=\s*".*"', f'__version__ = "{new_version}"', content)
with open(version_file, 'w') as file:
file.write(updated_content)
def increment_version(version, part='minor'):
"""Increment the version string. use major, minor, patch """
major, minor, patch = map(int, version.split('.'))
if part == 'major':
major += 1
minor = 0
patch = 0
elif part == 'minor':
minor += 1
patch = 0
elif part == 'patch':
patch += 1
else:
raise ValueError("Invalid part specified. Use 'major' or 'minor'.")
return f"{major}.{minor}.{patch}"
if __name__ == '__main__':
version_file = Path('pyptv/__version__.py')
pyproject_file = Path('pyproject.toml')
# Get the current version from __version__.py
current_version = get_version_from_file(version_file)
print(f"Current version is {current_version}")
# Example usage
new_version = increment_version(current_version, 'patch')
print(f"New version is {new_version}")
# Update the version in pyproject.toml
update_pyproject_version(pyproject_file, new_version)
update_version(version_file, new_version)
print(f"Updated pyproject.toml to version {new_version}")