-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild_version.py
74 lines (58 loc) · 2.14 KB
/
build_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
import datetime
import re
import subprocess
import sys
Import("env")
# Need to run this command in the PIO virtual environment
#env.Execute("$PYTHONEXE -m pip list --format=json --disable-pip-version-check");
#exit(0);
# Disable automatic versioning
#if 1:
# print("Automatic versioning disabled")
# exit
# Don't do anything if nothing has changed
if len(subprocess.check_output(["git", "diff", "--name-only"], universal_newlines=True)) == 0:
print("Nothing has changed")
else:
try:
ver_build = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], universal_newlines=True).strip()
except subprocess.CalledProcessError as e:
ver_build = "NOGIT"
header_file = "include/version.h"
ver_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
fw_date = datetime.datetime.now().strftime("%Y%m%d.%H")
rxs = ['^#define FN_VERSION_MAJOR (\w+)', '^#define FN_VERSION_MINOR (\w+)',
'^(#define FN_VERSION_BUILD)', '^(#define FN_VERSION_DATE)', '^(#define FN_VERSION_FULL)',
'^(#define FW_VERSION)']
ver_maj = ""
ver_min = ""
txt = [line for line in open(header_file)]
fout = open(header_file, "w")
for line in txt:
for i in range(len(rxs)):
m = re.match(rxs[i], line)
if m is not None:
break
if m is not None:
if i == 0:
ver_maj = m.groups(0)[0]
fout.write(line)
elif i == 1:
ver_min = m.groups(0)[0]
fout.write(line)
elif i == 2:
line = m.groups(0)[0] + " \"" + ver_build + "\"\n"
fout.write(line)
elif i == 3:
line = m.groups(0)[0] + " \"" + ver_date + "\"\n"
fout.write(line)
elif i == 4:
line = m.groups(0)[0] + " \"" + ver_maj + "." + \
ver_min + "." + ver_build + "\"\n"
fout.write(line)
elif i == 5:
line = m.groups(0)[0] + " \"" + fw_date + "\"\n"
fout.write(line)
else:
fout.write(line)
fout.close()