3 # Calle Rosenquist, 2017 (xbreak)
5 Create task that copies source files to the associated build node.
6 This is useful to e.g. construct a complete Python package so it can be unit tested
9 Source files to be copied can be specified either in `buildcopy_source` attribute, or
10 `source` attribute. If both are specified `buildcopy_source` has priority.
16 features = 'py buildcopy',
17 source = bld.path.ant_glob('src/bar/*.py'))
20 features = 'buildcopy',
21 buildcopy_source = bld.path.ant_glob('src/bar/*.py') + ['src/bar/resource.txt'])
25 from waflib import Errors, Task, TaskGen, Utils, Node
27 @TaskGen.before_method('process_source')
28 @TaskGen.feature('buildcopy')
29 def make_buildcopy(self):
31 Creates the buildcopy task.
33 def to_src_nodes(lst):
34 """Find file nodes only in src, TaskGen.to_nodes will not work for this since it gives
35 preference to nodes in build.
37 if isinstance(lst, Node.Node):
39 raise Errors.WafError('buildcopy: node %s is not in src'%lst)
40 if not os.path.isfile(lst.abspath()):
41 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%lst)
44 if isinstance(lst, str):
45 lst = [x for x in Utils.split_path(lst) if x and x != '.']
47 node = self.bld.path.get_src().search_node(lst)
49 if not os.path.isfile(node.abspath()):
50 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
53 node = self.bld.path.get_src().find_node(lst)
55 if not os.path.isfile(node.abspath()):
56 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
58 raise Errors.WafError('buildcopy: File not found in src: %s'%os.path.join(*lst))
60 nodes = [ to_src_nodes(n) for n in getattr(self, 'buildcopy_source', getattr(self, 'source', [])) ]
61 node_pairs = [(n, n.get_bld()) for n in nodes]
62 self.create_task('buildcopy', [n[0] for n in node_pairs], [n[1] for n in node_pairs], node_pairs=node_pairs)
65 class buildcopy(Task.Task):
67 Copy for each pair `n` in `node_pairs`: n[0] -> n[1].
69 Attribute `node_pairs` should contain a list of tuples describing source and target:
71 node_pairs = [(in, out), ...]
80 for f,t in self.node_pairs:
82 shutil.copy2(f.abspath(), t.abspath())