-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_version.py
80 lines (64 loc) · 2.16 KB
/
new_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
69
70
71
72
73
74
75
76
77
78
79
80
# encoding: utf-8
import re
import sys
from datetime import datetime
VERSION_FILE = "CURRENT_VERSION.txt"
CHANGELOG_FILE = "CHANGES.txt"
def is_valid_version(version):
""" Check if a string is a valid version
"""
return bool(re.search("[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}", version))
def validate(version):
""" Check if a version is valid, thro error if not
"""
if is_valid_version(version):
return version
else:
msg = "{} is not a valid version".format(version)
raise ValueError(msg)
def get_current_version():
""" Get the current version from file
"""
with open(VERSION_FILE) as f:
current_version = f.read().strip()
validate(current_version)
return current_version
def _increment_version(version):
""" Increment version 0.0.1 => 0.0.2
"""
validate(version)
parts = version.split(".")
last_digit = int(parts[-1])
last_digit += 1
incremented_version = ".".join(parts[:-1] + [str(last_digit)])
return incremented_version
def main():
"""
current_version = get_current_version()
suggested_version = _increment_version(current_version)
# Ask user for version
new_version = raw_input('New version (currently {}) [{}]: '\
.format(current_version, suggested_version))
new_version = new_version or suggested_version
validate(new_version)
# Ask user for message
msg = raw_input("Describe version changes (will be added to {}): "\
.format(CHANGELOG_FILE))
"""
if len(sys.argv) < 3:
raise ValueError("Must provide version and message as arguments.")
new_version = sys.argv[1]
validate(new_version)
msg = sys.argv[2]
date = datetime.now().strftime("%Y-%m-%d")
# Update changelog
# Format: v<version>, <date> -- Initial release.
change_log_row = "v{}, {} -- {}".format(new_version, date, msg)
with open(CHANGELOG_FILE, 'a') as file:
file.write(change_log_row + "\n")
print(u"{} updated: '{}'".format(CHANGELOG_FILE, change_log_row))
# Update CURRENT_VERSION
with open(VERSION_FILE, 'r+') as file:
file.write(new_version)
if __name__ == '__main__':
main()