-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheditversion.py
95 lines (77 loc) · 2.39 KB
/
editversion.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
#!/usr/bin/env python
import re
import os
import sys
def usage():
print("Usage:")
print(" $ ./%s X.Y.Z" % sys.argv[0])
print("")
def main():
if len(sys.argv) < 2:
usage()
return
new_version = sys.argv[1]
files = [
{
"directory": False,
"path": "setup.py",
"pattern": r" version='([0-9]+\.[0-9]+\.[0-9]+)',"
},
{
"directory": False,
"path": "docs/source/conf.py",
"pattern": r"release = '([0-9]+\.[0-9]+\.[0-9]+)'"
},
{
"directory": False,
"path": "README.md",
"pattern": r"\* Version: ([0-9]+\.[0-9]+\.[0-9]+)"
},
{
"directory": True,
"path": "yael/",
"pattern": r"__version__ = \"([0-9]+\.[0-9]+\.[0-9]+)\""
},
{
"directory": True,
"path": "yael/examples/",
"pattern": r"__version__ = \"([0-9]+\.[0-9]+\.[0-9]+)\""
},
]
try:
os.mkdir("bak/")
except OSError:
pass
for source_file in files:
if source_file["directory"]:
paths = []
for root, subdirs, files in os.walk(source_file["path"]):
for name in files:
if name.endswith(".py"):
paths.append(os.path.join(root, name))
else:
paths = [source_file["path"]]
for path in paths:
pattern = source_file["pattern"]
obj = open(path, "r")
contents = obj.read()
obj.close()
bak = open("bak/" + os.path.basename(path), "w")
bak.write(contents)
bak.close()
acc = contents.splitlines()
for i in range(len(acc)):
line = acc[i]
matched = re.search(pattern, line)
if matched != None:
replaced = line.replace(matched.group(1), new_version)
print("%s +%d" % (path, i+1))
print(" - " + line)
print(" + " + replaced)
acc[i] = replaced
out = open("bak/tmp", "w")
out.write("\n".join(acc) + "\n")
out.close()
os.rename("bak/tmp", path)
if __name__ == '__main__':
main()