-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkversion-1.py
84 lines (68 loc) · 2.79 KB
/
mkversion-1.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
#!/usr/bin/env python
# ------------------------------------------------------------------------------
# make the "git describe" string to a unique build number
# and generate a header file file
# In the long version it looks always so:
# * rev_1-1-gdb6f9da ==> SW_VERSION = 101
# * rev_2-0-gdb6f9da ==> SW_VERSION = 200
# This both tags refer to the same software version, because the commit hash
# (gdb6f9da) is equal.
#
# Call: python Tools\mkversion.py Src\Include\version_params.h
# ------------------------------------------------------------------------------
# === imports ==================================================================
import os, sys, time
import argparse
# === globals ==================================================================
HEADER_FMT = """\
/* autogenerated header file, do not modify
* command: %(cmd_line)s
* date: %(build_date)s
*/
#ifndef %(inc_guard)s
#define %(inc_guard)s (1)
#define VERSION_MAJOR (%(major)d)
#define VERSION_MINOR (%(minor)d)
#define VERSION_PATCH (%(patch)d)
#define VERSION_COMMIT_HASH (\"%(commit_hash)s\")
#define VERSION_IS_DIRTY (%(dirty)d)
#define BUILD_ID (%(build_id)x)
#define BUILD_DATE (\"%(build_date)s\")
#define GIT_ID (\"%(git_id)s\")
#endif /* #ifndef %(inc_guard)s*/
"""
# === main =====================================================================
if __name__ == "__main__":
argp = argparse.ArgumentParser()
argp.add_argument('--type', action='store', required=False, help='tag type')
argp.add_argument('out', help='output file')
args = argp.parse_args()
tag_format = 'release_'
if args.type is not None:
tag_format = args.type + '_' + tag_format;
git_id = os.popen('git describe --tags --long --dirty=:M --abbrev=7 --match=' + tag_format + '*').read().strip()
commit_hash = os.popen('git rev-parse --verify HEAD').read().strip()
major, minor, patch, ghash = git_id.replace(tag_format,"").split("-")
is_dirty = int(git_id.find(":M") > 0)
local_commits = int(patch != "0")
build_id = (int(major) << 24) | (int(minor) << 16) | (int(patch))
try:
fout = open(args.out, "w")
except:
print("\n\n###########################\n### FAILED TO OPEN FILE ###\n###########################\n\n");
fout = sys.stdout
contents = {
"cmd_line" : " ".join(sys.argv),
"inc_guard" : os.path.basename(fout.name).upper().replace(".","_"),
"major" : int(major),
"minor" : int(minor),
"patch" : int(patch),
"commit_hash" : commit_hash,
"dirty" : is_dirty,
"build_id" : build_id,
"build_date" : time.strftime("%Y-%m-%d %H:%M:%S %Z"),
"git_id" : git_id
}
fout.write(HEADER_FMT % contents)
if fout != sys.stdout:
fout.close()