7 Generate a C header file defining some information about the build.
15 def run(cmd, env=None):
17 Run a command with subprocess and return the stdout.
19 Avoiding use of subprocess.check_output to maintain backwards
22 proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE)
23 out = proc.communicate()
24 if proc.returncode != 0:
25 tpl =
'execution of command "{0}" failed'
26 raise Exception(tpl.format(
' '.join(cmd)))
31 Determine the locale to use in SVN.
36 for line
in run([
'locale',
'-a']).split(
'\n'):
40 if entry.startswith(
'en'):
43 if entry.startswith(
'de'):
53 SVN_NAME_MAPPING = {
'Révision':
'Revision'}
55 def svn(directory='.'):
57 Get information about the SVN repository.
59 Uses the --show-item syntax instead of parsing the normal svn info output
60 to avoid issues with language locales.
63 start_wd = os.getcwd()
66 result =
run([
'svn',
'info'], env={
'LC_MESSAGES':
locale()})
71 for line
in result.split(
'\n'):
74 name, value = line.split(
': ')
79 name = SVN_NAME_MAPPING.get(name.strip(), name.strip())
80 name = name.lower().replace(
' ',
'-')
82 info[name] = value.strip()
84 if name ==
'revision':
85 info[
'commit'] = info[name]
89 def git(directory='.'):
90 start_wd = os.getcwd()
95 result =
run([
'git',
'rev-list',
'--all',
'--count'])
96 info[
'revision'] = str(int(result));
99 result =
run([
'git',
'rev-parse',
'HEAD'])
100 result = result[0:len(result)-1]
101 info[
'commit'] = result.decode(
'ascii')
104 result =
run([
'git',
'describe',
'--tags',
'--always'])
105 result = result[0:len(result)-1]
106 info[
'tag'] = result.decode(
'ascii')
109 result =
run([
'git',
'remote',
'-v'])
111 for line
in result.decode(
'ascii').split(
'\n'):
112 line =
' '.join(line.split())
115 alias, path, direction = line.split(
' ')
119 if alias ==
'origin':
121 parts = urllib.parse.urlsplit(path)
122 info[
'url'] = urllib.parse.urlunsplit(parts._replace(netloc=parts.netloc.rpartition(
'@')[2]))
125 info[
'subrepo'] =
'.'
133 TEMPLATE =
"""// SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
134 // SPDX-License-Identifier: LicenseRef-APL-1.1
136 #ifndef __BUILD_INFO__
137 #define __BUILD_INFO__
139 #define GIT_COMMIT_TAG "{tag}"
140 #define GIT_COMMIT_HASH "{commit}"
141 #define GIT_COMMIT_COUNT {revision}
142 #define GIT_PATH "{url}"
144 #define SUBREPO_REVISIONS "{subrepo}"
146 #define SUBREPO_COMMITS "{subrepocommit}"
154 if subprocess.call([
"git",
"branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull,
'w')) == 0:
155 info =
git(os.path.dirname(os.path.abspath(__file__)))
165 info[
'subrepo'] =
','.join([
'{0[0]}={0[1]}'.format(v)
for v
in repo_versions])
168 newline =
'," \\\n' +
' ' * 24 +
'"'
169 info[
'subrepocommit'] = newline.join([
'{0[0]}={0[1]}'.format(v)
for v
in repo_commits])
173 info[
'tag'] =
"built outside a repository"
174 info[
'commit'] =
"built outside a repository"
176 info[
'url'] =
"https://git.opencarp.org/openCARP/openCARP.git"
178 info[
'subrepocommit'] =
""
180 return TEMPLATE.format(**info)
190 if os.path.isfile(filename):
191 with open(filename,
'r')
as myfile:
192 info_h = myfile.read()
193 if info_h == git_info:
198 f = open(filename,
"w")
201 if __name__ ==
'__main__':
202 if(len(sys.argv) > 1):
203 filename = sys.argv[1]
205 filename =
'build_info.h'
def print_build_info(filename)