forked from softpano/pythonizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbump.py
67 lines (55 loc) · 2.69 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
# Bump the pythonizer version
from datetime import date
import shutil
import yaml
import re
with open('bump.yaml', 'r') as conf:
config = yaml.safe_load(conf)
today = date.today()
for file in config.keys():
shutil.copy2(file, file+'.bak')
print(f'Backing {file} up to {file}.bak')
patterns = config[file]
with open(file+'.bak', 'r') as inf:
with open(file, 'w') as out:
for line in inf:
action = ''
new_line = ''
for pattern, act in patterns.items():
if (m:=re.search(pattern, line)):
action = act
for i, grp in enumerate(m.groups(), start=1):
if (m2:=re.match(r'(\d+)\.(\d\d\d)', grp)): # Version
vers = int(m2.group(2))
bump = f'{vers+1:03d}'
print(f'In {file}, bumped version to {m2.group(1)}.{bump}')
new_line = new_line + m2.group(1) + '.' + bump
elif re.match(r'\d\d\d\d\/\d\d\/\d\d', grp): # YYYY/MM/DD
dt = today.strftime("%Y/%m/%d")
new_line = new_line + dt
elif re.match(r'\d\d\d\d-\d\d-\d\d', grp): # YYYY-MM-DD
dt = today.strftime("%Y-%m-%d")
new_line = new_line + dt
elif re.match(r'[A-Z][a-z][a-z] \d*, \d\d\d\d', grp): # Mon DD, YYYY
dt = today.strftime("%b %d, %Y").replace(' 0', ' ')
new_line = new_line + dt
elif i == 1:
new_line = new_line + line[0:m.end(i)]
else:
new_line = new_line + line[m.end(i-1):m.end(i)]
if new_line[-1] != "\n":
new_line += "\n"
if action == 'replace':
print(new_line, end='', file=out)
elif action == 'append':
print(line, end='', file=out)
print(new_line, end='', file=out)
elif action == 'history':
print(new_line, end='', file=out)
print('------------------', file=out)
print(file=out)
print('* ', file=out)
print(file=out)
print(line, end='', file=out)
else:
print(line, end='', file=out)