Skip to content

Commit 18868ff

Browse files
committed
Convert project.py to the new style of argument parsing
1 parent c5ac2cf commit 18868ff

File tree

1 file changed

+41
-70
lines changed

1 file changed

+41
-70
lines changed

tools/project.py

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,97 @@
44
sys.path.insert(0, ROOT)
55

66
from shutil import move, rmtree
7-
from optparse import OptionParser
7+
from argparse import ArgumentParser
88
from os import path
99

1010
from tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP
1111
from tools.paths import MBED_BASE, MBED_LIBRARIES
1212
from tools.export import export, setup_user_prj, EXPORTERS, mcu_ide_matrix
1313
from tools.utils import args_error, mkdir
1414
from tools.tests import TESTS, Test, TEST_MAP
15+
from tools.tests import test_known, test_name_known
1516
from tools.targets import TARGET_NAMES
1617
from tools.libraries import LIBRARIES
18+
from utils import argparse_lowercase_type, argparse_uppercase_type, argparse_filestring_type, argparse_many
1719

18-
try:
19-
import tools.private_settings as ps
20-
except:
21-
ps = object()
2220

2321

2422
if __name__ == '__main__':
2523
# Parse Options
26-
parser = OptionParser()
24+
parser = ArgumentParser()
2725

2826
targetnames = TARGET_NAMES
2927
targetnames.sort()
3028
toolchainlist = EXPORTERS.keys()
3129
toolchainlist.sort()
3230

33-
parser.add_option("-m", "--mcu",
31+
parser.add_argument("-m", "--mcu",
3432
metavar="MCU",
3533
default='LPC1768',
34+
required=True,
35+
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")),
3636
help="generate project for the given MCU (%s)"% ', '.join(targetnames))
3737

38-
parser.add_option("-i",
38+
parser.add_argument("-i",
3939
dest="ide",
4040
default='uvision',
41+
required=True,
42+
type=argparse_many(argparse_lowercase_type(toolchainlist, "toolchain")),
4143
help="The target IDE: %s"% str(toolchainlist))
4244

43-
parser.add_option("-c", "--clean",
45+
parser.add_argument("-c", "--clean",
4446
action="store_true",
4547
default=False,
4648
help="clean the export directory")
4749

48-
parser.add_option("-p",
49-
type="int",
50+
group = parser.add_mutually_exclusive_group(required=True)
51+
group.add_argument("-p",
52+
type=test_known,
5053
dest="program",
5154
help="The index of the desired test program: [0-%d]"% (len(TESTS)-1))
5255

53-
parser.add_option("-n",
54-
dest="program_name",
56+
group.add_argument("-n",
57+
type=test_name_known,
58+
dest="program",
5559
help="The name of the desired test program")
5660

57-
parser.add_option("-b",
61+
parser.add_argument("-b",
5862
dest="build",
5963
action="store_true",
6064
default=False,
6165
help="use the mbed library build, instead of the sources")
6266

63-
parser.add_option("-L", "--list-tests",
67+
parser.add_argument("-L", "--list-tests",
6468
action="store_true",
6569
dest="list_tests",
6670
default=False,
6771
help="list available programs in order and exit")
6872

69-
parser.add_option("-S", "--list-matrix",
73+
parser.add_argument("-S", "--list-matrix",
7074
action="store_true",
7175
dest="supported_ides",
7276
default=False,
7377
help="displays supported matrix of MCUs and IDEs")
7478

75-
parser.add_option("-E",
79+
parser.add_argument("-E",
7680
action="store_true",
7781
dest="supported_ides_html",
7882
default=False,
7983
help="writes tools/export/README.md")
8084

81-
parser.add_option("--source",
82-
action="append",
85+
parser.add_argument("--source",
86+
nargs="*",
87+
type=argparse_filestring_type,
8388
dest="source_dir",
84-
default=None,
89+
default=[],
8590
help="The source (input) directory")
8691

87-
parser.add_option("-D", "",
92+
parser.add_argument("-D",
8893
action="append",
8994
dest="macros",
9095
help="Add a macro definition")
9196

92-
(options, args) = parser.parse_args()
97+
options = parser.parse_args()
9398

9499
# Print available tests in order and exit
95100
if options.list_tests is True:
@@ -122,16 +127,6 @@
122127
if exists(EXPORT_DIR):
123128
rmtree(EXPORT_DIR)
124129

125-
# Target
126-
if options.mcu is None :
127-
args_error(parser, "[ERROR] You should specify an MCU")
128-
mcus = options.mcu
129-
130-
# IDE
131-
if options.ide is None:
132-
args_error(parser, "[ERROR] You should specify an IDE")
133-
ide = options.ide
134-
135130
# Export results
136131
successes = []
137132
failures = []
@@ -141,14 +136,14 @@
141136
# source_dir = use relative paths, otherwise sources are copied
142137
sources_relative = True if options.source_dir else False
143138

144-
for mcu in mcus.split(','):
139+
for mcu in options.mcu:
145140
# Program Number or name
146-
p, n, src, ide = options.program, options.program_name, options.source_dir, options.ide
141+
p, src, ides = options.program, options.source_dir, options.ide
147142

148-
if src is not None:
143+
if src:
149144
# --source is used to generate IDE files to toolchain directly in the source tree and doesn't generate zip file
150145
project_dir = options.source_dir
151-
project_name = n if n else "Unnamed_Project"
146+
project_name = TESTS[p]
152147
project_temp = path.join(options.source_dir[0], 'projectfiles', ide)
153148
mkdir(project_temp)
154149
lib_symbols = []
@@ -157,31 +152,6 @@
157152
zip = False # don't create zip
158153
clean = False # don't cleanup because we use the actual source tree to generate IDE files
159154
else:
160-
if n is not None and p is not None:
161-
args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
162-
if n:
163-
if not n in TEST_MAP.keys():
164-
# Check if there is an alias for this in private_settings.py
165-
if getattr(ps, "test_alias", None) is not None:
166-
alias = ps.test_alias.get(n, "")
167-
if not alias in TEST_MAP.keys():
168-
args_error(parser, "[ERROR] Program with name '%s' not found" % n)
169-
else:
170-
n = alias
171-
else:
172-
args_error(parser, "[ERROR] Program with name '%s' not found" % n)
173-
p = TEST_MAP[n].n
174-
175-
if p is None or (p < 0) or (p > (len(TESTS)-1)):
176-
message = "[ERROR] You have to specify one of the following tests:\n"
177-
message += '\n'.join(map(str, sorted(TEST_MAP.values())))
178-
args_error(parser, message)
179-
180-
# Project
181-
if p is None or (p < 0) or (p > (len(TESTS)-1)):
182-
message = "[ERROR] You have to specify one of the following tests:\n"
183-
message += '\n'.join(map(str, sorted(TEST_MAP.values())))
184-
args_error(parser, message)
185155
test = Test(p)
186156

187157
# Some libraries have extra macros (called by exporter symbols) to we need to pass
@@ -210,16 +180,17 @@
210180
setup_user_prj(project_dir[0], test.source_dir, test.dependencies)
211181

212182
# Export to selected toolchain
213-
tmp_path, report = export(project_dir, project_name, ide, mcu, project_dir[0], project_temp, clean=clean, zip=zip, extra_symbols=lib_symbols, relative=sources_relative)
214-
if report['success']:
215-
if not zip:
216-
zip_path = join(project_temp, "%s_%s" % (project_name, mcu))
183+
for ide in ides:
184+
tmp_path, report = export(project_dir, project_name, ide, mcu, project_dir[0], project_temp, clean=clean, zip=zip, extra_symbols=lib_symbols, relative=sources_relative)
185+
if report['success']:
186+
if not zip:
187+
zip_path = join(project_temp, "%s_%s" % (project_name, mcu))
188+
else:
189+
zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (project_name, ide, mcu))
190+
move(tmp_path, zip_path)
191+
successes.append("%s::%s\t%s"% (mcu, ide, zip_path))
217192
else:
218-
zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (project_name, ide, mcu))
219-
move(tmp_path, zip_path)
220-
successes.append("%s::%s\t%s"% (mcu, ide, zip_path))
221-
else:
222-
failures.append("%s::%s\t%s"% (mcu, ide, report['errormsg']))
193+
failures.append("%s::%s\t%s"% (mcu, ide, report['errormsg']))
223194

224195
# Prints export results
225196
print

0 commit comments

Comments
 (0)