-
Notifications
You must be signed in to change notification settings - Fork 0
/
upversion.py
99 lines (64 loc) · 2.22 KB
/
upversion.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
import re
import click
VERSION_RE = r"""(\s*{name}\s*=\s*["'])([0-9.]+)(["'])"""
def compile_re(var):
return re.compile(VERSION_RE.format(name=var))
def parse_version(path, var):
r = compile_re(var)
with open(path) as stream:
version = r.search(stream.read()).group(2)
numbers = list(map(int, version.split('.')))
numbers += [0] * (len(numbers) - 3) # complete N.N.N
return tuple(numbers)
def string_version(version):
return '.'.join(map(str, version))
def write_version(path, var, version):
r = compile_re(var)
version = string_version(version)
with open(path, 'r') as stream:
content = stream.read()
with open(path, 'w') as stream:
stream.write(r.subn(r'\g<1>{}\g<3>'.format(version), content)[0])
@click.group()
def cli():
"""Handle version numbers"""
def options(function):
opts = [
click.option('--path', default='./setup.py',
type=click.Path(dir_okay=False, exists=True)),
click.option('--var', default='version'),
click.option('--dry', is_flag=True),
click.option('--major', is_flag=True),
click.option('--minor', is_flag=True),
click.option('--patch', is_flag=True)
]
for option in opts:
function = option(function)
return function
@cli.command()
@options
def view(path, var, major, minor, patch, dry):
change_version(parse_version(path, var), major, minor, patch)
def change_version(version, major, minor, patch):
new_version = upversion(version, major, minor, patch)
print('From {} to {}'.format(
string_version(version), string_version(new_version)))
return new_version
def upversion(version, major, minor, patch):
if major:
version = version[0] + 1, 0, 0
if minor:
version = version[0], version[1] + 1, 0
if patch:
version = version[0], version[1], version[2] + 1
return version
@cli.command()
@options
def up(path, var, major, minor, patch, dry):
new_version = change_version(parse_version(path, var), major, minor, patch)
if not dry:
print(u'writing {}'.format(path))
write_version(path, var, new_version)
if __name__ == '__main__':
cli()