forked from machinekit/machinekit-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntests.py
executable file
·416 lines (353 loc) · 15.2 KB
/
runtests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
#####################################################################
# Description: runtests.py
#
# This file, 'runtests.py', runs Machinekit-HAL's
# runtests script
#
# Copyright (C) 2020 - Jakub Fišer <jakub DOT fiser AT eryaf DOT com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
######################################################################
"""
Script for running Machinekit-HAL runtests
"""
# Debian 9 Stretch, Ubuntu 18.04 Bionic and (probably) other older distributions
# need in environment LANG=C.UTF-8 (or other similar specification of encoding)
# to properly function
__license__ = "LGPL 2.1"
import argparse
import sh
import os
import sys
import stat
import shutil
import pathlib
import tempfile
#from contextlib import nullcontext
from typing import Union, List
import importlib.util
spec = importlib.util.spec_from_file_location(
"machinekit_hal_script_helpers",
"{0}/support/python/machinekit_hal_script_helpers.py".format(
os.path.realpath(
"{0}/..".format(os.path.dirname(os.path.abspath(__file__)))
)))
helpers = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helpers)
# TODO: Clean this up!
class Build():
Generators = {
"Unix Makefiles": ["make", "M", "unix"],
"Ninja Multi-Config": ["Ninja", "N", "Ninja Multi-Config"],
}
Configs = [
"Debug",
"Release",
"RelWithDebInfo",
]
def __init__(self,
source_directory: Union[str, pathlib.Path],
build_directory: Union[str, pathlib.Path],
generator: str,
configs: Union[List[str], str],
parallel_jobs: int):
if not isinstance(source_directory, pathlib.Path):
source_directory = pathlib.Path(source_directory)
if not isinstance(build_directory, pathlib.Path):
build_directory = pathlib.Path(build_directory)
if generator not in Build.Generators.keys():
raise ValueError(
f"Generator has to be one of {Build.Generators}!")
self.generator = generator
if isinstance(configs, str):
configs = list(configs)
if not set(configs) <= set(Build.Configs):
raise ValueError(
f"Configs must be in {Build.Configs}!")
self.configs = configs
# Possible to be run from anywhere in Machinekit-HAL's repository tree
self.source_directory = helpers.NormalizeMachinekitHALPath(
source_directory)()
self.build_directory = build_directory
if not self.build_directory.is_absolute():
self.build_directory = self.build_directory.resolve()
if not self.build_directory.is_dir():
self.build_directory.mkdir() # Throws in case Path is another object
self.parallel_jobs = parallel_jobs
def disable_zeroconf(self) -> None:
ini_files = []
ini_postfix = "etc/machinekit/hal/machinekit.ini"
if self.generator == "Ninja Multi-Config":
for prefix in self.configs:
ini_files.append(self.build_directory / prefix / ini_postfix)
elif self.generator == "Unix Makefiles":
ini_files.append(self.build_directory / ini_postfix)
new_ini_items = "ANNOUNCE_IPV4=0\nANNOUNCE_IPV6=0\n"
for ini_file in ini_files:
ini_file.chmod(stat.S_IWUSR | stat.S_IRUSR)
with open(ini_file, "a") as writer:
writer.write(new_ini_items)
def _envrc_path(self, config: str) -> pathlib.Path:
if not config or config not in self.configs:
raise ValueError("Config for which .envrc is required must be of "
f"previously configured: {self.configs}")
if self.generator == "Ninja Multi-Config":
return self.build_directory / config / ".envrc"
else:
return self.build_directory / ".envrc"
def run_runtests(self, test_path: pathlib.Path = None) -> None:
if test_path:
test_path = test_path if isinstance(
test_path, pathlib.Path) else pathlib.Path(test_path)
for config in self.configs:
config_test_path = self.build_directory / \
(config if self.generator == "Ninja Multi-Config" else ".") / \
"share" / "machinekit" / "hal" / "testsuite" / \
"runtests" if test_path is None else test_path
test_directory = pathlib.Path(tempfile.mkdtemp(suffix=config))
destination = test_directory / "runtests"
print(
f"--->Tests for config {config} are run in directory {destination}.<---"
"\n====================================")
shutil.copytree(config_test_path, destination, ignore=shutil.ignore_patterns(
'pipe-*', 'result', 'strerr'))
bash_command_string = f". {self._envrc_path(config)}; run_runtests {destination}"
sh.bash("-c", bash_command_string,
_out=sys.stdout.buffer, _err=sys.stderr.buffer)
def run_ctests(self) -> None:
for config in self.configs:
sh.ctest("-C", config, _cwd=self.build_directory,
_out=sys.stdout.buffer, _err=sys.stderr.buffer)
def run_python_tests(self: object) -> None:
runpytest_path = self.source_directory / "tests" / "nosetests" / "runpytest.sh"
for config in self.configs:
print(
f"--->PyTests for config {config}.<---"
"\n====================================")
bash_command_string = f". {self._envrc_path(config)}; {runpytest_path}"
sh.bash("-c", bash_command_string,
_out=sys.stdout.buffer,
_err=sys.stderr.buffer)
def configure(self,
cache: dict = dict()) -> None:
toolchain_file = self.source_directory / \
"debian" / "debianMultiarchToolchain.cmake"
if "CMAKE_TOOLCHAIN_FILE" not in cache:
cache.update(dict(CMAKE_TOOLCHAIN_FILE=toolchain_file))
cache_list = [f"-D{key}={value}" for key,
value in cache.items()]
sh.cmake("-S", self.source_directory,
"-B", self.build_directory,
"-G", self.generator,
*cache_list,
_out=sys.stdout.buffer)
def build(self,
target: str = None,
sudo: bool = False) -> None:
build_additional = list()
if target is not None:
build_additional.append(["--target", target])
# Available only in Python 3.7 and later
# if sudo:
# _context = sh.contrib.sudo(
# password=self.sudo_password, _with=True, _out=sys.stdout.buffer)
# else:
# _context = nullcontext()
for config in self.configs:
# Until fully supported, use ExitStack
# with _context:
if not sudo:
sh.cmake("--build", self.build_directory,
"-j", self.parallel_jobs,
"--verbose",
"--config", config,
*build_additional,
_out=sys.stdout.buffer)
else:
# Using `sh.contrib.sudo` is not possible because of the old version of `sh`
# in Ubuntu Bionic registry
sudo_cmd = sh.sudo.bake("-S", _in=self.sudo_password)
sudo_cmd.cmake("--build", self.build_directory,
"-j", self.parallel_jobs,
"--verbose",
"--config", config,
*build_additional,
_out=sys.stdout.buffer)
def test_for_sudo(self: object) -> bool:
try:
sh.dpkg_query("-W", "sudo", _tty_out=False)
return True
except Exception:
return False
def am_i_root(self: object) -> bool:
uid = os.getuid()
if uid in [0]:
return True
return False
def test_sudo_rights(self: object, sudo_password) -> bool:
# Test for sudo rights if some password was specified
try:
sh.sudo("-S", "true", _in=sudo_password)
self.sudo_password = sudo_password
return True
except Exception:
return False
def main(args):
""" Main entry point of the app """
exception = False
try:
build_script = Build(
args.source_path,
args.build_path if args.build_path else f"{args.source_path}/build",
args.generator,
args.configs,
args.jobs)
if build_script.am_i_root():
raise ValueError(
"This script cannot be run under the 'root' user.")
if not build_script.test_for_sudo():
raise ValueError(
"The 'sudo' executable has to be installed.")
if not build_script.test_sudo_rights(args.sudo_password):
raise ValueError(
"The user has to be able to use 'sudo'.")
if not args.no_configure:
build_script.configure()
if not args.no_build:
build_script.build()
build_script.build(target='binary_tree_venv')
build_script.build(target='setuid', sudo=True)
if not (args.no_runtests or args.no_ctests or args.no_python_tests):
build_script.disable_zeroconf()
if not args.no_runtests:
try:
build_script.run_runtests(args.test_path)
except Exception as e:
exception = True
print(e)
print("Run of Machinekit-HAL's Runtests FAILED!")
if not args.no_ctests:
try:
build_script.run_ctests()
except Exception as e:
exception = True
print(e)
print("Run of Machinekit-HAL's CTests FAILED!")
if not args.no_python_tests:
try:
build_script.run_python_tests()
except Exception as e:
exception = True
print(e)
print("Run of Machinekit-HAL's PyTests FAILED!")
except ValueError as e:
print(e)
exception = True
if exception:
print("Machinekit-HAL test suite FAILED!")
sys.exit(1)
print("Machinekit-HAL test suite ran successfully!")
class SelectGeneratorAction(argparse.Action):
def normalize_generator(self, value: str) -> str:
for key, values in Build.Generators.items():
for alias in values:
if alias == value:
return key
raise argparse.ArgumentError(
self, f"Generator {value} is not valid value!")
def __call__(self, parser, namespace, values, option_string=None):
if type(values) == list:
raise argparse.ArgumentError(
self, "Only one generator can be specified!")
generator = self.normalize_generator(values)
setattr(namespace, self.dest, generator)
if __name__ == "__main__":
""" This is executed when run from the command line """
parser = argparse.ArgumentParser(
description="Build Machinekit-HAL and run Runtests on it")
# Optional argument for path to Machinekit-HAL repository
parser.add_argument("-p",
"--source-path",
action=helpers.PathExistsAction,
dest="source_path",
default=os.getcwd(),
metavar="PATH",
help="Path to root of Machinekit-HAL repository")
# Optional argument for Debian host architecture
parser.add_argument("-b",
"--build-path",
action="store",
dest="build_path",
default=None,
metavar="PATH",
help="Path to the build tree of Machinekit-HAL")
parser.add_argument("-g",
"--generator",
action=SelectGeneratorAction,
dest="generator",
choices=[alias for aliases in Build.Generators.values()
for alias in aliases],
default=list(Build.Generators.keys())[0],
metavar="GENERATOR",
help="Generator for which create the build files")
parser.add_argument("-c",
"--config",
action="store",
dest="configs",
nargs="+",
choices=Build.Configs,
default=[Build.Configs[0]],
metavar="CONFIG",
help="Generator for which create the build files")
parser.add_argument("-t",
"--tests-path",
dest="test_path",
action=helpers.PathExistsAction,
metavar="PATH",
help="Path with test definitions.")
parser.add_argument("-s",
"--sudo-password",
dest="sudo_password",
action="store",
metavar="PASSWORD",
default="",
help="Password for usage with sudo command.")
parser.add_argument("--no-configure",
action="store_true",
help="Do not configure and generate the CMake buildsystem")
parser.add_argument("--no-build",
action="store_true",
help="Do not build")
parser.add_argument("--no-runtests",
action="store_true",
help="Do not run 'runtests'")
parser.add_argument("--no-ctests",
action="store_true",
help="Do not run CMake CTest tests")
parser.add_argument("--no-python-tests",
action="store_true",
help="Do not run python tests")
parser.add_argument("-j",
"--jobs",
dest="jobs",
action="store",
type=int,
metavar="PROCESSES",
default=sh.nproc(_tty_out=False).strip(),
help="Number of processes started at given time by the underlying buildsystem.")
args = parser.parse_args()
main(args)