From 23433f501b055457cc82ed8536af48cd08ebf219 Mon Sep 17 00:00:00 2001 From: grbd Date: Tue, 13 Jun 2017 14:23:08 +0100 Subject: Added support for nested tools --- src/engine/SCons/Tool/__init__.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/engine/SCons/Tool/__init__.py b/src/engine/SCons/Tool/__init__.py index 61b7788..e15c2f3 100644 --- a/src/engine/SCons/Tool/__init__.py +++ b/src/engine/SCons/Tool/__init__.py @@ -118,6 +118,16 @@ class Tool(object): if hasattr(module, 'options'): self.options = module.options + def _load_dotted_module(self, short_name, full_name, searchpaths=None): + splitname = short_name.split('.') + index = 0 + srchpths = searchpaths + for item in splitname: + file, path, desc = imp.find_module(item, srchpths) + mod = imp.load_module(full_name, file, path, desc) + srchpths = [path] + return mod, file + def _tool_module(self): oldpythonpath = sys.path sys.path = self.toolpath + sys.path @@ -127,10 +137,10 @@ class Tool(object): # Py 2 code try: try: - file, path, desc = imp.find_module(self.name, self.toolpath) + file = None try: - return imp.load_module(self.name, file, path, desc) - + mod, file = self._load_dotted_module(self.name, self.name, self.toolpath) + return mod finally: if file: file.close() @@ -231,8 +241,7 @@ class Tool(object): try: smpath = sys.modules['SCons.Tool'].__path__ try: - file, path, desc = imp.find_module(self.name, smpath) - module = imp.load_module(full_name, file, path, desc) + module, file = self._load_dotted_module(self.name, full_name, smpath) setattr(SCons.Tool, self.name, module) if file: file.close() -- cgit v0.12 From e38f013a4006c6734d57057258b96ce1367554f0 Mon Sep 17 00:00:00 2001 From: grbd Date: Wed, 14 Jun 2017 02:08:48 +0100 Subject: Fix the loading of tools where the tool is a package instead of a module --- src/engine/SCons/Tool/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/engine/SCons/Tool/__init__.py b/src/engine/SCons/Tool/__init__.py index e15c2f3..7eac0e0 100644 --- a/src/engine/SCons/Tool/__init__.py +++ b/src/engine/SCons/Tool/__init__.py @@ -189,6 +189,7 @@ class Tool(object): if debug: print("file_Path:%s FOUND"%file_path) break elif os.path.isdir(file_package): + file_package = os.path.join(file_package, '__init__.py') spec = importlib.util.spec_from_file_location(self.name, file_package) if debug: print("PACKAGE:%s Found"%file_package) break -- cgit v0.12 From a96ff49dee3669b25888e138916b6bb06c6dc363 Mon Sep 17 00:00:00 2001 From: grbd Date: Wed, 14 Jun 2017 02:26:58 +0100 Subject: Nested Tool support under python 3 --- src/engine/SCons/Tool/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/engine/SCons/Tool/__init__.py b/src/engine/SCons/Tool/__init__.py index 7eac0e0..876b1d2 100644 --- a/src/engine/SCons/Tool/__init__.py +++ b/src/engine/SCons/Tool/__init__.py @@ -118,7 +118,7 @@ class Tool(object): if hasattr(module, 'options'): self.options = module.options - def _load_dotted_module(self, short_name, full_name, searchpaths=None): + def _load_dotted_module_py2(self, short_name, full_name, searchpaths=None): splitname = short_name.split('.') index = 0 srchpths = searchpaths @@ -139,7 +139,7 @@ class Tool(object): try: file = None try: - mod, file = self._load_dotted_module(self.name, self.name, self.toolpath) + mod, file = self._load_dotted_module_py2(self.name, self.name, self.toolpath) return mod finally: if file: @@ -179,8 +179,9 @@ class Tool(object): found_name = self.name add_to_scons_tools_namespace = False for path in self.toolpath: - file_path = os.path.join(path, "%s.py"%self.name) - file_package = os.path.join(path, self.name) + sepname = self.name.replace('.', os.path.sep) + file_path = os.path.join(path, "%s.py"%sepname) + file_package = os.path.join(path, sepname) if debug: sys.stderr.write("Trying:%s %s\n"%(file_path, file_package)) @@ -242,7 +243,7 @@ class Tool(object): try: smpath = sys.modules['SCons.Tool'].__path__ try: - module, file = self._load_dotted_module(self.name, full_name, smpath) + module, file = self._load_dotted_module_py2(self.name, full_name, smpath) setattr(SCons.Tool, self.name, module) if file: file.close() -- cgit v0.12 From 87649e791c9241c2bf6149a16d780d525016afed Mon Sep 17 00:00:00 2001 From: grbd Date: Wed, 14 Jun 2017 19:07:44 +0100 Subject: Additional fix for nested tools under python2 --- src/engine/SCons/Tool/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/engine/SCons/Tool/__init__.py b/src/engine/SCons/Tool/__init__.py index 876b1d2..e5b4b05 100644 --- a/src/engine/SCons/Tool/__init__.py +++ b/src/engine/SCons/Tool/__init__.py @@ -145,7 +145,8 @@ class Tool(object): if file: file.close() except ImportError as e: - if str(e)!="No module named %s"%self.name: + splitname = self.name.split('.') + if str(e)!="No module named %s"%splitname[0]: raise SCons.Errors.EnvironmentError(e) try: import zipimport -- cgit v0.12 From a25367b4f6151a7888b9446e45e234ed84c9a163 Mon Sep 17 00:00:00 2001 From: grbd Date: Mon, 19 Jun 2017 15:45:03 +0100 Subject: Updated the documentation for nested tools located within subdirs --- doc/man/scons.xml | 16 +++++++++++++++ doc/user/builders-writing.xml | 47 +++++++++++++++++++++++++++++++++++++++++++ src/CHANGES.txt | 4 ++++ 3 files changed, 67 insertions(+) diff --git a/doc/man/scons.xml b/doc/man/scons.xml index b68f27a..3268860 100644 --- a/doc/man/scons.xml +++ b/doc/man/scons.xml @@ -2187,6 +2187,22 @@ platform name when the Environment is constructed. Changing the PATH variable after the Environment is constructed will not cause the tools to be redetected. + One feature now present within Scons is the ability to have nested tools. +Tools which can be located within a subdirectory in the toolpath. +With a nested tool name the dot represents a directory seperator + + +# namespaced builder +env = Environment(ENV = os.environ, tools = ['SubDir1.SubDir2.SomeTool']) +env.SomeTool(targets, sources) + +# Search Paths +# SCons\Tool\SubDir1\SubDir2\SomeTool.py +# SCons\Tool\SubDir1\SubDir2\SomeTool\__init__.py +# .\site_scons\site_tools\SubDir1\SubDir2\SomeTool.py +# .\site_scons\site_tools\SubDir1\SubDir2\SomeTool\__init__.py + + SCons supports the following tool specifications out of the box: diff --git a/doc/user/builders-writing.xml b/doc/user/builders-writing.xml index 07f2dec..35dd989 100644 --- a/doc/user/builders-writing.xml +++ b/doc/user/builders-writing.xml @@ -880,6 +880,53 @@ env2.Foo('file2') +
+ Nested and namespace builders; + + + &SCons; now supports the ability for a Builder to be located within a sub-directory of the toolpath. + This is similar to namespacing within python. + + Normally when loading a tool into the environment, scons will search for the tool within two locations + + + +# Regular non namespace target +env = Environment(ENV = os.environ, tools = ['SomeTool']) +env.SomeTool(targets, sources) + + + + The locations would include + SCons\Tool\SomeTool.py + SCons\Tool\SomeTool\__init__.py + .\site_scons\site_tools\SomeTool.py + .\site_scons\site_tools\SomeTool\__init__.py + + If a toolpath is specified this is also searched as well. + With nested or namespaced tools we can use the dot notation to specify a sub-directoty that the tool is located under + + + +# namespaced target +env = Environment(ENV = os.environ, tools = ['SubDir1.SubDir2.SomeTool']) +env.SomeTool(targets, sources) + + + + With this example the search locations would include + SCons\Tool\SubDir1\SubDir2\SomeTool.py + SCons\Tool\SubDir1\SubDir2\SomeTool\__init__.py + .\site_scons\site_tools\SubDir1\SubDir2\SomeTool.py + .\site_scons\site_tools\SubDir1\SubDir2\SomeTool\__init__.py + + It's important to note when creating tools within sub-directories, there needs to be a __init__.py file within each directory. + This file can just be empty however. + This is the same constraint used by python when loading modules from within sub-directories (packages). + + +
+