This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbump.py
97 lines (66 loc) · 2.47 KB
/
bump.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
import re
import datetime
import pytz
SEARCH_PATTERNS = [
('.travis.yml', r'(?P<pre>.*TAG=)(?P<date>\d{4}-\d{2}-\d{2})\.?(?P<ver>\d{2})?(?P<post>[.\s]*)$'),
('jupyter-config.yml', r'(?P<pre>.*tag: )(?P<date>\d{4}-\d{2}-\d{2})\.?(?P<ver>\d{2})?(?P<post>[.\s]*)$')]
def bump_file(fname, pattern):
current_version = None
new_version = None
with open(fname, 'r') as f:
lines = f.readlines()
newlines = []
for l in lines:
matcher = re.match(pattern, l, re.I)
if matcher:
if current_version is not None:
raise ValueError(
'version declared twice in file {}'.format(fname))
current_version = (matcher.group('date'), matcher.group('ver'))
timestamp = datetime.datetime(
*map(int, matcher.group('date').split('-')), tzinfo=pytz.utc)
date = datetime.date(
timestamp.year, timestamp.month, timestamp.day)
now = datetime.datetime.utcnow()
today = datetime.date(now.year, now.month, now.day)
if (date == today):
if matcher.group('ver') is None:
ver = 1
else:
ver = int(matcher.group('ver')) + 1
else:
ver = 1
newlines.append(
matcher.group('pre')
+ '{:>04}-{:>02}-{:>02}.{:>02}'
.format(now.year, now.month, now.day, ver)
+ matcher.group('post'))
new_version = (
'{:>04}-{:>02}-{:>02}'.format(now.year, now.month, now.day),
'{:>02}'.format(ver))
else:
newlines.append(l)
return (current_version, new_version, ''.join(newlines))
def main():
cv = None
nv = None
contents = []
for fname, pattern in SEARCH_PATTERNS:
this_cv, this_nv, this_contents = bump_file(fname, pattern)
if cv is None:
cv = this_cv
nv = this_nv
if cv != this_cv:
raise ValueError(
'Version mismatch in {}: {} != {}'
.format(fname, cv, this_cv))
if nv != this_nv:
raise ValueError(
'New version mismatch in {}: {} != {}'
.format(fname, nv, this_nv))
contents.append((fname, this_contents))
for fname, c in contents:
with open(fname, 'w+') as f:
f.write(c)
if __name__ == '__main__':
main()