Convert project.py to the new style of argument parsing

pull/1976/head
Jimmy Brisson 2016-06-21 14:00:32 -05:00
parent c5ac2cfdec
commit 18868ff3fb
1 changed files with 41 additions and 70 deletions

View File

@ -4,7 +4,7 @@ ROOT = abspath(join(dirname(__file__), ".."))
sys.path.insert(0, ROOT) sys.path.insert(0, ROOT)
from shutil import move, rmtree from shutil import move, rmtree
from optparse import OptionParser from argparse import ArgumentParser
from os import path from os import path
from tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP from tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP
@ -12,84 +12,89 @@ from tools.paths import MBED_BASE, MBED_LIBRARIES
from tools.export import export, setup_user_prj, EXPORTERS, mcu_ide_matrix from tools.export import export, setup_user_prj, EXPORTERS, mcu_ide_matrix
from tools.utils import args_error, mkdir from tools.utils import args_error, mkdir
from tools.tests import TESTS, Test, TEST_MAP from tools.tests import TESTS, Test, TEST_MAP
from tools.tests import test_known, test_name_known
from tools.targets import TARGET_NAMES from tools.targets import TARGET_NAMES
from tools.libraries import LIBRARIES from tools.libraries import LIBRARIES
from utils import argparse_lowercase_type, argparse_uppercase_type, argparse_filestring_type, argparse_many
try:
import tools.private_settings as ps
except:
ps = object()
if __name__ == '__main__': if __name__ == '__main__':
# Parse Options # Parse Options
parser = OptionParser() parser = ArgumentParser()
targetnames = TARGET_NAMES targetnames = TARGET_NAMES
targetnames.sort() targetnames.sort()
toolchainlist = EXPORTERS.keys() toolchainlist = EXPORTERS.keys()
toolchainlist.sort() toolchainlist.sort()
parser.add_option("-m", "--mcu", parser.add_argument("-m", "--mcu",
metavar="MCU", metavar="MCU",
default='LPC1768', default='LPC1768',
required=True,
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")),
help="generate project for the given MCU (%s)"% ', '.join(targetnames)) help="generate project for the given MCU (%s)"% ', '.join(targetnames))
parser.add_option("-i", parser.add_argument("-i",
dest="ide", dest="ide",
default='uvision', default='uvision',
required=True,
type=argparse_many(argparse_lowercase_type(toolchainlist, "toolchain")),
help="The target IDE: %s"% str(toolchainlist)) help="The target IDE: %s"% str(toolchainlist))
parser.add_option("-c", "--clean", parser.add_argument("-c", "--clean",
action="store_true", action="store_true",
default=False, default=False,
help="clean the export directory") help="clean the export directory")
parser.add_option("-p", group = parser.add_mutually_exclusive_group(required=True)
type="int", group.add_argument("-p",
type=test_known,
dest="program", dest="program",
help="The index of the desired test program: [0-%d]"% (len(TESTS)-1)) help="The index of the desired test program: [0-%d]"% (len(TESTS)-1))
parser.add_option("-n", group.add_argument("-n",
dest="program_name", type=test_name_known,
dest="program",
help="The name of the desired test program") help="The name of the desired test program")
parser.add_option("-b", parser.add_argument("-b",
dest="build", dest="build",
action="store_true", action="store_true",
default=False, default=False,
help="use the mbed library build, instead of the sources") help="use the mbed library build, instead of the sources")
parser.add_option("-L", "--list-tests", parser.add_argument("-L", "--list-tests",
action="store_true", action="store_true",
dest="list_tests", dest="list_tests",
default=False, default=False,
help="list available programs in order and exit") help="list available programs in order and exit")
parser.add_option("-S", "--list-matrix", parser.add_argument("-S", "--list-matrix",
action="store_true", action="store_true",
dest="supported_ides", dest="supported_ides",
default=False, default=False,
help="displays supported matrix of MCUs and IDEs") help="displays supported matrix of MCUs and IDEs")
parser.add_option("-E", parser.add_argument("-E",
action="store_true", action="store_true",
dest="supported_ides_html", dest="supported_ides_html",
default=False, default=False,
help="writes tools/export/README.md") help="writes tools/export/README.md")
parser.add_option("--source", parser.add_argument("--source",
action="append", nargs="*",
type=argparse_filestring_type,
dest="source_dir", dest="source_dir",
default=None, default=[],
help="The source (input) directory") help="The source (input) directory")
parser.add_option("-D", "", parser.add_argument("-D",
action="append", action="append",
dest="macros", dest="macros",
help="Add a macro definition") help="Add a macro definition")
(options, args) = parser.parse_args() options = parser.parse_args()
# Print available tests in order and exit # Print available tests in order and exit
if options.list_tests is True: if options.list_tests is True:
@ -122,16 +127,6 @@ if __name__ == '__main__':
if exists(EXPORT_DIR): if exists(EXPORT_DIR):
rmtree(EXPORT_DIR) rmtree(EXPORT_DIR)
# Target
if options.mcu is None :
args_error(parser, "[ERROR] You should specify an MCU")
mcus = options.mcu
# IDE
if options.ide is None:
args_error(parser, "[ERROR] You should specify an IDE")
ide = options.ide
# Export results # Export results
successes = [] successes = []
failures = [] failures = []
@ -141,14 +136,14 @@ if __name__ == '__main__':
# source_dir = use relative paths, otherwise sources are copied # source_dir = use relative paths, otherwise sources are copied
sources_relative = True if options.source_dir else False sources_relative = True if options.source_dir else False
for mcu in mcus.split(','): for mcu in options.mcu:
# Program Number or name # Program Number or name
p, n, src, ide = options.program, options.program_name, options.source_dir, options.ide p, src, ides = options.program, options.source_dir, options.ide
if src is not None: if src:
# --source is used to generate IDE files to toolchain directly in the source tree and doesn't generate zip file # --source is used to generate IDE files to toolchain directly in the source tree and doesn't generate zip file
project_dir = options.source_dir project_dir = options.source_dir
project_name = n if n else "Unnamed_Project" project_name = TESTS[p]
project_temp = path.join(options.source_dir[0], 'projectfiles', ide) project_temp = path.join(options.source_dir[0], 'projectfiles', ide)
mkdir(project_temp) mkdir(project_temp)
lib_symbols = [] lib_symbols = []
@ -157,31 +152,6 @@ if __name__ == '__main__':
zip = False # don't create zip zip = False # don't create zip
clean = False # don't cleanup because we use the actual source tree to generate IDE files clean = False # don't cleanup because we use the actual source tree to generate IDE files
else: else:
if n is not None and p is not None:
args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
if n:
if not n in TEST_MAP.keys():
# Check if there is an alias for this in private_settings.py
if getattr(ps, "test_alias", None) is not None:
alias = ps.test_alias.get(n, "")
if not alias in TEST_MAP.keys():
args_error(parser, "[ERROR] Program with name '%s' not found" % n)
else:
n = alias
else:
args_error(parser, "[ERROR] Program with name '%s' not found" % n)
p = TEST_MAP[n].n
if p is None or (p < 0) or (p > (len(TESTS)-1)):
message = "[ERROR] You have to specify one of the following tests:\n"
message += '\n'.join(map(str, sorted(TEST_MAP.values())))
args_error(parser, message)
# Project
if p is None or (p < 0) or (p > (len(TESTS)-1)):
message = "[ERROR] You have to specify one of the following tests:\n"
message += '\n'.join(map(str, sorted(TEST_MAP.values())))
args_error(parser, message)
test = Test(p) test = Test(p)
# Some libraries have extra macros (called by exporter symbols) to we need to pass # Some libraries have extra macros (called by exporter symbols) to we need to pass
@ -210,16 +180,17 @@ if __name__ == '__main__':
setup_user_prj(project_dir[0], test.source_dir, test.dependencies) setup_user_prj(project_dir[0], test.source_dir, test.dependencies)
# Export to selected toolchain # Export to selected toolchain
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) for ide in ides:
if report['success']: 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)
if not zip: if report['success']:
zip_path = join(project_temp, "%s_%s" % (project_name, mcu)) if not zip:
zip_path = join(project_temp, "%s_%s" % (project_name, mcu))
else:
zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (project_name, ide, mcu))
move(tmp_path, zip_path)
successes.append("%s::%s\t%s"% (mcu, ide, zip_path))
else: else:
zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (project_name, ide, mcu)) failures.append("%s::%s\t%s"% (mcu, ide, report['errormsg']))
move(tmp_path, zip_path)
successes.append("%s::%s\t%s"% (mcu, ide, zip_path))
else:
failures.append("%s::%s\t%s"% (mcu, ide, report['errormsg']))
# Prints export results # Prints export results
print print