Skip to content

Commit fd9dc7b

Browse files
committed
Unify how tools dir, target and macros are handled for commands compile, export and test
1 parent d660d10 commit fd9dc7b

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

mbed/mbed.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Default paths to Mercurial and Git
2020
hg_cmd = 'hg'
2121
git_cmd = 'git'
22-
ver = '0.4.0'
22+
ver = '0.4.1'
2323

2424
ignores = [
2525
# Version control folders
@@ -1146,7 +1146,7 @@ def post_action(self):
11461146
if not mbed_tools_path:
11471147
if not os.path.exists(os.path.join(self.path, '.temp')):
11481148
os.mkdir(os.path.join(self.path, '.temp'))
1149-
self.get_tools(os.path.join(self.path, '.temp'))
1149+
self.add_tools(os.path.join(self.path, '.temp'))
11501150
mbed_tools_path = self.get_tools_dir()
11511151

11521152
if not mbed_tools_path:
@@ -1182,7 +1182,7 @@ def post_action(self):
11821182
"The missing Python modules are: %s\n"
11831183
"You can install all missing modules by opening a command prompt in \"%s\" and running \"pip install -r %s\"" % (', '.join(missing), mbed_os_path, fname))
11841184

1185-
def get_tools(self, path):
1185+
def add_tools(self, path):
11861186
with cd(path):
11871187
tools_dir = 'tools'
11881188
if not os.path.exists(tools_dir):
@@ -1194,6 +1194,31 @@ def get_tools(self, path):
11941194
rmtree_readonly(tools_dir)
11951195
raise Exception(128, "An error occurred while cloning the mbed SDK tools from \"%s\"" % mbed_sdk_tools_url)
11961196

1197+
def get_tools(self):
1198+
mbed_tools_path = self.get_tools_dir()
1199+
if not mbed_tools_path:
1200+
error('The mbed_tools_pathd in "%s". \n Run `mbed deploy` to install dependencies and tools. ' % self.path, -1)
1201+
return mbed_tools_path
1202+
1203+
def get_mcu(self, mcu=None):
1204+
target = mcu if mcu else self.get_cfg('TARGET')
1205+
if target is None:
1206+
error('Please specify compile target using the -m switch or set default target using command "target"', 1)
1207+
return target
1208+
1209+
def get_toolchain(self, toolchain=None):
1210+
tchain = toolchain if toolchain else program.get_cfg('TOOLCHAIN')
1211+
if tchain is None:
1212+
error('Please specify compile toolchain using the -t switch or set default toolchain using command "toolchain"', 1)
1213+
return tchain
1214+
1215+
def get_macros(self):
1216+
macros = []
1217+
if os.path.isfile('MACROS.txt'):
1218+
with open('MACROS.txt') as f:
1219+
macros = f.read().splitlines()
1220+
return macros
1221+
11971222

11981223
def formaturl(url, format="default"):
11991224
url = "%s" % url
@@ -1716,23 +1741,10 @@ def compile(toolchain=None, mcu=None, source=False, build=False, compile_library
17161741
orig_path = os.getcwd()
17171742

17181743
with cd(program.path):
1719-
mbed_tools_path = program.get_tools_dir()
1720-
if not mbed_tools_path:
1721-
error('The mbed tools were not found in "%s". \n Run `mbed deploy` to install dependencies and tools. ' % program.path, -1)
1722-
tools_dir = os.path.abspath(mbed_tools_path)
1723-
1724-
target = mcu if mcu else program.get_cfg('TARGET')
1725-
if target is None:
1726-
error('Please specify compile target using the -m switch or set default target using command "target"', 1)
1727-
1728-
tchain = toolchain if toolchain else program.get_cfg('TOOLCHAIN')
1729-
if tchain is None:
1730-
error('Please specify compile toolchain using the -t switch or set default toolchain using command "toolchain"', 1)
1731-
1732-
macros = []
1733-
if os.path.isfile('MACROS.txt'):
1734-
with open('MACROS.txt') as f:
1735-
macros = f.read().splitlines()
1744+
tools_dir = os.path.abspath(program.get_tools())
1745+
target = program.get_mcu(mcu)
1746+
tchain = program.get_toolchain(toolchain)
1747+
macros = program.get_macros()
17361748

17371749
env = os.environ.copy()
17381750
env['PYTHONPATH'] = os.path.abspath(program.path)
@@ -1793,16 +1805,15 @@ def test(tlist=False):
17931805
program = Program(os.getcwd(), True)
17941806
# Change directories to the program root to use mbed OS tools
17951807
with cd(program.path):
1796-
if not program.get_tools_dir():
1797-
error('The mbed tools were not found in "%s".' % program.path, -1)
1808+
tools_dir = program.get_tools()
17981809

17991810
# Prepare environment variables
18001811
env = os.environ.copy()
18011812
env['PYTHONPATH'] = '.'
18021813
if tlist:
18031814
# List all available tests (by default in a human-readable format)
18041815
try:
1805-
popen(['python', os.path.join(program.get_tools_dir(), 'test.py'), '-l'] + args, env=env)
1816+
popen(['python', os.path.join(tools_dir, 'test.py'), '-l'] + args, env=env)
18061817
except ProcessException:
18071818
error('Failed to run test script')
18081819

@@ -1819,21 +1830,13 @@ def export(ide=None, mcu=None):
18191830
program = Program(os.getcwd(), True)
18201831
# Change directories to the program root to use mbed OS tools
18211832
with cd(program.path):
1822-
if not program.get_tools_dir():
1823-
error('The mbed tools were not found in "%s".' % program.path, -1)
1824-
1825-
target = mcu if mcu else program.get_cfg('TARGET')
1826-
if target is None:
1827-
error('Please specify export target using the -m switch or set default target using command "target"', 1)
1828-
1829-
macros = []
1830-
if os.path.isfile('MACROS.txt'):
1831-
with open('MACROS.txt') as f:
1832-
macros = f.read().splitlines()
1833+
tools_dir = program.get_tools()
1834+
target = program.get_mcu(mcu)
1835+
macros = program.get_macros()
18331836

18341837
env = os.environ.copy()
18351838
env['PYTHONPATH'] = '.'
1836-
popen(['python', os.path.join(program.get_tools_dir(), 'project.py')]
1839+
popen(['python', os.path.join(tools_dir, 'project.py')]
18371840
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
18381841
+ ['-i', ide, '-m', target, '--source=%s' % program.path]
18391842
+ args,

0 commit comments

Comments
 (0)