mirror of https://gitlab.com/ita1024/waf.git
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:
commit
bc8840e0df
|
@ -234,7 +234,7 @@ class ConfigurationContext(Context.Context):
|
||||||
tmpenv = self.all_envs[key]
|
tmpenv = self.all_envs[key]
|
||||||
tmpenv.store(os.path.join(self.cachedir.abspath(), key + Build.CACHE_SUFFIX))
|
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.
|
Load Waf tools, which will be imported whenever a build is started.
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ class ConfigurationContext(Context.Context):
|
||||||
|
|
||||||
module = None
|
module = None
|
||||||
try:
|
try:
|
||||||
module = Context.load_tool(tool, tooldir, ctx=self)
|
module = Context.load_tool(tool, tooldir, ctx=self, loadFromSysPath=loadFromSysPath)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
self.fatal('Could not load the Waf tool %r from %r\n%s' % (tool, sys.path, e))
|
self.fatal('Could not load the Waf tool %r from %r\n%s' % (tool, sys.path, e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -210,9 +210,10 @@ class Context(ctx):
|
||||||
"""
|
"""
|
||||||
tools = Utils.to_list(tool_list)
|
tools = Utils.to_list(tool_list)
|
||||||
path = Utils.to_list(kw.get('tooldir', ''))
|
path = Utils.to_list(kw.get('tooldir', ''))
|
||||||
|
loadFromSysPath = kw.get('loadFromSysPath', True)
|
||||||
|
|
||||||
for t in tools:
|
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)
|
fun = getattr(module, kw.get('name', self.fun), None)
|
||||||
if fun:
|
if fun:
|
||||||
fun(self)
|
fun(self)
|
||||||
|
@ -620,14 +621,14 @@ def load_module(path, encoding=None):
|
||||||
module_dir = os.path.dirname(path)
|
module_dir = os.path.dirname(path)
|
||||||
sys.path.insert(0, module_dir)
|
sys.path.insert(0, module_dir)
|
||||||
|
|
||||||
exec(compile(code, path, 'exec'), module.__dict__)
|
try : exec(compile(code, path, 'exec'), module.__dict__)
|
||||||
sys.path.remove(module_dir)
|
finally: sys.path.remove(module_dir)
|
||||||
|
|
||||||
cache_modules[path] = module
|
cache_modules[path] = module
|
||||||
|
|
||||||
return 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`
|
Import a Waf tool (python module), and store it in the dict :py:const:`waflib.Context.Context.tools`
|
||||||
|
|
||||||
|
@ -635,24 +636,31 @@ def load_tool(tool, tooldir=None, ctx=None):
|
||||||
:param tool: Name of the tool
|
:param tool: Name of the tool
|
||||||
:type tooldir: list
|
:type tooldir: list
|
||||||
:param tooldir: List of directories to search for the tool module
|
: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':
|
if tool == 'java':
|
||||||
tool = 'javaw' # jython
|
tool = 'javaw' # jython
|
||||||
else:
|
else:
|
||||||
tool = tool.replace('++', 'xx')
|
tool = tool.replace('++', 'xx')
|
||||||
|
|
||||||
|
origSysPath = sys.path
|
||||||
|
if not loadFromSysPath: sys.path = []
|
||||||
|
try:
|
||||||
if tooldir:
|
if tooldir:
|
||||||
assert isinstance(tooldir, list)
|
assert isinstance(tooldir, list)
|
||||||
sys.path = tooldir + sys.path
|
sys.path = tooldir + sys.path
|
||||||
try:
|
try:
|
||||||
__import__(tool)
|
__import__(tool)
|
||||||
ret = sys.modules[tool]
|
|
||||||
Context.tools[tool] = ret
|
|
||||||
return ret
|
|
||||||
finally:
|
finally:
|
||||||
for d in tooldir:
|
for d in tooldir:
|
||||||
sys.path.remove(d)
|
sys.path.remove(d)
|
||||||
|
ret = sys.modules[tool]
|
||||||
|
Context.tools[tool] = ret
|
||||||
|
return ret
|
||||||
else:
|
else:
|
||||||
|
if not loadFromSysPath: sys.path.insert(0, waf_dir)
|
||||||
|
try:
|
||||||
for x in ('waflib.Tools.%s', 'waflib.extras.%s', 'waflib.%s', '%s'):
|
for x in ('waflib.Tools.%s', 'waflib.extras.%s', 'waflib.%s', '%s'):
|
||||||
try:
|
try:
|
||||||
__import__(x % tool)
|
__import__(x % tool)
|
||||||
|
@ -661,7 +669,11 @@ def load_tool(tool, tooldir=None, ctx=None):
|
||||||
x = None
|
x = None
|
||||||
if x is None: # raise an exception
|
if x is None: # raise an exception
|
||||||
__import__(tool)
|
__import__(tool)
|
||||||
|
finally:
|
||||||
|
if not loadFromSysPath: sys.path.remove(waf_dir)
|
||||||
ret = sys.modules[x % tool]
|
ret = sys.modules[x % tool]
|
||||||
Context.tools[tool] = ret
|
Context.tools[tool] = ret
|
||||||
return ret
|
return ret
|
||||||
|
finally:
|
||||||
|
if not loadFromSysPath: sys.path += origSysPath
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue