sumolib.version

This module contains functions to determine the current SUMO version.

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    version.py
14# @author  Michael Behrisch
15# @author  Daniel Krajzewicz
16# @author  Jakob Erdmann
17# @date    2007
18
19"""
20This module contains functions to determine the current SUMO version.
21"""
22from __future__ import absolute_import
23from __future__ import print_function
24
25import subprocess
26from os.path import dirname, exists, join
27
28try:
29    # this tries to determine the version number of an installed wheel
30    import importlib.metadata  # noqa
31    _version = importlib.metadata.version("sumolib")
32except ImportError:
33    # this is the fallback version, it gets replaced with the current version on "make install" or "make dist"
34    _version = "0.0.0"
35
36GITDIR = join(dirname(__file__), '..', '..', '.git')
37
38
39def fromVersionHeader():
40    """
41    Returns the version as defined in "include/version.h" or as a fallback
42    "src/config.h.cmake". Since the latter only contains the last release info it is extended
43    with "-0000000000" to mark that it may not be a release and the commit hash is unknown.
44    """
45    versionFile = join(dirname(__file__), '..', '..', 'include', 'version.h')
46    if exists(versionFile):
47        with open(versionFile) as f:
48            version = f.read().split()
49        if len(version) > 2:
50            return version[2][1:-1]
51    # try to find the version in the config.h
52    configFile = join(dirname(__file__), '..', '..', 'src', 'config.h.cmake')
53    if exists(configFile):
54        with open(configFile) as f:
55            config = f.read()
56        version = config.find("VERSION_STRING") + 16
57        if version > 16:
58            return "v" + config[version:config.find('"\n', version)] + "-" + (10 * "0")
59    return "v" + _version
60
61
62def gitDescribe(commit="HEAD", gitDir=GITDIR, padZero=True):
63    """
64    The original git describe format is "<tag>-<commit_count>-g<hash>".
65    This function converts it to "<tag>+<commit_count>-<hash>".
66    If padZero is true (the default), the commit count is padded to four digits.
67    If the git describe call fails or cannot find tags (because it is a shallow clone),
68    the result of fromVersionHeader is returned.
69    """
70    command = ["git", "describe", "--long", "--always", commit]
71    if gitDir:
72        command[1:1] = ["--git-dir=" + gitDir]
73        if not exists(gitDir):
74            return fromVersionHeader()
75    try:
76        d = subprocess.check_output(command, universal_newlines=True).strip()
77    except (subprocess.CalledProcessError, EnvironmentError):
78        return fromVersionHeader()
79    if "-" in d:
80        # remove the "g" in describe output
81        d = d.replace("-g", "-")
82        m1 = d.find("-") + 1
83        m2 = d.find("-", m1)
84        diff = max(0, 4 - (m2 - m1)) if padZero else 0
85        # prefix the number of commits with a "+" and pad with 0
86        return d[:m1].replace("-", "+") + (diff * "0") + d[m1:]
87    return fromVersionHeader()
GITDIR = '/home/delphi/gcc/sumo/tools/sumolib/../../.git'
def fromVersionHeader():
40def fromVersionHeader():
41    """
42    Returns the version as defined in "include/version.h" or as a fallback
43    "src/config.h.cmake". Since the latter only contains the last release info it is extended
44    with "-0000000000" to mark that it may not be a release and the commit hash is unknown.
45    """
46    versionFile = join(dirname(__file__), '..', '..', 'include', 'version.h')
47    if exists(versionFile):
48        with open(versionFile) as f:
49            version = f.read().split()
50        if len(version) > 2:
51            return version[2][1:-1]
52    # try to find the version in the config.h
53    configFile = join(dirname(__file__), '..', '..', 'src', 'config.h.cmake')
54    if exists(configFile):
55        with open(configFile) as f:
56            config = f.read()
57        version = config.find("VERSION_STRING") + 16
58        if version > 16:
59            return "v" + config[version:config.find('"\n', version)] + "-" + (10 * "0")
60    return "v" + _version

Returns the version as defined in "include/version.h" or as a fallback "src/config.h.cmake". Since the latter only contains the last release info it is extended with "-0000000000" to mark that it may not be a release and the commit hash is unknown.

def gitDescribe( commit='HEAD', gitDir='/home/delphi/gcc/sumo/tools/sumolib/../../.git', padZero=True):
63def gitDescribe(commit="HEAD", gitDir=GITDIR, padZero=True):
64    """
65    The original git describe format is "<tag>-<commit_count>-g<hash>".
66    This function converts it to "<tag>+<commit_count>-<hash>".
67    If padZero is true (the default), the commit count is padded to four digits.
68    If the git describe call fails or cannot find tags (because it is a shallow clone),
69    the result of fromVersionHeader is returned.
70    """
71    command = ["git", "describe", "--long", "--always", commit]
72    if gitDir:
73        command[1:1] = ["--git-dir=" + gitDir]
74        if not exists(gitDir):
75            return fromVersionHeader()
76    try:
77        d = subprocess.check_output(command, universal_newlines=True).strip()
78    except (subprocess.CalledProcessError, EnvironmentError):
79        return fromVersionHeader()
80    if "-" in d:
81        # remove the "g" in describe output
82        d = d.replace("-g", "-")
83        m1 = d.find("-") + 1
84        m2 = d.find("-", m1)
85        diff = max(0, 4 - (m2 - m1)) if padZero else 0
86        # prefix the number of commits with a "+" and pad with 0
87        return d[:m1].replace("-", "+") + (diff * "0") + d[m1:]
88    return fromVersionHeader()

The original git describe format is "--g". This function converts it to "+-". If padZero is true (the default), the commit count is padded to four digits. If the git describe call fails or cannot find tags (because it is a shallow clone), the result of fromVersionHeader is returned.