|
4 | 4 | sys.path.insert(0, ROOT)
|
5 | 5 |
|
6 | 6 | from shutil import move, rmtree
|
7 |
| -from optparse import OptionParser |
| 7 | +from argparse import ArgumentParser |
8 | 8 | from os import path
|
9 | 9 |
|
10 | 10 | from tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP
|
11 | 11 | from tools.paths import MBED_BASE, MBED_LIBRARIES
|
12 | 12 | from tools.export import export, setup_user_prj, EXPORTERS, mcu_ide_matrix
|
13 | 13 | from tools.utils import args_error, mkdir
|
14 | 14 | from tools.tests import TESTS, Test, TEST_MAP
|
| 15 | +from tools.tests import test_known, test_name_known |
15 | 16 | from tools.targets import TARGET_NAMES
|
16 | 17 | from tools.libraries import LIBRARIES
|
| 18 | +from utils import argparse_lowercase_type, argparse_uppercase_type, argparse_filestring_type, argparse_many |
17 | 19 |
|
18 |
| -try: |
19 |
| - import tools.private_settings as ps |
20 |
| -except: |
21 |
| - ps = object() |
22 | 20 |
|
23 | 21 |
|
24 | 22 | if __name__ == '__main__':
|
25 | 23 | # Parse Options
|
26 |
| - parser = OptionParser() |
| 24 | + parser = ArgumentParser() |
27 | 25 |
|
28 | 26 | targetnames = TARGET_NAMES
|
29 | 27 | targetnames.sort()
|
30 | 28 | toolchainlist = EXPORTERS.keys()
|
31 | 29 | toolchainlist.sort()
|
32 | 30 |
|
33 |
| - parser.add_option("-m", "--mcu", |
| 31 | + parser.add_argument("-m", "--mcu", |
34 | 32 | metavar="MCU",
|
35 | 33 | default='LPC1768',
|
| 34 | + required=True, |
| 35 | + type=argparse_many(argparse_uppercase_type(targetnames, "MCU")), |
36 | 36 | help="generate project for the given MCU (%s)"% ', '.join(targetnames))
|
37 | 37 |
|
38 |
| - parser.add_option("-i", |
| 38 | + parser.add_argument("-i", |
39 | 39 | dest="ide",
|
40 | 40 | default='uvision',
|
| 41 | + required=True, |
| 42 | + type=argparse_many(argparse_lowercase_type(toolchainlist, "toolchain")), |
41 | 43 | help="The target IDE: %s"% str(toolchainlist))
|
42 | 44 |
|
43 |
| - parser.add_option("-c", "--clean", |
| 45 | + parser.add_argument("-c", "--clean", |
44 | 46 | action="store_true",
|
45 | 47 | default=False,
|
46 | 48 | help="clean the export directory")
|
47 | 49 |
|
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, |
50 | 53 | dest="program",
|
51 | 54 | help="The index of the desired test program: [0-%d]"% (len(TESTS)-1))
|
52 | 55 |
|
53 |
| - parser.add_option("-n", |
54 |
| - dest="program_name", |
| 56 | + group.add_argument("-n", |
| 57 | + type=test_name_known, |
| 58 | + dest="program", |
55 | 59 | help="The name of the desired test program")
|
56 | 60 |
|
57 |
| - parser.add_option("-b", |
| 61 | + parser.add_argument("-b", |
58 | 62 | dest="build",
|
59 | 63 | action="store_true",
|
60 | 64 | default=False,
|
61 | 65 | help="use the mbed library build, instead of the sources")
|
62 | 66 |
|
63 |
| - parser.add_option("-L", "--list-tests", |
| 67 | + parser.add_argument("-L", "--list-tests", |
64 | 68 | action="store_true",
|
65 | 69 | dest="list_tests",
|
66 | 70 | default=False,
|
67 | 71 | help="list available programs in order and exit")
|
68 | 72 |
|
69 |
| - parser.add_option("-S", "--list-matrix", |
| 73 | + parser.add_argument("-S", "--list-matrix", |
70 | 74 | action="store_true",
|
71 | 75 | dest="supported_ides",
|
72 | 76 | default=False,
|
73 | 77 | help="displays supported matrix of MCUs and IDEs")
|
74 | 78 |
|
75 |
| - parser.add_option("-E", |
| 79 | + parser.add_argument("-E", |
76 | 80 | action="store_true",
|
77 | 81 | dest="supported_ides_html",
|
78 | 82 | default=False,
|
79 | 83 | help="writes tools/export/README.md")
|
80 | 84 |
|
81 |
| - parser.add_option("--source", |
82 |
| - action="append", |
| 85 | + parser.add_argument("--source", |
| 86 | + nargs="*", |
| 87 | + type=argparse_filestring_type, |
83 | 88 | dest="source_dir",
|
84 |
| - default=None, |
| 89 | + default=[], |
85 | 90 | help="The source (input) directory")
|
86 | 91 |
|
87 |
| - parser.add_option("-D", "", |
| 92 | + parser.add_argument("-D", |
88 | 93 | action="append",
|
89 | 94 | dest="macros",
|
90 | 95 | help="Add a macro definition")
|
91 | 96 |
|
92 |
| - (options, args) = parser.parse_args() |
| 97 | + options = parser.parse_args() |
93 | 98 |
|
94 | 99 | # Print available tests in order and exit
|
95 | 100 | if options.list_tests is True:
|
|
122 | 127 | if exists(EXPORT_DIR):
|
123 | 128 | rmtree(EXPORT_DIR)
|
124 | 129 |
|
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 |
| - |
135 | 130 | # Export results
|
136 | 131 | successes = []
|
137 | 132 | failures = []
|
|
141 | 136 | # source_dir = use relative paths, otherwise sources are copied
|
142 | 137 | sources_relative = True if options.source_dir else False
|
143 | 138 |
|
144 |
| - for mcu in mcus.split(','): |
| 139 | + for mcu in options.mcu: |
145 | 140 | # 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 |
147 | 142 |
|
148 |
| - if src is not None: |
| 143 | + if src: |
149 | 144 | # --source is used to generate IDE files to toolchain directly in the source tree and doesn't generate zip file
|
150 | 145 | project_dir = options.source_dir
|
151 |
| - project_name = n if n else "Unnamed_Project" |
| 146 | + project_name = TESTS[p] |
152 | 147 | project_temp = path.join(options.source_dir[0], 'projectfiles', ide)
|
153 | 148 | mkdir(project_temp)
|
154 | 149 | lib_symbols = []
|
|
157 | 152 | zip = False # don't create zip
|
158 | 153 | clean = False # don't cleanup because we use the actual source tree to generate IDE files
|
159 | 154 | 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) |
185 | 155 | test = Test(p)
|
186 | 156 |
|
187 | 157 | # Some libraries have extra macros (called by exporter symbols) to we need to pass
|
|
210 | 180 | setup_user_prj(project_dir[0], test.source_dir, test.dependencies)
|
211 | 181 |
|
212 | 182 | # 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)) |
217 | 192 | 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'])) |
223 | 194 |
|
224 | 195 | # Prints export results
|
225 | 196 | print
|
|
0 commit comments