Skip to content

Commit 44030c2

Browse files
committed
Simplify main function
1 parent 71323af commit 44030c2

File tree

1 file changed

+72
-82
lines changed

1 file changed

+72
-82
lines changed

tools/project.py

Lines changed: 72 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,27 @@ def export(target, ide, build=None, src=None, macros=None, project_id=None,
142142
libraries_paths=lib,
143143
zip_proj=zip_name,
144144
build_profile=build_profile,
145-
notify=notify,
145+
notify=TerminalNotifier(),
146146
app_config=app_config,
147147
ignore=ignore
148148
)
149149

150-
151-
def main():
152-
"""Entry point"""
153-
# Parse Options
150+
def clean(source_dir):
151+
if exists(EXPORT_DIR):
152+
rmtree(EXPORT_DIR)
153+
for cls in EXPORTERS.values():
154+
try:
155+
cls.clean(basename(abspath(source_dir[0])))
156+
except (NotImplementedError, IOError, OSError):
157+
pass
158+
for f in list(EXPORTERS.values())[0].CLEAN_FILES:
159+
try:
160+
remove(f)
161+
except (IOError, OSError):
162+
pass
163+
164+
165+
def get_args(argv):
154166
parser = ArgumentParser()
155167

156168
targetnames = TARGET_NAMES
@@ -221,6 +233,13 @@ def main():
221233
help="displays supported matrix of MCUs and IDEs"
222234
)
223235

236+
group.add_argument(
237+
"--update-packs",
238+
dest="update_packs",
239+
action="store_true",
240+
default=False
241+
)
242+
224243
parser.add_argument(
225244
"-E",
226245
action="store_true",
@@ -264,13 +283,6 @@ def main():
264283
default=[]
265284
)
266285

267-
parser.add_argument(
268-
"--update-packs",
269-
dest="update_packs",
270-
action="store_true",
271-
default=False
272-
)
273-
274286
parser.add_argument(
275287
"--app-config",
276288
dest="app_config",
@@ -286,92 +298,70 @@ def main():
286298
"(eg. ./main.cpp)")
287299
)
288300

289-
options = parser.parse_args()
301+
return parser.parse_args(argv), parser
302+
303+
304+
def main():
305+
"""Entry point"""
306+
# Parse Options
307+
options, parser = get_args(sys.argv)
290308

291309
# Print available tests in order and exit
292-
if options.list_tests is True:
310+
if options.list_tests:
293311
print('\n'.join(str(test) for test in sorted(TEST_MAP.values())))
294-
sys.exit()
295-
296-
# Only prints matrix of supported IDEs
297-
if options.supported_ides:
312+
elif options.supported_ides:
298313
if options.supported_ides == "matrix":
299314
print_large_string(mcu_ide_matrix())
300315
elif options.supported_ides == "ides":
301316
print(mcu_ide_list())
302-
exit(0)
303-
304-
# Only prints matrix of supported IDEs
305-
if options.supported_ides_html:
317+
elif options.supported_ides_html:
306318
html = mcu_ide_matrix(verbose_html=True)
307319
with open("./export/README.md", "w") as readme:
308320
readme.write("Exporter IDE/Platform Support\n")
309321
readme.write("-----------------------------------\n")
310322
readme.write("\n")
311323
readme.write(html)
312-
exit(0)
313-
314-
if options.update_packs:
324+
elif options.update_packs:
315325
from tools.arm_pack_manager import Cache
316326
cache = Cache(True, True)
317327
cache.cache_everything()
318-
319-
# Target
320-
if not options.mcu:
321-
args_error(parser, "argument -m/--mcu is required")
322-
323-
# Toolchain
324-
if not options.ide:
325-
args_error(parser, "argument -i is required")
326-
327-
# Clean Export Directory
328-
if options.clean:
329-
if exists(EXPORT_DIR):
330-
rmtree(EXPORT_DIR)
331-
332-
zip_proj = not bool(options.source_dir)
333-
334-
notify = TerminalNotifier()
335-
336-
ide = resolve_exporter_alias(options.ide)
337-
exporter, toolchain_name = get_exporter_toolchain(ide)
338-
profile = extract_profile(parser, options, toolchain_name, fallback="debug")
339-
mcu = extract_mcus(parser, options)[0]
340-
341-
if not exporter.is_target_supported(mcu):
342-
args_error(parser, "%s not supported by %s" % (mcu, ide))
343-
344-
if (options.program is None) and (not options.source_dir):
345-
args_error(parser, "one of -p, -n, or --source is required")
346-
347-
if options.clean:
348-
for cls in EXPORTERS.values():
349-
try:
350-
cls.clean(basename(abspath(options.source_dir[0])))
351-
except (NotImplementedError, IOError, OSError):
352-
pass
353-
for f in list(EXPORTERS.values())[0].CLEAN_FILES:
354-
try:
355-
remove(f)
356-
except (IOError, OSError):
357-
pass
358-
try:
359-
export(
360-
mcu,
361-
ide,
362-
build=options.build,
363-
src=options.source_dir,
364-
macros=options.macros,
365-
project_id=options.program,
366-
zip_proj=zip_proj,
367-
build_profile=profile,
368-
app_config=options.app_config,
369-
export_path=options.build_dir,
370-
notify=notify,
371-
ignore=options.ignore
372-
)
373-
except NotSupportedException as exc:
374-
print("[Not Supported] %s" % str(exc))
328+
else:
329+
# Check required arguments
330+
if not options.mcu:
331+
args_error(parser, "argument -m/--mcu is required")
332+
if not options.ide:
333+
args_error(parser, "argument -i is required")
334+
if (options.program is None) and (not options.source_dir):
335+
args_error(parser, "one of -p, -n, or --source is required")
336+
337+
if options.clean:
338+
clean(options.source_dir)
339+
340+
ide = resolve_exporter_alias(options.ide)
341+
exporter, toolchain_name = get_exporter_toolchain(ide)
342+
profile = extract_profile(parser, options, toolchain_name, fallback="debug")
343+
mcu = extract_mcus(parser, options)[0]
344+
if not exporter.is_target_supported(mcu):
345+
args_error(parser, "%s not supported by %s" % (mcu, ide))
346+
347+
try:
348+
export(
349+
mcu,
350+
ide,
351+
build=options.build,
352+
src=options.source_dir,
353+
macros=options.macros,
354+
project_id=options.program,
355+
zip_proj=not bool(options.source_dir),
356+
build_profile=profile,
357+
app_config=options.app_config,
358+
export_path=options.build_dir,
359+
ignore=options.ignore
360+
)
361+
except NotSupportedException as exc:
362+
args_error(parser, "%s not supported by %s" % (mcu, ide))
363+
print("[Not Supported] %s" % str(exc))
364+
exit(0)
375365

376366
if __name__ == "__main__":
377367
main()

0 commit comments

Comments
 (0)