Skip to content

Commit 7e3ce84

Browse files
Updated increment_version.py to not overwrite the first lines with comments.
1 parent b1674eb commit 7e3ce84

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

increment_version.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
import toml
2+
import os
23

34
def main():
4-
with open("pyproject.toml", 'r+') as f:
5-
data = toml.load(f)
6-
project = data.get("project")
7-
if not project is None:
8-
version = project.get("version")
9-
if not version is None:
10-
a,b,c = map(int, version.split("."))
11-
if c < 99:
12-
c += 1
13-
elif b < 99:
14-
c = 0
15-
b += 1
5+
try:
6+
os.rename("pyproject.toml", "pyproject.txt")
7+
with open("pyproject.txt", 'r+') as f:
8+
comment = []
9+
while True:
10+
line = f.readline()
11+
if line[0] == "#":
12+
comment = [line] + comment
1613
else:
17-
c = 0
18-
b = 0
19-
a += 1
20-
data["project"]["version"] = str(a) + "." + str(b) + "." + str(c)
21-
if not data == {}:
22-
with open("pyproject.toml", 'w') as f:
23-
toml.dump(data, f)
14+
break
15+
os.rename("pyproject.txt", "pyproject.toml")
16+
with open("pyproject.toml", 'r+') as f:
17+
data = toml.load(f)
18+
project = data.get("project")
19+
if not project is None:
20+
version = project.get("version")
21+
if not version is None:
22+
a,b,c = map(int, version.split("."))
23+
if c < 99:
24+
c += 1
25+
elif b < 99:
26+
c = 0
27+
b += 1
28+
else:
29+
c = 0
30+
b = 0
31+
a += 1
32+
data["project"]["version"] = str(a) + "." + str(b) + "." + str(c)
33+
if not data == {}:
34+
with open("pyproject.toml", 'w') as f:
35+
toml.dump(data, f)
36+
os.rename("pyproject.toml", "pyproject.txt")
37+
with open("pyproject.txt", 'r+') as f:
38+
content = f.readlines()
39+
content = comment + content
40+
with open("pyproject.txt", 'w') as f:
41+
for line in content:
42+
f.write(line)
43+
os.rename("pyproject.txt", "pyproject.toml")
44+
except (FileNotFoundError):
45+
pass
2446

2547
main()

0 commit comments

Comments
 (0)