3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Calle Rosenquist, 2017 (xbreak)
9 Create task that copies source files to the associated build node.
10 This is useful to e.g. construct a complete Python package so it can be unit tested
13 Source files to be copied can be specified either in `buildcopy_source` attribute, or
14 `source` attribute. If both are specified `buildcopy_source` has priority.
20 features = 'py buildcopy',
21 source = bld.path.ant_glob('src/bar/*.py'))
24 features = 'buildcopy',
25 buildcopy_source = bld.path.ant_glob('src/bar/*.py') + ['src/bar/resource.txt'])
29 from waflib import Errors, Task, TaskGen, Utils, Node
31 @TaskGen.before_method('process_source')
32 @TaskGen.feature('buildcopy')
33 def make_buildcopy(self):
35 Creates the buildcopy task.
37 def to_src_nodes(lst):
38 """Find file nodes only in src, TaskGen.to_nodes will not work for this since it gives
39 preference to nodes in build.
41 if isinstance(lst, Node.Node):
43 raise Errors.WafError('buildcopy: node %s is not in src'%lst)
44 if not os.path.isfile(lst.abspath()):
45 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%lst)
48 if isinstance(lst, str):
49 lst = [x for x in Utils.split_path(lst) if x and x != '.']
51 node = self.bld.path.get_src().search_node(lst)
53 if not os.path.isfile(node.abspath()):
54 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
57 node = self.bld.path.get_src().find_node(lst)
59 if not os.path.isfile(node.abspath()):
60 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
62 raise Errors.WafError('buildcopy: File not found in src: %s'%os.path.join(*lst))
64 nodes = [ to_src_nodes(n) for n in getattr(self, 'buildcopy_source', getattr(self, 'source', [])) ]
65 node_pairs = [(n, n.get_bld()) for n in nodes]
66 self.create_task('buildcopy', [n[0] for n in node_pairs], [n[1] for n in node_pairs], node_pairs=node_pairs)
69 class buildcopy(Task.Task):
71 Copy for each pair `n` in `node_pairs`: n[0] -> n[1].
73 Attribute `node_pairs` should contain a list of tuples describing source and target:
75 node_pairs = [(in, out), ...]
84 for f,t in self.node_pairs:
86 shutil.copy2(f.abspath(), t.abspath())