Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/state/runner.py
169678 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 runner.py
15
# @author Michael Behrisch
16
# @author Jakob Erdmann
17
# @date 2009-11-04
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
import os
22
import subprocess
23
import sys
24
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
25
import sumolib # noqa
26
27
28
redirectStdout = False
29
redirectStderr = False
30
compare = []
31
if '--compare' in sys.argv:
32
cmpIdx = sys.argv.index('--compare')
33
for c in sys.argv[cmpIdx + 1].split(","):
34
entry = c.split(":") + [0, 0]
35
compare.append((entry[0], int(entry[1]), int(entry[2])))
36
if entry[0] == "stdout":
37
redirectStdout = True
38
if entry[0] == "stderr":
39
redirectStderr = True
40
del sys.argv[cmpIdx:cmpIdx + 2]
41
idx = sys.argv.index(":")
42
saveParams = sys.argv[1:idx]
43
loadParams = sys.argv[idx + 1:]
44
# work around texttests limitation of removing duplicate options
45
if '--mesosim' in loadParams and '--mesosim' not in saveParams:
46
saveParams.append('--mesosim')
47
if '--mesosim' in saveParams and '--mesosim' not in loadParams:
48
loadParams.append('--mesosim')
49
50
# need to add runner.py again in options.complex.meso to ensure it is the
51
# last entry
52
saveParams = [p for p in saveParams if 'runner.py' not in p]
53
loadParams = [p for p in loadParams if 'runner.py' not in p]
54
55
# print("save:", saveParams)
56
# print("load:", loadParams)
57
58
sumoBinary = sumolib.checkBinary("sumo")
59
saveOut = open("save.out", "w") if redirectStdout else sys.stdout
60
loadOut = open("load.out", "w") if redirectStdout else sys.stdout
61
saveErr = open("save.err", "w") if redirectStderr else sys.stderr
62
loadErr = open("load.err", "w") if redirectStderr else sys.stderr
63
subprocess.call([sumoBinary] + saveParams,
64
shell=(os.name == "nt"), stdout=saveOut, stderr=saveErr)
65
subprocess.call([sumoBinary] + loadParams,
66
shell=(os.name == "nt"), stdout=loadOut, stderr=loadErr)
67
if compare:
68
for f in (saveOut, loadOut, saveErr, loadErr):
69
f.flush()
70
for fileType, offsetSave, offsetLoad in compare:
71
if fileType == "stdout":
72
sys.stdout.write(open(saveOut.name).read())
73
saveLines = open(saveOut.name).readlines()[offsetSave:]
74
loadLines = open(loadOut.name).readlines()[offsetLoad:]
75
sys.stdout.write("".join(sumolib.fpdiff.diff(saveLines, loadLines, 0.01)))
76
elif fileType == "stderr":
77
sys.stderr.write(open(saveErr.name).read())
78
saveLines = open(saveErr.name).readlines()[offsetSave:]
79
loadLines = open(loadErr.name).readlines()[offsetLoad:]
80
sys.stderr.write("".join(sumolib.fpdiff.diff(saveLines, loadLines, 0.01)))
81
else:
82
with open(fileType + ".xml") as saved:
83
saveLines = saved.readlines()
84
saveLines = saveLines[saveLines.index("-->\n") + offsetSave:]
85
with open(fileType + "2.xml") as loaded:
86
loadLines = loaded.readlines()
87
loadLines = loadLines[loadLines.index("-->\n") + offsetLoad:]
88
sys.stdout.write("".join(sumolib.fpdiff.diff(saveLines, loadLines, 0.01)))
89
90