-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_version.py
96 lines (79 loc) · 2.8 KB
/
set_version.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
# -*- coding: utf-8 -*-
import subprocess
import fileinput
import re
import os
LF = "\n" if os.name == "nt" else "\r\n"
OFFSET = 41
MAIN = "./main.cpp"
MAIN_MATCH = "VERSION("
README = "./README.md"
README_MATCH = "badge/version-"
RM_MATCH_POST= "-brightgreen"
RESOURCE = "./resources.rc"
GIT_COMMIT_CMD = ['git', 'rev-list', '--all', '--count']
GIT_COMMIT_COUNT = str(int(subprocess.check_output(GIT_COMMIT_CMD)) + 1 - OFFSET)
# Major.Minor.Patch-extra
VERSION = (0, 0, 0, "")
def split_version(line):
global VERSION
pre, version = line.split('(')
version, post = version.split(')')
major, minor, patch, extra = version.split(', ')
minor = GIT_COMMIT_COUNT
VERSION = (int(major), int(minor), int(patch), extra)
return pre, version, post
def version_string(delim='.', edelim='-', simple=True):
if not edelim or not VERSION[-1]:
return "{2}{0}{3}{0}{4}{0}0".format(delim, edelim, *VERSION)
else:
newVersion = VERSION
if simple:
extra = VERSION[3].lower()
if "alpha" in extra:
extra = "alpha"
elif "beta" in extra:
extra = "beta"
elif "rc" in extra:
extra = "rc"
newVersion = (newVersion[0], newVersion[1], newVersion[2], extra)
return "{2}{0}{3}{0}{4}{1}{5}".format(delim, edelim, *newVersion)
# Acquire version from main.cpp
try:
for line in fileinput.FileInput(MAIN, inplace=1):
if MAIN_MATCH in line:
pre, v, post = split_version(line)
line = pre + '(' + version_string(", ", ", ", False) + ')' + post
print(line.rstrip(), end=LF)
except Exception as e:
fileinput.close()
print(e)
print("change version to: " + version_string(", ", ", ", False))
print("change version to: " + version_string(".", "-", True))
# Set version in README
try:
for line in fileinput.FileInput(README, inplace=1):
if README_MATCH in line:
pre, version = line.split(README_MATCH)
version, post = version.split(RM_MATCH_POST)
line = pre + README_MATCH \
+ version_string(".", "--") \
+ RM_MATCH_POST + post
print(line.rstrip(), end=LF)
except Exception as e:
fileinput.close()
print(e)
# Set version in Resources
try:
for line in fileinput.FileInput(RESOURCE, inplace=1):
if "FILEVERSION" in line or "PRODUCTVERSION" in line:
splits = line.split(' ')
splits[-1] = version_string(",", "")
line = ' '.join(splits)
elif "FileVersion" in line or "ProductVersion" in line:
pre, _ = line.split(', ')
line = pre + ", \"" + version_string(".", "-") + "\""
print(line.rstrip(), end=LF)
except Exception as e:
fileinput.close()
print(e)