third_party:waf: update to upstream 2.0.4 release
[vlendec/samba-autobuild/.git] / third_party / waf / waflib / extras / buildcopy.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #! /usr/bin/env python
6 # encoding: utf-8
7 # Calle Rosenquist, 2017 (xbreak)
8 """
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
11 without installation.
12
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.
15
16 Examples::
17
18         def build(bld):
19                 bld(name             = 'bar',
20                         features         = 'py buildcopy',
21                         source           = bld.path.ant_glob('src/bar/*.py'))
22
23                 bld(name             = 'py baz',
24                         features         = 'buildcopy',
25                         buildcopy_source = bld.path.ant_glob('src/bar/*.py') + ['src/bar/resource.txt'])
26
27 """
28 import os, shutil
29 from waflib import Errors, Task, TaskGen, Utils, Node
30
31 @TaskGen.before_method('process_source')
32 @TaskGen.feature('buildcopy')
33 def make_buildcopy(self):
34         """
35         Creates the buildcopy task.
36         """
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.
40                 """
41                 if isinstance(lst, Node.Node):
42                         if not lst.is_src():
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)
46                         return lst
47
48                 if isinstance(lst, str):
49                         lst = [x for x in Utils.split_path(lst) if x and x != '.']
50
51                 node = self.bld.path.get_src().search_node(lst)
52                 if node:
53                         if not os.path.isfile(node.abspath()):
54                                 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
55                         return node
56
57                 node = self.bld.path.get_src().find_node(lst)
58                 if node:
59                         if not os.path.isfile(node.abspath()):
60                                 raise Errors.WafError('buildcopy: Cannot copy directory %s (unsupported action)'%node)
61                         return node
62                 raise Errors.WafError('buildcopy: File not found in src: %s'%os.path.join(*lst))
63
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)
67
68
69 class buildcopy(Task.Task):
70         """
71         Copy for each pair `n` in `node_pairs`: n[0] -> n[1].
72
73         Attribute `node_pairs` should contain a list of tuples describing source and target:
74
75                 node_pairs = [(in, out), ...]
76
77         """
78         color = 'PINK'
79
80         def keyword(self):
81                 return 'Copying'
82
83         def run(self):
84                 for f,t in self.node_pairs:
85                         t.parent.mkdir()
86                         shutil.copy2(f.abspath(), t.abspath())