Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/build_config/version.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file version.py
15
# @author Michael Behrisch
16
# @author Daniel Krajzewicz
17
# @author Jakob Erdmann
18
# @date 2007
19
20
"""
21
This script rebuilds "<BUILD_DIR>/src/version.h", the file which
22
lets the applications know the version of their build.
23
It does this by parsing the output of git describe where the function is
24
implemented in sumolib.
25
If the version file is newer than the .git index file or the revision cannot be
26
determined any existing version.h is kept.
27
"""
28
from __future__ import absolute_import
29
from __future__ import print_function
30
31
import sys
32
from os.path import dirname, exists, getmtime, join, abspath
33
34
sys.path.append(dirname(dirname(abspath(__file__))))
35
import sumolib # noqa
36
37
38
SUMO_ROOT = abspath(join(dirname(__file__), '..', '..'))
39
40
41
def get_version(padZero=True):
42
return sumolib.version.gitDescribe(gitDir=join(SUMO_ROOT, ".git"), padZero=padZero)
43
44
45
def get_pep440_version():
46
v = get_version(padZero=False).replace("_", ".").replace("+", ".post")
47
v = v[1:v.rfind("-")]
48
vs = v.split(".")
49
if len(vs) == 4 and vs[3] == "post0":
50
return v[:-6]
51
return v
52
53
54
def create_version_file(versionFile, revision):
55
with open(versionFile, 'w') as f:
56
print('#define VERSION_STRING "%s"' % revision, file=f)
57
58
59
def filter_setup_py(in_file, out_file):
60
with open(in_file) as inf, open(out_file, "w") as outf:
61
for line in inf:
62
if line.startswith("package_dir = "):
63
print('package_dir = os.path.dirname(os.path.abspath(__file__))', file=outf)
64
elif line.startswith("SUMO_VERSION = "):
65
print('SUMO_VERSION = "%s"' % get_pep440_version(), file=outf)
66
elif not line.startswith("import version"):
67
outf.write(line)
68
69
70
def main():
71
if len(sys.argv) > 2:
72
filter_setup_py(sys.argv[1], sys.argv[2])
73
return
74
# determine output file
75
if len(sys.argv) > 1:
76
versionDir = sys.argv[1]
77
if sys.argv[1] == "-":
78
sys.stdout.write(get_version())
79
return
80
else:
81
versionDir = join(SUMO_ROOT, "src")
82
versionFile = join(versionDir, 'version.h')
83
84
vcsFile = join(SUMO_ROOT, ".git", "index")
85
try:
86
if exists(vcsFile):
87
if not exists(versionFile) or getmtime(versionFile) < getmtime(vcsFile):
88
# vcsFile is newer. lets update the revision number
89
print('generating %s from revision in %s' % (versionFile, vcsFile))
90
create_version_file(versionFile, get_version())
91
else:
92
print("version control file '%s' not found" % vcsFile)
93
if not exists(versionFile):
94
print('trying to generate version file %s from existing header' % versionFile)
95
create_version_file(versionFile, sumolib.version.fromVersionHeader())
96
except Exception as e:
97
print("Error creating", versionFile, e)
98
try:
99
# try at least to create something
100
create_version_file(versionFile, "UNKNOWN")
101
except Exception as ee:
102
print("Error creating", versionFile, ee)
103
pass
104
105
106
if __name__ == "__main__":
107
main()
108
109