-
Notifications
You must be signed in to change notification settings - Fork 68
/
build_version.py
71 lines (56 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
import datetime
import re
import subprocess
import sys
Import("env")
# Don't do anything if this is an 'uploadfs' or 'erase' target
cmdline = ','.join(sys.argv)
if cmdline.find('buildfs') or cmdline.find('uploadfs'):
# Change build tool if we are using LittleFS
if any("FLASH_LITTLEFS" in x for x in env['BUILD_FLAGS']):
print("\033[1;31mReplacing MKSPIFFSTOOL with mklittlefs\033[1;37m")
#env.Replace (MKSPIFFSTOOL = "mklittlefs")
# Disable automatic versioning
if 1:
print("Automatic versioning disabled")
# Don't do anything if nothing has changed
elif 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.utcnow().strftime("%Y-%m-%d %H:%M:%S")
rxs = ['^#define FN_VERSION_MAJOR (\\w)', '^#define FN_VERSION_MINOR (\\w)',
'^(#define FN_VERSION_BUILD)', '^(#define FN_VERSION_DATE)', '^(#define FN_VERSION_FULL)']
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)
else:
fout.write(line)
fout.close()