summaryrefslogtreecommitdiffstats
path: root/extras/buildcopy.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-11-24 13:44:02 +0100
committerDavid Robillard <d@drobilla.net>2018-11-24 13:44:02 +0100
commitd74de7f575a9ec49f96138c3c5251f28946c0c0e (patch)
treed9b620bfba1e7462df4ddb3f6225cc5216c0ca81 /extras/buildcopy.py
parentc6e874c2cc1c5c99a3859112e1bba0f07bcdb8ba (diff)
downloadganv-d74de7f575a9ec49f96138c3c5251f28946c0c0e.tar.gz
ganv-d74de7f575a9ec49f96138c3c5251f28946c0c0e.tar.bz2
ganv-d74de7f575a9ec49f96138c3c5251f28946c0c0e.zip
Squashed 'waflib/' changes from 6e726eb..5ea8f99
5ea8f99 Improve test output spacing 0e23b29 Raise exception when test suite fails to ensure non-zero exit status d6de073 Show run time of unit tests 5b65554 Add short configure option for ultra-strict flags 4687ba6 Use gtest-like test output 258903d Fix failure count in test group summaries da07e73 Fix verbose tests with Python 3 git-subtree-dir: waflib git-subtree-split: 5ea8f99f6e1246079c1fe6bb590c38a53aadd40d
Diffstat (limited to 'extras/buildcopy.py')
-rw-r--r--extras/buildcopy.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/extras/buildcopy.py b/extras/buildcopy.py
new file mode 100644
index 0000000..a6d9ac8
--- /dev/null
+++ b/extras/buildcopy.py
@@ -0,0 +1,82 @@
+#! /usr/bin/env python
+# encoding: utf-8
+# Calle Rosenquist, 2017 (xbreak)
+"""
+Create task that copies source files to the associated build node.
+This is useful to e.g. construct a complete Python package so it can be unit tested
+without installation.
+
+Source files to be copied can be specified either in `buildcopy_source` attribute, or
+`source` attribute. If both are specified `buildcopy_source` has priority.
+
+Examples::
+
+ def build(bld):
+ bld(name = 'bar',
+ features = 'py buildcopy',
+ source = bld.path.ant_glob('src/bar/*.py'))
+
+ bld(name = 'py baz',
+ features = 'buildcopy',
+ buildcopy_source = bld.path.ant_glob('src/bar/*.py') + ['src/bar/resource.txt'])
+
+"""
+import os, shutil
+from waflib import Errors, Task, TaskGen, Utils, Node
+
+@TaskGen.before_method('process_source')
+@TaskGen.feature('buildcopy')
+def make_buildcopy(self):
+ """
+ Creates the buildcopy task.
+ """
+ def to_src_nodes(lst):
+ """Find file nodes only in src, TaskGen.to_nodes will not work for this since it gives
+ preference to nodes in build.
+ """
+ if isinstance(lst, Node.Node):
+ if not lst.is_src():
+ raise Errors.WafError('buildcopy: node %s is not in src'%lst)
+ if not os.path.isfile(lst.abspath()):
+ raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%lst)
+ return lst
+
+ if isinstance(lst, str):
+ lst = [x for x in Utils.split_path(lst) if x and x != '.']
+
+ node = self.bld.path.get_src().search_node(lst)
+ if node:
+ if not os.path.isfile(node.abspath()):
+ raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
+ return node
+
+ node = self.bld.path.get_src().find_node(lst)
+ if node:
+ if not os.path.isfile(node.abspath()):
+ raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
+ return node
+ raise Errors.WafError('buildcopy: File not found in src: %s'%os.path.join(*lst))
+
+ nodes = [ to_src_nodes(n) for n in getattr(self, 'buildcopy_source', getattr(self, 'source', [])) ]
+ node_pairs = [(n, n.get_bld()) for n in nodes]
+ self.create_task('buildcopy', [n[0] for n in node_pairs], [n[1] for n in node_pairs], node_pairs=node_pairs)
+
+
+class buildcopy(Task.Task):
+ """
+ Copy for each pair `n` in `node_pairs`: n[0] -> n[1].
+
+ Attribute `node_pairs` should contain a list of tuples describing source and target:
+
+ node_pairs = [(in, out), ...]
+
+ """
+ color = 'PINK'
+
+ def keyword(self):
+ return 'Copying'
+
+ def run(self):
+ for f,t in self.node_pairs:
+ t.parent.mkdir()
+ shutil.copy2(f.abspath(), t.abspath())