Skip to content

Commit b98c8c1

Browse files
committed
Generalize all appropriate arguments and check for file existance
1 parent 43e036d commit b98c8c1

File tree

5 files changed

+45
-52
lines changed

5 files changed

+45
-52
lines changed

tools/build.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@
3535
from tools.build_api import static_analysis_scan, static_analysis_scan_lib, static_analysis_scan_library
3636
from tools.build_api import print_build_results
3737
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT
38+
from utils import argparse_filestring_type
3839

3940
if __name__ == '__main__':
4041
start = time()
4142

4243
# Parse Options
4344
parser = get_default_options_parser()
4445

45-
parser.add_argument("--source", dest="source_dir",
46+
parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
4647
default=None, help="The source (input) directory", action="append")
4748

48-
parser.add_argument("--build", dest="build_dir",
49+
parser.add_argument("--build", dest="build_dir", type=argparse_filestring_type,
4950
default=None, help="The build (output) directory")
5051

5152
parser.add_argument("--no-archive", dest="no_archive", action="store_true",
@@ -160,21 +161,13 @@
160161
# Get target list
161162
if options.mcu:
162163
mcu_list = (options.mcu).split(",")
163-
for mcu in mcu_list:
164-
if mcu not in TARGET_NAMES:
165-
print "Given MCU '%s' not into the supported list:\n%s" % (mcu, TARGET_NAMES)
166-
sys.exit(1)
167164
targets = mcu_list
168165
else:
169166
targets = TARGET_NAMES
170167

171168
# Get toolchains list
172169
if options.tool:
173170
toolchain_list = (options.tool).split(",")
174-
for tc in toolchain_list:
175-
if tc not in TOOLCHAINS:
176-
print "Given toolchain '%s' not into the supported list:\n%s" % (tc, TOOLCHAINS)
177-
sys.exit(1)
178171
toolchains = toolchain_list
179172
else:
180173
toolchains = TOOLCHAINS

tools/get_config.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from tools.options import get_default_options_parser
2929
from tools.build_api import get_config
3030
from config import Config
31+
from utils import argparse_filestring_type
3132
try:
3233
import tools.private_settings as ps
3334
except:
@@ -36,19 +37,15 @@
3637
if __name__ == '__main__':
3738
# Parse Options
3839
parser = get_default_options_parser(add_clean=False, add_options=False)
39-
parser.add_argument("--source", dest="source_dir",
40-
default=None, help="The source (input) directory", action="append")
40+
parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
41+
default=[], help="The source (input) directory", action="append")
4142
parser.add_argument("--prefix", dest="prefix", action="append",
42-
default=None, help="Restrict listing to parameters that have this prefix")
43+
default=[], help="Restrict listing to parameters that have this prefix")
4344
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose",
4445
default=False, help="Verbose diagnostic output")
4546

4647
options = parser.parse_args()
4748

48-
for path in options.source_dir :
49-
if not isdir(path) :
50-
args_error(parser, "[ERROR] you passed \"{}\" to --source, which does not exist".
51-
format(path))
5249
# Target
5350
if options.mcu is None :
5451
args_error(parser, "[ERROR] You should specify an MCU")

tools/make.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,38 @@
4242
from tools.options import get_default_options_parser
4343
from tools.build_api import build_project
4444
from tools.build_api import mcu_toolchain_matrix
45+
from utils import argparse_filestring_type
46+
from argparse import ArgumentTypeError
4547
try:
4648
import tools.private_settings as ps
4749
except:
4850
ps = object()
4951

52+
def test_known(string):
53+
i = int(string)
54+
if i >= 0 and i < len(TESTS) : return i
55+
else : raise ArgumentTypeError("{0} does not index a test".format(i))
56+
57+
def test_name_known(string):
58+
nlist = string.split(',')
59+
for test_id in nlist:
60+
if test_id not in TEST_MAP.keys():
61+
raise ArgumentTypeError("Program with name '%s' not found"% test_id)
62+
63+
return [TEST_MAP[n].n for n in nlist]
64+
5065
if __name__ == '__main__':
5166
# Parse Options
5267
parser = get_default_options_parser()
53-
parser.add_argument("-p",
54-
type=int,
68+
group = parser.add_mutually_exclusive_group(required=True)
69+
group.add_argument("-p",
70+
type=test_known,
5571
dest="program",
5672
help="The index of the desired test program: [0-%d]" % (len(TESTS)-1))
5773

58-
parser.add_argument("-n",
59-
dest="program_name",
74+
group.add_argument("-n",
75+
type=test_name_known,
76+
dest="program",
6077
help="The name of the desired test program")
6178

6279
parser.add_argument("-j", "--jobs",
@@ -104,7 +121,7 @@
104121
default=None, help="Required peripherals")
105122
parser.add_argument("--dep", dest="dependencies",
106123
default=None, help="Dependencies")
107-
parser.add_argument("--source", dest="source_dir",
124+
parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
108125
default=None, help="The source (input) directory", action="append")
109126
parser.add_argument("--duration", type=int, dest="duration",
110127
default=None, help="Duration of the test")
@@ -174,6 +191,7 @@
174191

175192
# Specify a different linker script
176193
parser.add_argument("-l", "--linker", dest="linker_script",
194+
type=argparse_filestring_type,
177195
default=None, help="use the specified linker script")
178196

179197
options = parser.parse_args()
@@ -183,12 +201,6 @@
183201
print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
184202
exit(0)
185203

186-
if options.source_dir:
187-
for path in options.source_dir :
188-
if not isfile(path) and not isdir(path) :
189-
args_error(parser, "[ERROR] you passed \"{}\" to --source, which does not exist".
190-
format(path))
191-
192204
# Print available tests in order and exit
193205
if options.list_tests is True:
194206
print '\n'.join(map(str, sorted(TEST_MAP.values())))
@@ -197,25 +209,9 @@
197209
# force program to "0" if a source dir is specified
198210
if options.source_dir is not None:
199211
p = 0
200-
n = None
201212
else:
202213
# Program Number or name
203-
p, n = options.program, options.program_name
204-
205-
if n is not None and p is not None:
206-
args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
207-
if n:
208-
# We will transform 'n' to list of 'p' (integers which are test numbers)
209-
nlist = n.split(',')
210-
for test_id in nlist:
211-
if test_id not in TEST_MAP.keys():
212-
args_error(parser, "[ERROR] Program with name '%s' not found"% test_id)
213-
214-
p = [TEST_MAP[n].n for n in nlist]
215-
elif p is None or (p < 0) or (p > (len(TESTS)-1)):
216-
message = "[ERROR] You have to specify one of the following tests:\n"
217-
message += '\n'.join(map(str, sorted(TEST_MAP.values())))
218-
args_error(parser, message)
214+
p = options.program
219215

220216
# If 'p' was set via -n to list of numbers make this a single element integer list
221217
if type(p) != type([]):

tools/test.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
from tools.targets import TARGET_MAP
3333
from tools.utils import mkdir, ToolException, NotSupportedException
3434
from tools.test_exporters import ReportExporter, ResultExporterType
35-
from utils import argparse_lowercase_type
35+
from utils import argparse_filestring_type, argparse_lowercase_type
3636

3737
if __name__ == '__main__':
3838
try:
3939
# Parse Options
4040
parser = get_default_options_parser()
4141

42-
parser.add_argument("-D", "",
42+
parser.add_argument("-D",
4343
action="append",
4444
dest="macros",
4545
help="Add a macro definition")
@@ -51,6 +51,7 @@
5151
help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
5252

5353
parser.add_argument("--source", dest="source_dir",
54+
type=argparse_filestring_type,
5455
default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
5556

5657
parser.add_argument("--build", dest="build_dir",
@@ -60,18 +61,20 @@
6061
default=False, help="List (recursively) available tests in order and exit")
6162

6263
parser.add_argument("-p", "--paths", dest="paths",
64+
type=argparse_filestring_type, nargs="*"
6365
default=None, help="Limit the tests to those within the specified comma separated list of paths")
6466

6567
format_choices = ["list", "json"]
6668
format_default_choice = "list"
6769
format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice)
68-
parser.add_argument("-f", "--format", type=argparse_lowercase_type(format_coices,"format"), dest="format",
69-
default=format_default_choice, help=format_help)
70+
parser.add_argument("-f", "--format", dest="format",
71+
type=argparse_lowercase_type(format_choices, "format"),
72+
default=format_default_choice, help=format_help)
7073

7174
parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
7275
default=None, help="Continue trying to build all tests if a build failure occurs")
7376

74-
parser.add_argument("-n", "--names", dest="names",
77+
parser.add_argument("-n", "--names", dest="names", nargs="*"
7578
default=None, help="Limit the tests to a comma separated list of names")
7679

7780
parser.add_argument("--test-spec", dest="test_spec",
@@ -90,7 +93,7 @@
9093

9194
# Filter tests by path if specified
9295
if options.paths:
93-
all_paths = options.paths.split(",")
96+
all_paths = options.paths
9497
else:
9598
all_paths = ["."]
9699

@@ -103,7 +106,7 @@
103106

104107
# Filter tests by name if specified
105108
if options.names:
106-
all_names = options.names.split(",")
109+
all_names = options.names
107110
all_names = [x.lower() for x in all_names]
108111

109112
for name in all_names:

tools/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,7 @@ def parse_type(string):
217217
argparse_lowercase_type = argparse_list_type(str.lower, False)
218218
argparse_uppercase_hyphen_type = argparse_list_type(str.upper, True)
219219
argparse_lowercase_hyphen_type = argparse_list_type(str.lower, True)
220+
221+
def argparse_filestring_type(string) :
222+
if exists(string) : return string
223+
else : raise argparse.ArgumentTypeError("{0} does not exist in the filesystem.".format(string))

0 commit comments

Comments
 (0)