Skip to content

Commit 3d28850

Browse files
committed
Add Program.get_tools_dir() for unified handling of mbed-os tools directory
1 parent dc0e6f4 commit 3d28850

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

mbed/mbed.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -836,18 +836,29 @@ def get_os_dir(self):
836836
else:
837837
return None
838838

839+
# Gets mbed OS tools dir (unified)
840+
def get_tools_dir(self):
841+
mbed_os_path = self.get_os_dir()
842+
if mbed_os_path and os.path.isdir(os.path.join(mbed_os_path, 'tools')):
843+
return os.path.join(mbed_os_path, 'tools')
844+
else:
845+
return None
846+
839847
# Routines after cloning mbed-os
840848
def post_clone(self):
841849
mbed_os_path = self.get_os_dir()
842850
if not mbed_os_path:
851+
warning("Cannot find the mbed-os directory in \"%s\"" % self.path)
843852
return False
844-
if not os.path.isdir(os.path.join(mbed_os_path, 'tools')):
853+
854+
mbed_tools_path = self.get_tools_dir()
855+
if not mbed_tools_path:
845856
warning("Cannot find the mbed-os tools directory in \"%s\"" % mbed_os_path)
846857
return False
847858

848859
if (not os.path.isfile(os.path.join(self.path, 'mbed_settings.py')) and
849-
os.path.isfile(os.path.join(mbed_os_path, 'tools/default_settings.py'))):
850-
shutil.copy(os.path.join(mbed_os_path, 'tools/default_settings.py'), os.path.join(self.path, 'mbed_settings.py'))
860+
os.path.isfile(os.path.join(mbed_tools_path, 'default_settings.py'))):
861+
shutil.copy(os.path.join(mbed_tools_path, 'default_settings.py'), os.path.join(self.path, 'mbed_settings.py'))
851862

852863

853864
missing = []
@@ -1311,12 +1322,10 @@ def compile(toolchain=None, mcu=None, source=False, build=False, compile_library
13111322
orig_path = os.getcwd()
13121323

13131324
with cd(program.path):
1314-
if os.path.isdir('mbed-os'): # its application with mbed-os sub dir
1315-
mbed_os_path = os.path.abspath('mbed-os')
1316-
elif os.path.basename(os.getcwd()) == 'mbed-os':# its standalone mbed-os (is root)
1317-
mbed_os_path = os.path.abspath('.')
1318-
else:
1319-
error('The mbed-os codebase and tools were not found.', -1)
1325+
mbed_os_path = program.get_os_dir()
1326+
tools_dir = os.path.abspath(program.get_tools_dir())
1327+
if not mbed_os_path or not tools_dir:
1328+
error('The mbed-os codebase or tools were not found in "%s".' % program.path, -1)
13201329

13211330
target = mcu if mcu else program.get_cfg('TARGET')
13221331
if target is None:
@@ -1331,8 +1340,6 @@ def compile(toolchain=None, mcu=None, source=False, build=False, compile_library
13311340
with open('MACROS.txt') as f:
13321341
macros = f.read().splitlines()
13331342

1334-
tools_dir = os.path.abspath(os.path.join(mbed_os_path, 'tools'))
1335-
13361343
env = os.environ.copy()
13371344
env['PYTHONPATH'] = os.path.abspath(program.path)
13381345

@@ -1390,17 +1397,16 @@ def test(tlist=False):
13901397
program = Program(os.getcwd(), True)
13911398
# Change directories to the program root to use mbed OS tools
13921399
with cd(program.path):
1393-
# If "mbed-os" folder doesn't exist, error
1394-
if not os.path.isdir('mbed-os'):
1395-
error('The mbed-os codebase and tools were not found in this program.', -1)
1400+
if not program.get_tools_dir():
1401+
error('The mbed-os codebase or tools were not found in "%s".' % program.path, -1)
13961402

13971403
# Prepare environment variables
13981404
env = os.environ.copy()
13991405
env['PYTHONPATH'] = '.'
14001406
if tlist:
14011407
# List all available tests (by default in a human-readable format)
14021408
try:
1403-
popen(['python', 'mbed-os/tools/test.py', '-l'] + args, env=env)
1409+
popen(['python', os.path.join(program.get_tools_dir(), 'test.py'), '-l'] + args, env=env)
14041410
except ProcessException:
14051411
error('Failed to run test script')
14061412

@@ -1417,8 +1423,8 @@ def export(ide=None, mcu=None):
14171423
program = Program(os.getcwd(), True)
14181424
# Change directories to the program root to use mbed OS tools
14191425
with cd(program.path):
1420-
if not os.path.isdir('mbed-os'):
1421-
error('The mbed-os codebase and tools were not found in this program.', -1)
1426+
if not program.get_tools_dir():
1427+
error('The mbed-os codebase or tools were not found in "%s".' % program.path, -1)
14221428

14231429
target = mcu if mcu else program.get_cfg('TARGET')
14241430
if target is None:
@@ -1431,7 +1437,7 @@ def export(ide=None, mcu=None):
14311437

14321438
env = os.environ.copy()
14331439
env['PYTHONPATH'] = '.'
1434-
popen(['python', 'mbed-os/tools/project.py']
1440+
popen(['python', os.path.join(program.get_tools_dir(), 'project.py')]
14351441
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
14361442
+ ['-i', ide, '-m', target, '--source=%s' % program.path]
14371443
+ args,

0 commit comments

Comments
 (0)