-
Notifications
You must be signed in to change notification settings - Fork 30
/
_runner.py
89 lines (81 loc) · 2.68 KB
/
_runner.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
# Data Parallel Control (dpctl)
#
# Copyright 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import inspect
def has_nondefault_params(sgn):
for v in sgn.parameters.values():
if v.default is inspect._empty:
return True
return False
def run_examples(example_description, glbls_dict):
parser = argparse.ArgumentParser(
description=example_description,
)
parser.add_argument(
"-r",
"--run",
type=str,
help="Functions to execute. Use --run all to run all of them.",
)
parser.add_argument(
"-l",
"--list",
action="store_true",
help="List available function names to run",
)
parser.add_argument(
"-q", "--quiet", action="store_true", help="Do not echo example name."
)
args = parser.parse_args()
if args.list or not args.run:
fns = []
for n in glbls_dict:
if inspect.isfunction(glbls_dict.get(n)):
fns.append(n)
if fns:
print("Available examples:")
print(", ".join(fns))
else:
print("No examples are available.")
exit(0)
if args.run == "all":
fns = []
for n in glbls_dict:
if inspect.isfunction(glbls_dict.get(n)):
fns.append(n)
args.run = fns
else:
args.run = args.run.split()
if args.run:
for fn in args.run:
if fn in glbls_dict:
clbl = glbls_dict.get(fn)
sgn = inspect.signature(clbl)
print("")
if has_nondefault_params(sgn):
if not args.quiet:
print(
f"INFO: Skip exectution of {fn} as it "
"requires arguments"
)
else:
if not args.quiet:
print(f"INFO: Executing example {fn}")
clbl()
if not args.quiet:
print("INFO: ===========================")
else:
raise ValueError("No function to run was specified")