From 50171409c48dee540aab13b42857261433af18e4 Mon Sep 17 00:00:00 2001 From: "Morten V. Pedersen" Date: Fri, 7 Oct 2016 21:38:43 +0200 Subject: [PATCH 1/2] Allow using directories as waf tools Sometimes it is useful to be able to add a module to waf as a tool. Using this patch one can use ./waf-light configure build --tools /tmp/mytool This will add the files under /tmp/mytool under /waflib/extras/mytool. Such that they can be imported in a wscript as from waflib.extras import mytool. --- wscript | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/wscript b/wscript index 5c97c588..6bc3407a 100644 --- a/wscript +++ b/wscript @@ -232,11 +232,28 @@ def create_waf(self, *k, **kw): if zipType not in zip_types: zipType = zip_types[0] - + directoryFiles = {} files = [] add3rdparty = [] for x in Options.options.add3rdparty.split(','): - if os.path.isabs(x): + if os.path.isdir(x): + # Create mapping over files in directory to location in + # waflib.extras + root_dir = os.path.basename(os.path.normpath(x)) + + for root, _, file_list in os.walk(x): + for file_name in file_list: + relative_dir = os.path.relpath(root, x) + relative_file = os.path.normpath( + os.path.join(root_dir, relative_dir, file_name)) + + file_from = os.path.abspath(os.path.join(root, file_name)) + file_to = relative_file + + directoryFiles[file_from] = file_to + files.append(file_from) + + elif os.path.isabs(x): files.append(x) else: add3rdparty.append(x + '.py') @@ -283,7 +300,9 @@ def create_waf(self, *k, **kw): (code, size, cnt) = sfilter(x) tarinfo.size = size - if os.path.isabs(x): + if x in directoryFiles: + tarinfo.name = 'waflib/extras/' + directoryFiles[x] + elif os.path.isabs(x): tarinfo.name = 'waflib/extras/' + os.path.split(x)[1] print(' adding %s as %s' % (x, tarinfo.name)) @@ -408,4 +427,3 @@ def build(bld): class Dist(Scripting.Dist): def get_excl(self): return super(self.__class__, self).get_excl() + ' **/waflib.zip' - From 55786e4ac8c7316d9ab5e26ceb811743177f8d25 Mon Sep 17 00:00:00 2001 From: "Morten V. Pedersen" Date: Mon, 10 Oct 2016 13:00:53 +0200 Subject: [PATCH 2/2] Update approach to include module - Using ant_glob(..) for filtering and iterating files - Update location in zip - Also include files in zip --- wscript | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/wscript b/wscript index 4cd3c9b1..de3adfb6 100644 --- a/wscript +++ b/wscript @@ -232,27 +232,32 @@ def create_waf(self, *k, **kw): if zipType not in zip_types: zipType = zip_types[0] - directoryFiles = {} + directory_files = {} files = [] add3rdparty = [] for x in Options.options.add3rdparty.split(','): if os.path.isdir(x): - # Create mapping over files in directory to location in - # waflib.extras - root_dir = os.path.basename(os.path.normpath(x)) - - for root, _, file_list in os.walk(x): - for file_name in file_list: - relative_dir = os.path.relpath(root, x) - relative_file = os.path.normpath( - os.path.join(root_dir, relative_dir, file_name)) - - file_from = os.path.abspath(os.path.join(root, file_name)) - file_to = relative_file - - directoryFiles[file_from] = file_to - files.append(file_from) - + # Create mapping from files absolute path to path in module + # directory (for module mylib): + # + # {"/home/path/mylib/__init__.py": "mylib/__init__.py", + # "/home/path/mylib/lib.py": "mylib/lib.py", + # "/home/path/mylib/sub/sub.py": "mylib/sub/lib.py" + # } + # + x_dir = self.generator.bld.root.find_dir( + os.path.abspath(os.path.expanduser(x))) + + file_list = x_dir.ant_glob('**/*.py') + + for f in file_list: + + file_from = f.abspath() + file_to = os.path.join(x_dir.name, f.path_from(x_dir)) + + directory_files[file_from] = file_to + files.append(file_from) + elif os.path.isabs(x): files.append(x) else: @@ -300,15 +305,17 @@ def create_waf(self, *k, **kw): (code, size, cnt) = sfilter(x) tarinfo.size = size - if x in directoryFiles: - tarinfo.name = 'waflib/extras/' + directoryFiles[x] + if x in directory_files: + tarinfo.name = 'waflib/extras/' + directory_files[x] elif os.path.isabs(x): tarinfo.name = 'waflib/extras/' + os.path.split(x)[1] print(' adding %s as %s' % (x, tarinfo.name)) def dest(x): - if os.path.isabs(x): - return os.path.join('extras', os.path.basename(x)) + if x in directory_files: + return os.path.join('waflib', 'extras', directory_files[x]) + elif os.path.isabs(x): + return os.path.join('waflib', 'extras', os.path.basename(x)) else: return os.path.normpath(os.path.relpath(x, "."))