7 Generate a C header file defining some information about the build.
14 def run(cmd, env=None):
16 Run a command with subprocess and return the stdout.
18 Avoiding use of subprocess.check_output to maintain backwards
21 proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE)
22 out = proc.communicate()
23 if proc.returncode != 0:
24 tpl =
'execution of command "{0}" failed'
25 raise Exception(tpl.format(
' '.join(cmd)))
30 Determine the locale to use in SVN.
35 for line
in run([
'locale',
'-a']).split(
'\n'):
39 if entry.startswith(
'en'):
42 if entry.startswith(
'de'):
52 SVN_NAME_MAPPING = {
'Révision':
'Revision'}
54 def svn(directory='.'):
56 Get information about the SVN repository.
58 Uses the --show-item syntax instead of parsing the normal svn info output
59 to avoid issues with language locales.
62 start_wd = os.getcwd()
65 result =
run([
'svn',
'info'], env={
'LC_MESSAGES':
locale()})
70 for line
in result.split(
'\n'):
73 name, value = line.split(
': ')
78 name = SVN_NAME_MAPPING.get(name.strip(), name.strip())
79 name = name.lower().replace(
' ',
'-')
81 info[name] = value.strip()
83 if name ==
'revision':
84 info[
'commit'] = info[name]
88 def git(directory='.'):
89 start_wd = os.getcwd()
94 result =
run([
'git',
'rev-list',
'--all',
'--count'])
95 info[
'revision'] = str(int(result));
98 result =
run([
'git',
'rev-parse',
'HEAD'])
99 result = result[0:len(result)-1]
100 info[
'commit'] = result.decode(
'ascii')
103 result =
run([
'git',
'describe',
'--tags',
'--always'])
104 result = result[0:len(result)-1]
105 info[
'tag'] = result.decode(
'ascii')
108 result =
run([
'git',
'remote',
'-v'])
110 for line
in result.decode(
'ascii').split(
'\n'):
111 line =
' '.join(line.split())
114 alias, path, direction = line.split(
' ')
118 if alias ==
'origin':
122 info[
'subrepo'] =
'.'
130 TEMPLATE =
"""// SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
131 // SPDX-License-Identifier: LicenseRef-APL-1.1
133 #ifndef __BUILD_INFO__
134 #define __BUILD_INFO__
136 #define GIT_COMMIT_TAG "{tag}"
137 #define GIT_COMMIT_HASH "{commit}"
138 #define GIT_COMMIT_COUNT {revision}
139 #define GIT_PATH "{url}"
141 #define SUBREPO_REVISIONS "{subrepo}"
143 #define SUBREPO_COMMITS "{subrepocommit}"
151 if subprocess.call([
"git",
"branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull,
'w')) == 0:
152 info =
git(os.path.dirname(os.path.abspath(__file__)))
162 info[
'subrepo'] =
','.join([
'{0[0]}={0[1]}'.format(v)
for v
in repo_versions])
165 newline =
'," \\\n' +
' ' * 24 +
'"'
166 info[
'subrepocommit'] = newline.join([
'{0[0]}={0[1]}'.format(v)
for v
in repo_commits])
170 info[
'tag'] =
"built outside a repository"
171 info[
'commit'] =
"built outside a repository"
173 info[
'url'] =
"https://git.opencarp.org/openCARP/openCARP.git"
175 info[
'subrepocommit'] =
""
177 return TEMPLATE.format(**info)
187 if os.path.isfile(filename):
188 with open(filename,
'r')
as myfile:
189 info_h = myfile.read()
190 if info_h == git_info:
195 f = open(filename,
"w")
198 if __name__ ==
'__main__':
199 if(len(sys.argv) > 1):
200 filename = sys.argv[1]
202 filename =
'build_info.h'
def print_build_info(filename)