1
2
3
4
5 """
6 Custom objects:
7 - execute a function everytime
8 - copy a file somewhere else
9 """
10
11 import shutil, re, os, types
12
13 import TaskGen, Node, Task, Utils, Build
14 import pproc
15 from Logs import debug
16
18 "Make a file copy. This might be used to make other kinds of file processing (even calling a compiler is possible)"
19 env = tsk.env
20 infile = tsk.inputs[0].abspath(env)
21 outfile = tsk.outputs[0].abspath(env)
22 try:
23 shutil.copy2(infile, outfile)
24 except OSError, IOError:
25 return 1
26 else:
27 if tsk.chmod: os.chmod(outfile, tsk.chmod)
28 return 0
29
31 "Ask the function attached to the task to process it"
32 if not tsk.fun: raise Utils.WafError('task must have a function attached to it for copy_func to work!')
33 return tsk.fun(tsk)
34
36 "This object will call a command everytime"
41
50
52 "By default, make a file copy, if fun is provided, fun will make the copy (or call a compiler, etc)"
62
64
65 lst = self.to_list(self.source)
66
67 for filename in lst:
68 node = self.path.find_resource(filename)
69 if not node: raise Utils.WafError('cannot find input file %s for processing' % filename)
70
71 target = self.target
72 if not target or len(lst)>1: target = node.name
73
74
75 newnode = self.path.find_or_declare(target)
76
77 tsk = self.create_task('copy')
78 tsk.set_inputs(node)
79 tsk.set_outputs(newnode)
80 tsk.fun = self.fun
81 tsk.chmod = self.chmod
82
83 if not tsk.env:
84 tsk.debug()
85 raise Utils.WafError('task without an environment')
86
88 "Substitutes variables in a .in file"
89
90 m4_re = re.compile('@(\w+)@', re.M)
91
92 env = tsk.env
93 infile = tsk.inputs[0].abspath(env)
94 outfile = tsk.outputs[0].abspath(env)
95
96 file = open(infile, 'r')
97 code = file.read()
98 file.close()
99
100
101 code = code.replace('%', '%%')
102
103 s = m4_re.sub(r'%(\1)s', code)
104
105 dict = tsk.dict
106 if not dict:
107 names = m4_re.findall(code)
108 for i in names:
109 if env[i] and type(env[i]) is types.ListType :
110 dict[i] = " ".join(env[i])
111 else: dict[i] = env[i]
112
113 file = open(outfile, 'w')
114 file.write(s % dict)
115 file.close()
116
117 return 0
118
150
151
152
153
154
156 """Represents a command-output argument that is based on input or output files or directories"""
157 pass
158
160 - def __init__(self, file_name, template=None):
161 CmdArg.__init__(self)
162 self.file_name = file_name
163 if template is None:
164 self.template = '%s'
165 else:
166 self.template = template
167 self.node = None
168
181
184 assert isinstance(base_path, Node.Node)
185 self.node = base_path.find_or_declare(self.file_name)
186 if self.node is None:
187 raise Utils.WafError("Output file %s not found in " % (self.file_name, base_path))
189 if absolute:
190 return self.template % self.node.abspath(env)
191 else:
192 return self.template % self.node.bldpath(env)
193
196 CmdArg.__init__(self)
197 self.dir_name = dir_name
198 self.node = None
200 assert isinstance(base_path, Node.Node)
201 self.node = base_path.find_dir(self.dir_name)
202 if self.node is None:
203 raise Utils.WafError("Directory %s not found in " % (self.dir_name, base_path))
204
208
210 - def get_path(self, env, dummy_absolute):
211 return self.node.abspath(env)
212
213
215 m_color = "BLUE"
216 - def __init__(self, env, command, command_node, command_args, stdin, stdout, cwd, os_env):
217 Task.Task.__init__(self, env, normal=1)
218 assert isinstance(command, (str, Node.Node))
219 self.command = command
220 self.command_args = command_args
221 self.stdin = stdin
222 self.stdout = stdout
223 self.cwd = cwd
224 self.os_env = os_env
225
226 if command_node is not None: self.dep_nodes = [command_node]
227 self.dep_vars = []
228
230 task = self
231 assert len(task.inputs) > 0
232
233 def input_path(node, template):
234 if task.cwd is None:
235 return template % node.bldpath(task.env)
236 else:
237 return template % node.abspath()
238 def output_path(node, template):
239 fun = node.abspath
240 if task.cwd is None: fun = node.bldpath
241 return template % fun(task.env)
242
243 if isinstance(task.command, Node.Node):
244 argv = [input_path(task.command, '%s')]
245 else:
246 argv = [task.command]
247
248 for arg in task.command_args:
249 if isinstance(arg, str):
250 argv.append(arg)
251 else:
252 assert isinstance(arg, CmdArg)
253 argv.append(arg.get_path(task.env, (task.cwd is not None)))
254
255 if task.stdin:
256 stdin = file(input_path(task.stdin, '%s'))
257 else:
258 stdin = None
259
260 if task