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