118
118
# verbose logging
119
119
verbose = False
120
120
very_verbose = False
121
+ install_requirements = True
121
122
122
123
# stores current working directory for recursive operations
123
124
cwd_root = ""
@@ -1213,15 +1214,44 @@ def _find_file_paths(self, paths, fl):
1213
1214
return os .path .join (path )
1214
1215
return None
1215
1216
1216
- def get_env (self ):
1217
- env = os .environ .copy ()
1218
- env ['PYTHONPATH' ] = os .path .abspath (self .path )
1219
- compilers = ['ARM' , 'GCC_ARM' , 'IAR' ]
1220
- for c in compilers :
1221
- if self .get_cfg (c + '_PATH' ):
1222
- env ['MBED_' + c + '_PATH' ] = self .get_cfg (c + '_PATH' )
1217
+ def check_requirements (self , show_warning = False ):
1218
+ req_path = self .get_requirements () or self .path
1219
+ req_file = 'requirements.txt'
1220
+ missing = []
1221
+ try :
1222
+ with open (os .path .join (req_path , req_file ), 'r' ) as f :
1223
+ import pip
1224
+ installed_packages = [package .project_name .lower () for package in pip .get_installed_distributions (local_only = True )]
1225
+ for line in f .read ().splitlines ():
1226
+ pkg = re .sub (r'^([\w-]+).*$' , r'\1' , line ).lower ()
1227
+ if not pkg in installed_packages :
1228
+ missing .append (pkg )
1229
+
1230
+ if missing and install_requirements :
1231
+ try :
1232
+ action ("Auto-installing missing Python modules..." )
1233
+ pquery (['pip' , 'install' , '-q' , '-r' , os .path .join (req_path , req_file )])
1234
+ missing = []
1235
+ except ProcessException :
1236
+ warning ("Unable to auto-install required Python modules." )
1237
+ pass
1238
+
1239
+ except (IOError , ImportError , OSError ):
1240
+ pass
1241
+
1242
+ if missing :
1243
+ err = (
1244
+ "-----------------------------------------------------------------\n "
1245
+ "The mbed OS tools in this program require the following Python modules: %s\n "
1246
+ "You can install all missing modules by running \" pip install -r %s\" in \" %s\" " % (', ' .join (missing ), req_file , req_path ))
1247
+ if os .name == 'posix' :
1248
+ err += "\n On Posix systems (Linux, Mac, etc) you might have to switch to superuser account or use \" sudo\" "
1249
+
1250
+ if show_warning :
1251
+ warning (err )
1252
+ else :
1253
+ error (err , 1 )
1223
1254
1224
- return env
1225
1255
1226
1256
# Routines after cloning mbed-os
1227
1257
def post_action (self ):
@@ -1239,29 +1269,7 @@ def post_action(self):
1239
1269
os .path .isfile (os .path .join (mbed_tools_path , 'default_settings.py' ))):
1240
1270
shutil .copy (os .path .join (mbed_tools_path , 'default_settings.py' ), os .path .join (self .path , 'mbed_settings.py' ))
1241
1271
1242
- req_path = self .get_requirements () or self .path
1243
- req_file = 'requirements.txt'
1244
- missing = []
1245
- try :
1246
- with open (os .path .join (os .path .join (req_path , req_file )), 'r' ) as f :
1247
- import pip
1248
- installed_packages = [package .project_name .lower () for package in pip .get_installed_distributions ()]
1249
- for line in f .read ().splitlines ():
1250
- pkg = re .sub (r'^([\w-]+).*$' , r'\1' , line ).lower ()
1251
- if not pkg in installed_packages :
1252
- missing .append (pkg )
1253
- except IOError :
1254
- pass
1255
- except ImportError :
1256
- pass
1257
-
1258
- if len (missing ):
1259
- warning (
1260
- "-----------------------------------------------------------------\n "
1261
- "The mbed build tools in this program require Python modules that are not installed.\n "
1262
- "This might prevent compiling code or exporting to IDEs and other toolchains.\n "
1263
- "The missing Python modules are: %s\n "
1264
- "You can install all missing modules by running \" pip install -r %s\" in \" %s\" " % (', ' .join (missing ), req_file , req_path ))
1272
+ self .check_requirements (True )
1265
1273
1266
1274
def add_tools (self , path ):
1267
1275
if not os .path .exists (path ):
@@ -1281,9 +1289,19 @@ def add_tools(self, path):
1281
1289
def get_tools (self ):
1282
1290
mbed_tools_path = self .get_tools_dir ()
1283
1291
if not mbed_tools_path :
1284
- error ('The mbed tools were not found in "%s". \n Run `mbed deploy` to install dependencies and tools. ' % self .path , - 1 )
1292
+ error ('The mbed tools were not found in "%s". \n Run `mbed deploy` to install dependencies and tools. ' % self .path , - 1 )
1285
1293
return mbed_tools_path
1286
1294
1295
+ def get_env (self ):
1296
+ env = os .environ .copy ()
1297
+ env ['PYTHONPATH' ] = os .path .abspath (self .path )
1298
+ compilers = ['ARM' , 'GCC_ARM' , 'IAR' ]
1299
+ for c in compilers :
1300
+ if self .get_cfg (c + '_PATH' ):
1301
+ env ['MBED_' + c + '_PATH' ] = self .get_cfg (c + '_PATH' )
1302
+
1303
+ return env
1304
+
1287
1305
def get_target (self , target = None ):
1288
1306
target_cfg = self .get_cfg ('TARGET' )
1289
1307
target = target if target else target_cfg
@@ -1976,6 +1994,7 @@ def compile_(toolchain=None, target=None, options=False, compile_library=False,
1976
1994
args = remainder
1977
1995
# Find the root of the program
1978
1996
program = Program (os .getcwd (), True )
1997
+ program .check_requirements ()
1979
1998
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
1980
1999
orig_path = os .getcwd ()
1981
2000
@@ -2065,6 +2084,7 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
2065
2084
args = remainder
2066
2085
# Find the root of the program
2067
2086
program = Program (os .getcwd (), True )
2087
+ program .check_requirements ()
2068
2088
# Save original working directory
2069
2089
orig_path = os .getcwd ()
2070
2090
@@ -2150,6 +2170,7 @@ def export(ide=None, target=None, source=False, clean=False, supported=False):
2150
2170
args = remainder
2151
2171
# Find the root of the program
2152
2172
program = Program (os .getcwd (), True )
2173
+ program .check_requirements ()
2153
2174
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
2154
2175
orig_path = os .getcwd ()
2155
2176
# Change directories to the program root to use mbed OS tools
@@ -2193,6 +2214,7 @@ def detect():
2193
2214
args = remainder
2194
2215
# Find the root of the program
2195
2216
program = Program (os .getcwd (), True )
2217
+ program .check_requirements ()
2196
2218
# Change directories to the program root to use mbed OS tools
2197
2219
with cd (program .path ):
2198
2220
tools_dir = program .get_tools ()
0 commit comments