2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-11-26 03:39:53 +01:00

Merge pull request #1595 from nikoder/increase_control_over_tool_loading

Disable regular sys.path when loading tool with specified tooldir.
This commit is contained in:
ita1024 2015-07-28 13:28:34 +02:00
commit bc8840e0df
2 changed files with 37 additions and 25 deletions

View File

@ -234,7 +234,7 @@ class ConfigurationContext(Context.Context):
tmpenv = self.all_envs[key]
tmpenv.store(os.path.join(self.cachedir.abspath(), key + Build.CACHE_SUFFIX))
def load(self, input, tooldir=None, funs=None):
def load(self, input, tooldir=None, funs=None, loadFromSysPath=True):
"""
Load Waf tools, which will be imported whenever a build is started.
@ -260,7 +260,7 @@ class ConfigurationContext(Context.Context):
module = None
try:
module = Context.load_tool(tool, tooldir, ctx=self)
module = Context.load_tool(tool, tooldir, ctx=self, loadFromSysPath=loadFromSysPath)
except ImportError as e:
self.fatal('Could not load the Waf tool %r from %r\n%s' % (tool, sys.path, e))
except Exception as e:

View File

@ -210,9 +210,10 @@ class Context(ctx):
"""
tools = Utils.to_list(tool_list)
path = Utils.to_list(kw.get('tooldir', ''))
loadFromSysPath = kw.get('loadFromSysPath', True)
for t in tools:
module = load_tool(t, path)
module = load_tool(t, path, loadFromSysPath=loadFromSysPath)
fun = getattr(module, kw.get('name', self.fun), None)
if fun:
fun(self)
@ -620,14 +621,14 @@ def load_module(path, encoding=None):
module_dir = os.path.dirname(path)
sys.path.insert(0, module_dir)
exec(compile(code, path, 'exec'), module.__dict__)
sys.path.remove(module_dir)
try : exec(compile(code, path, 'exec'), module.__dict__)
finally: sys.path.remove(module_dir)
cache_modules[path] = module
return module
def load_tool(tool, tooldir=None, ctx=None):
def load_tool(tool, tooldir=None, ctx=None, loadFromSysPath=True):
"""
Import a Waf tool (python module), and store it in the dict :py:const:`waflib.Context.Context.tools`
@ -635,33 +636,44 @@ def load_tool(tool, tooldir=None, ctx=None):
:param tool: Name of the tool
:type tooldir: list
:param tooldir: List of directories to search for the tool module
:type loadFromSysPath: boolean
:param loadFromSysPath: whether or not to search the regular sys.path, besides waf_dir and potentially given tooldirs
"""
if tool == 'java':
tool = 'javaw' # jython
else:
tool = tool.replace('++', 'xx')
if tooldir:
assert isinstance(tooldir, list)
sys.path = tooldir + sys.path
try:
__import__(tool)
origSysPath = sys.path
if not loadFromSysPath: sys.path = []
try:
if tooldir:
assert isinstance(tooldir, list)
sys.path = tooldir + sys.path
try:
__import__(tool)
finally:
for d in tooldir:
sys.path.remove(d)
ret = sys.modules[tool]
Context.tools[tool] = ret
return ret
finally:
for d in tooldir:
sys.path.remove(d)
else:
for x in ('waflib.Tools.%s', 'waflib.extras.%s', 'waflib.%s', '%s'):
else:
if not loadFromSysPath: sys.path.insert(0, waf_dir)
try:
__import__(x % tool)
break
except ImportError:
x = None
if x is None: # raise an exception
__import__(tool)
ret = sys.modules[x % tool]
Context.tools[tool] = ret
return ret
for x in ('waflib.Tools.%s', 'waflib.extras.%s', 'waflib.%s', '%s'):
try:
__import__(x % tool)
break
except ImportError:
x = None
if x is None: # raise an exception
__import__(tool)
finally:
if not loadFromSysPath: sys.path.remove(waf_dir)
ret = sys.modules[x % tool]
Context.tools[tool] = ret
return ret
finally:
if not loadFromSysPath: sys.path += origSysPath