-
Notifications
You must be signed in to change notification settings - Fork 29
/
version.py
61 lines (45 loc) · 1.35 KB
/
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
import re
import sys
from os.path import exists
def find(path):
if not exists(path):
raise RuntimeError('path not found: %r' % path)
with open(path) as file:
for line in file:
match = re.search(r'\d+\.\d+\.\d+', line)
if match:
return match.group()
raise RuntimeError('version not found')
def inc(version):
try:
parts = version.split('.')
parts = map(int, parts)
major, minor, fix = parts
except ValueError:
raise RuntimeError('bad version: %r' % version)
return '%d.%d.%d' % (major, minor + 1, fix)
def patch(path, before, after):
if not exists(path):
raise RuntimeError('path not found: %r' % path)
with open(path) as file:
text = file.read()
text = text.replace(before, after)
with open(path, 'w') as file:
file.write(text)
def main(args):
if len(args) != 2:
raise RuntimeError('version.py [get|inc] path')
command, path = args
if command == 'get':
print(find(path))
elif command == 'inc':
before = find(path)
after = inc(before)
patch(path, before, after)
print('%s: %s -> %s' % (path, before, after))
if __name__ == '__main__':
args = sys.argv[1:]
try:
main(args)
except RuntimeError as error:
sys.exit(error)