Commits

dirkbaechle committed db1646b

- added first version of a ProvidesTool method

Comments (0)

Files changed (1)

src/engine/SCons/Environment.py

             tool = SCons.Tool.Tool(tool, toolpath, **kw)
         tool(self)
 
+    def ProvidesTool(self, tool, toolpath=None):
+        if SCons.Util.is_String(tool):
+            tool = self.subst(tool)
+            if toolpath is None:
+                toolpath = self.get('toolpath', [])
+            toolpath = list(map(self._find_toolpath_dir, toolpath))
+
+            import imp
+
+            oldpythonpath = sys.path
+            sys.path = toolpath + sys.path
+    
+            try:
+                try:
+                    file, path, desc = imp.find_module(tool, toolpath)
+                except ImportError, e:
+                    return False
+                finally:
+                    if file:
+                        file.close()
+            finally:
+                sys.path = oldpythonpath
+    
+            full_name = 'SCons.Tool.' + tool
+            try:
+                return sys.modules[full_name]
+            except KeyError:
+                try:
+                    smpath = sys.modules['SCons.Tool'].__path__
+                    try:
+                        file, path, desc = imp.find_module(tool, smpath)
+                        if file:
+                            file.close()
+                        return True
+                    except ImportError, e:
+                        if str(e)!="No module named %s"%tool:
+                            raise SCons.Errors.EnvironmentError(e)
+                        try:
+                            import zipimport
+                            importer = zipimport.zipimporter( sys.modules['SCons.Tool'].__path__[0] )
+                            module = importer.load_module(full_name)
+                            setattr(SCons.Tool, self.name, module)
+                            return module
+                        except ImportError, e:
+                            m = "No tool named '%s': %s" % (self.name, e)
+                            raise SCons.Errors.EnvironmentError(m)
+                except ImportError, e:
+                    m = "No tool named '%s': %s" % (self.name, e)
+                    raise SCons.Errors.EnvironmentError(m)
+
+
     def WhereIs(self, prog, path=None, pathext=None, reject=[]):
         """Find prog in the path.
         """