Package wafadmin :: Package Tools :: Module misc
[hide private]
[frames] | no frames]

Source Code for Module wafadmin.Tools.misc

  1  #!/usr/bin/env python 
  2  # encoding: utf-8 
  3  # Thomas Nagy, 2006 (ita) 
  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   
17 -def copy_func(tsk):
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
30 -def action_process_file_func(tsk):
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
35 -class cmd_taskgen(TaskGen.task_gen):
36 "This object will call a command everytime"
37 - def __init__(self, type='none'):
38 TaskGen.task_gen.__init__(self) 39 self.type = type 40 self.fun = None
41
42 - def apply(self):
43 # create a task 44 if not self.fun: raise Utils.WafError('cmdobj needs a function!') 45 tsk = Task.TaskBase() 46 tsk.fun = self.fun 47 tsk.env = self.env 48 self.tasks.append(tsk) 49 tsk.install_path = self.install_path
50
51 -class copy_taskgen(TaskGen.task_gen):
52 "By default, make a file copy, if fun is provided, fun will make the copy (or call a compiler, etc)"
53 - def __init__(self, type='none'):
54 TaskGen.task_gen.__init__(self) 55 56 self.source = '' 57 self.target = '' 58 self.chmod = '' 59 self.fun = copy_func 60 61 self.env = Build.bld.env.copy()
62
63 - def apply(self):
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 # TODO the file path may be incorrect 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
87 -def subst_func(tsk):
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 # replace all % by %% to prevent errors by % signs in the input file while string formatting 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
119 -class subst_taskgen(TaskGen.task_gen):
120 - def __init__(self, type='none'):
121 TaskGen.task_gen.__init__(self) 122 self.fun = subst_func 123 self.dict = {}
124
125 - def apply(self):
126 127 lst = self.to_list(self.source) 128 129 for filename in lst: 130 node = self.path.find_resource(filename) 131 if not node: raise Utils.WafError('cannot find input file %s for processing' % filename) 132 133 newnode = node.change_ext('') 134 135 if self.dict and not self.env['DICT_HASH']: 136 self.env = self.env.copy() 137 self.env['DICT_HASH'] = hash(str(self.dict)) # <- pretty sure it wont work (ita) 138 139 tsk = self.create_task('copy') 140 tsk.set_inputs(node) 141 tsk.set_outputs(newnode) 142 tsk.fun = self.fun 143 tsk.dict = self.dict 144 tsk.dep_vars = ['DICT_HASH'] 145 tsk.install_path = self.install_path 146 147 if not tsk.env: 148 tsk.debug() 149 raise Utils.WafError('task without an environment')
150 151 #################### 152 ## command-output #### 153 #################### 154
155 -class CmdArg(object):
156 """Represents a command-output argument that is based on input or output files or directories""" 157 pass
158
159 -class CmdFileArg(CmdArg):
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
169 -class CmdInputFileArg(CmdFileArg):
170 - def find_node(self, base_path):
171 assert isinstance(base_path, Node.Node) 172 self.node = base_path.find_resource(self.file_name) 173 if self.node is None: 174 raise Utils.WafError("Input file %s not found in " % (self.file_name, base_path))
175
176 - def get_path(self, env, absolute):
177 if absolute: 178 return self.template % self.node.abspath(env) 179 else: 180 return self.template % self.node.srcpath(env)
181
182 -class CmdOutputFileArg(CmdFileArg):
183 - def find_node(self, base_path):
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))
188 - def get_path(self, env, absolute):
189 if absolute: 190 return self.template % self.node.abspath(env) 191 else: 192 return self.template % self.node.bldpath(env)
193
194 -class CmdDirArg(CmdArg):
195 - def __init__(self, dir_name):
196 CmdArg.__init__(self) 197 self.dir_name = dir_name 198 self.node = None
199 - def find_node(self, base_path):
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
205 -class CmdInputDirArg(CmdDirArg):
206 - def get_path(self, dummy_env, dummy_absolute):
207 return self.node.abspath()
208
209 -class CmdOutputDirArg(CmdFileArg):
210 - def get_path(self, env, dummy_absolute):
211 return self.node.abspath(env)
212 213
214 -class command_output(Task.Task):
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 = [] # additional environment variables to look
228
229 - def run(self):
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