118118# verbose logging
119119verbose = False
120120very_verbose = False
121+ install_requirements = True
121122
122123# stores current working directory for recursive operations
123124cwd_root = ""
@@ -1213,15 +1214,44 @@ def _find_file_paths(self, paths, fl):
12131214 return os .path .join (path )
12141215 return None
12151216
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 )
12231254
1224- return env
12251255
12261256 # Routines after cloning mbed-os
12271257 def post_action (self ):
@@ -1239,29 +1269,7 @@ def post_action(self):
12391269 os .path .isfile (os .path .join (mbed_tools_path , 'default_settings.py' ))):
12401270 shutil .copy (os .path .join (mbed_tools_path , 'default_settings.py' ), os .path .join (self .path , 'mbed_settings.py' ))
12411271
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 )
12651273
12661274 def add_tools (self , path ):
12671275 if not os .path .exists (path ):
@@ -1281,9 +1289,19 @@ def add_tools(self, path):
12811289 def get_tools (self ):
12821290 mbed_tools_path = self .get_tools_dir ()
12831291 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 )
12851293 return mbed_tools_path
12861294
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+
12871305 def get_target (self , target = None ):
12881306 target_cfg = self .get_cfg ('TARGET' )
12891307 target = target if target else target_cfg
@@ -1976,6 +1994,7 @@ def compile_(toolchain=None, target=None, options=False, compile_library=False,
19761994 args = remainder
19771995 # Find the root of the program
19781996 program = Program (os .getcwd (), True )
1997+ program .check_requirements ()
19791998 # Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
19801999 orig_path = os .getcwd ()
19812000
@@ -2065,6 +2084,7 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
20652084 args = remainder
20662085 # Find the root of the program
20672086 program = Program (os .getcwd (), True )
2087+ program .check_requirements ()
20682088 # Save original working directory
20692089 orig_path = os .getcwd ()
20702090
@@ -2150,6 +2170,7 @@ def export(ide=None, target=None, source=False, clean=False, supported=False):
21502170 args = remainder
21512171 # Find the root of the program
21522172 program = Program (os .getcwd (), True )
2173+ program .check_requirements ()
21532174 # Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
21542175 orig_path = os .getcwd ()
21552176 # Change directories to the program root to use mbed OS tools
@@ -2193,6 +2214,7 @@ def detect():
21932214 args = remainder
21942215 # Find the root of the program
21952216 program = Program (os .getcwd (), True )
2217+ program .check_requirements ()
21962218 # Change directories to the program root to use mbed OS tools
21972219 with cd (program .path ):
21982220 tools_dir = program .get_tools ()
0 commit comments