From 334b9d54cf6810f55ffb14c2c42a03ac1f2ab433 Mon Sep 17 00:00:00 2001 From: grbd Date: Fri, 4 Aug 2017 02:57:11 -0700 Subject: Added fixes for docs / nested test for use of sys.path in toolpath --- doc/user/environments.xml | 49 ++++++++++++++++++++++------------- test/toolpath/nested/image/SConstruct | 6 ++++- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/doc/user/environments.xml b/doc/user/environments.xml index 4657c05..ae670a8 100644 --- a/doc/user/environments.xml +++ b/doc/user/environments.xml @@ -1781,7 +1781,7 @@ env.AppendENVPath('LIB', '/usr/local/lib') -# Inbuilt tool or tool located within site_tools +# Builtin tool or tool located within site_tools env = Environment(tools = ['SomeTool']) env.SomeTool(targets, sources) @@ -1847,11 +1847,12 @@ SCons/Tool/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. - This is the same constraint used by python when loading modules - from within sub-directories (packages). + For python2 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. + This is the same constraint used by python when loading modules + from within sub-directories (packages). + For python3 this appears to be no longer a requirement. @@ -1859,20 +1860,30 @@ SCons/Tool/SubDir1/SubDir2/SomeTool/__init__.py Using sys.path within the toolpath - Using the toolpath option with sys.path - we can also include tools installed via the pip package manager. - + If we want to access tools externally to scons on the sys.path + (one example would be tools installed via the pip package manager) + One way to do this is to use sys.path with the toolpath. + + One thing to watch out for with this approach is that sys.path + can sometimes contains paths to .egg files instead of directories. + So we need to filter those out with this approach. + # namespaced target using sys.path within toolpath -env = Environment(tools = ['someinstalledpackage.SomeTool'], toolpath = sys.path) + +searchpaths = [] +for item in sys.path: + if os.path.isdir(item): searchpaths.append(item) + +env = Environment(tools = ['someinstalledpackage.SomeTool'], toolpath = searchpaths) env.SomeTool(targets, sources) - By supplying sys.path to the toolpath argument - and by using the nested syntax we can have scons search - the sys.path (which will include packages installed via pip). + By using sys.path with the toolpath argument + and by using the nested syntax we can have scons search + packages installed via pip for Tools. @@ -1893,19 +1904,21 @@ C:\Python35\Lib\site-packages\someinstalledpackage\SomeTool\__init__.py In some cases you may want to use a tool located within a installed external pip package. - This is possible by the use of sys.path within the toolpath. - However in that situaion you need to provide a prefix to the toolname + This is possible by the use of sys.path with the toolpath. + However in that situation you need to provide a prefix to the toolname to indicate where it is located within sys.path -# namespaced target using sys.path -env = Environment(tools = ['tools_example.subdir1.subdir2.SomeTool'], toolpath = sys.path) +searchpaths = [] +for item in sys.path: + if os.path.isdir(item): searchpaths.append(item) +env = Environment(tools = ['tools_example.subdir1.subdir2.SomeTool'], toolpath = searchpaths) env.SomeTool(targets, sources) - To avoid the use of a prefix within the name of the tool, + To avoid the use of a prefix within the name of the tool or filtering sys.path for directories, we can use the PyPackageDir(modulename) function to locate the directory of the python package. PyPackageDir returns a Dir object which represents the path of the directory for the python package / module specified as a parameter. diff --git a/test/toolpath/nested/image/SConstruct b/test/toolpath/nested/image/SConstruct index 78ae21d..a7c6ceb 100644 --- a/test/toolpath/nested/image/SConstruct +++ b/test/toolpath/nested/image/SConstruct @@ -39,6 +39,10 @@ dir_path = Dir('.').srcnode().abspath dir_path = os.path.join(dir_path, 'Libs') sys.path.append(dir_path) +searchpaths = [] +for item in sys.path: + if os.path.isdir(item): searchpaths.append(item) + toollist = ['tools_example.Toolpath_TestTool1', 'tools_example.Toolpath_TestTool2', 'tools_example.subdir1.Toolpath_TestTool1_1', @@ -47,7 +51,7 @@ toollist = ['tools_example.Toolpath_TestTool1', 'tools_example.subdir1.subdir2.Toolpath_TestTool2_2', ] -env3 = Environment(tools=toollist, toolpath=sys.path) +env3 = Environment(tools=toollist, toolpath=searchpaths) print("env3['Toolpath_TestTool1'] =", env3.get('Toolpath_TestTool1')) print("env3['Toolpath_TestTool2'] =", env3.get('Toolpath_TestTool2')) print("env3['Toolpath_TestTool1_1'] =", env3.get('Toolpath_TestTool1_1')) -- cgit v0.12