~ubuntu-branches/ubuntu/raring/xmms2/raring

« back to all changes in this revision

Viewing changes to waflib/extras/misc.py

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-11-25 19:23:15 UTC
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: package-import@ubuntu.com-20121125192315-m9z6nu9wwlzrrz9z
ImportĀ upstreamĀ versionĀ 0.8+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# encoding: utf-8
 
3
# WARNING! Do not edit! http://waf.googlecode.com/svn/docs/wafbook/single.html#_obtaining_the_waf_file
 
4
 
 
5
import shutil,re,os
 
6
from waflib import TaskGen,Node,Task,Utils,Build,Errors
 
7
from waflib.TaskGen import feature,after_method,before_method
 
8
from waflib.Logs import debug
 
9
def copy_attrs(orig,dest,names,only_if_set=False):
 
10
        for a in Utils.to_list(names):
 
11
                u=getattr(orig,a,())
 
12
                if u or not only_if_set:
 
13
                        setattr(dest,a,u)
 
14
def copy_func(tsk):
 
15
        env=tsk.env
 
16
        infile=tsk.inputs[0].abspath()
 
17
        outfile=tsk.outputs[0].abspath()
 
18
        try:
 
19
                shutil.copy2(infile,outfile)
 
20
        except(OSError,IOError):
 
21
                return 1
 
22
        else:
 
23
                if tsk.chmod:os.chmod(outfile,tsk.chmod)
 
24
                return 0
 
25
def action_process_file_func(tsk):
 
26
        if not tsk.fun:raise Errors.WafError('task must have a function attached to it for copy_func to work!')
 
27
        return tsk.fun(tsk)
 
28
def apply_cmd(self):
 
29
        if not self.fun:raise Errors.WafError('cmdobj needs a function!')
 
30
        tsk=Task.TaskBase()
 
31
        tsk.fun=self.fun
 
32
        tsk.env=self.env
 
33
        self.tasks.append(tsk)
 
34
        tsk.install_path=self.install_path
 
35
def apply_copy(self):
 
36
        Utils.def_attrs(self,fun=copy_func)
 
37
        self.default_install_path=0
 
38
        lst=self.to_list(self.source)
 
39
        self.meths.remove('process_source')
 
40
        for filename in lst:
 
41
                node=self.path.find_resource(filename)
 
42
                if not node:raise Errors.WafError('cannot find input file %s for processing'%filename)
 
43
                target=self.target
 
44
                if not target or len(lst)>1:target=node.name
 
45
                newnode=self.path.find_or_declare(target)
 
46
                tsk=self.create_task('copy',node,newnode)
 
47
                tsk.fun=self.fun
 
48
                tsk.chmod=getattr(self,'chmod',Utils.O644)
 
49
                if not tsk.env:
 
50
                        tsk.debug()
 
51
                        raise Errors.WafError('task without an environment')
 
52
def subst_func(tsk):
 
53
        m4_re=re.compile('@(\w+)@',re.M)
 
54
        code=tsk.inputs[0].read()
 
55
        code=code.replace('%','%%')
 
56
        s=m4_re.sub(r'%(\1)s',code)
 
57
        env=tsk.env
 
58
        di=getattr(tsk,'dict',{})or getattr(tsk.generator,'dict',{})
 
59
        if not di:
 
60
                names=m4_re.findall(code)
 
61
                for i in names:
 
62
                        di[i]=env.get_flat(i)or env.get_flat(i.upper())
 
63
        tsk.outputs[0].write(s%di)
 
64
def apply_subst(self):
 
65
        Utils.def_attrs(self,fun=subst_func)
 
66
        lst=self.to_list(self.source)
 
67
        self.meths.remove('process_source')
 
68
        self.dict=getattr(self,'dict',{})
 
69
        for filename in lst:
 
70
                node=self.path.find_resource(filename)
 
71
                if not node:raise Errors.WafError('cannot find input file %s for processing'%filename)
 
72
                if self.target:
 
73
                        newnode=self.path.find_or_declare(self.target)
 
74
                else:
 
75
                        newnode=node.change_ext('')
 
76
                try:
 
77
                        self.dict=self.dict.get_merged_dict()
 
78
                except AttributeError:
 
79
                        pass
 
80
                if self.dict and not self.env['DICT_HASH']:
 
81
                        self.env=self.env.derive()
 
82
                        keys=list(self.dict.keys())
 
83
                        keys.sort()
 
84
                        lst=[self.dict[x]for x in keys]
 
85
                        self.env['DICT_HASH']=str(Utils.h_list(lst))
 
86
                tsk=self.create_task('copy',node,newnode)
 
87
                tsk.fun=self.fun
 
88
                tsk.dict=self.dict
 
89
                tsk.dep_vars=['DICT_HASH']
 
90
                tsk.chmod=getattr(self,'chmod',Utils.O644)
 
91
                if not tsk.env:
 
92
                        tsk.debug()
 
93
                        raise Errors.WafError('task without an environment')
 
94
class cmd_arg(object):
 
95
        def __init__(self,name,template='%s'):
 
96
                self.name=name
 
97
                self.template=template
 
98
                self.node=None
 
99
class input_file(cmd_arg):
 
100
        def find_node(self,base_path):
 
101
                assert isinstance(base_path,Node.Node)
 
102
                self.node=base_path.find_resource(self.name)
 
103
                if self.node is None:
 
104
                        raise Errors.WafError("Input file %s not found in "%(self.name,base_path))
 
105
        def get_path(self,env,absolute):
 
106
                if absolute:
 
107
                        return self.template%self.node.abspath()
 
108
                else:
 
109
                        return self.template%self.node.srcpath()
 
110
class output_file(cmd_arg):
 
111
        def find_node(self,base_path):
 
112
                assert isinstance(base_path,Node.Node)
 
113
                self.node=base_path.find_or_declare(self.name)
 
114
                if self.node is None:
 
115
                        raise Errors.WafError("Output file %s not found in "%(self.name,base_path))
 
116
        def get_path(self,env,absolute):
 
117
                if absolute:
 
118
                        return self.template%self.node.abspath()
 
119
                else:
 
120
                        return self.template%self.node.bldpath()
 
121
class cmd_dir_arg(cmd_arg):
 
122
        def find_node(self,base_path):
 
123
                assert isinstance(base_path,Node.Node)
 
124
                self.node=base_path.find_dir(self.name)
 
125
                if self.node is None:
 
126
                        raise Errors.WafError("Directory %s not found in "%(self.name,base_path))
 
127
class input_dir(cmd_dir_arg):
 
128
        def get_path(self,dummy_env,dummy_absolute):
 
129
                return self.template%self.node.abspath()
 
130
class output_dir(cmd_dir_arg):
 
131
        def get_path(self,env,dummy_absolute):
 
132
                return self.template%self.node.abspath()
 
133
class command_output(Task.Task):
 
134
        color="BLUE"
 
135
        def __init__(self,env,command,command_node,command_args,stdin,stdout,cwd,os_env,stderr):
 
136
                Task.Task.__init__(self,env=env)
 
137
                assert isinstance(command,(str,Node.Node))
 
138
                self.command=command
 
139
                self.command_args=command_args
 
140
                self.stdin=stdin
 
141
                self.stdout=stdout
 
142
                self.cwd=cwd
 
143
                self.os_env=os_env
 
144
                self.stderr=stderr
 
145
                if command_node is not None:self.dep_nodes=[command_node]
 
146
                self.dep_vars=[]
 
147
        def run(self):
 
148
                task=self
 
149
                def input_path(node,template):
 
150
                        if task.cwd is None:
 
151
                                return template%node.bldpath()
 
152
                        else:
 
153
                                return template%node.abspath()
 
154
                def output_path(node,template):
 
155
                        fun=node.abspath
 
156
                        if task.cwd is None:fun=node.bldpath
 
157
                        return template%fun()
 
158
                if isinstance(task.command,Node.Node):
 
159
                        argv=[input_path(task.command,'%s')]
 
160
                else:
 
161
                        argv=[task.command]
 
162
                for arg in task.command_args:
 
163
                        if isinstance(arg,str):
 
164
                                argv.append(arg)
 
165
                        else:
 
166
                                assert isinstance(arg,cmd_arg)
 
167
                                argv.append(arg.get_path(task.env,(task.cwd is not None)))
 
168
                if task.stdin:
 
169
                        stdin=open(input_path(task.stdin,'%s'))
 
170
                else:
 
171
                        stdin=None
 
172
                if task.stdout:
 
173
                        stdout=open(output_path(task.stdout,'%s'),"w")
 
174
                else:
 
175
                        stdout=None
 
176
                if task.stderr:
 
177
                        stderr=open(output_path(task.stderr,'%s'),"w")
 
178
                else:
 
179
                        stderr=None
 
180
                if task.cwd is None:
 
181
                        cwd=('None (actually %r)'%os.getcwd())
 
182
                else:
 
183
                        cwd=repr(task.cwd)
 
184
                debug("command-output: cwd=%s, stdin=%r, stdout=%r, argv=%r"%(cwd,stdin,stdout,argv))
 
185
                if task.os_env is None:
 
186
                        os_env=os.environ
 
187
                else:
 
188
                        os_env=task.os_env
 
189
                command=Utils.subprocess.Popen(argv,stdin=stdin,stdout=stdout,stderr=stderr,cwd=task.cwd,env=os_env)
 
190
                return command.wait()
 
191
def init_cmd_output(self):
 
192
        Utils.def_attrs(self,stdin=None,stdout=None,stderr=None,command=None,command_is_external=False,argv=[],dependencies=[],dep_vars=[],hidden_inputs=[],hidden_outputs=[],cwd=None,os_env=None)
 
193
def apply_cmd_output(self):
 
194
        if self.command is None:
 
195
                raise Errors.WafError("command-output missing command")
 
196
        if self.command_is_external:
 
197
                cmd=self.command
 
198
                cmd_node=None
 
199
        else:
 
200
                cmd_node=self.path.find_resource(self.command)
 
201
                assert cmd_node is not None,('''Could not find command '%s' in source tree.
 
202
Hint: if this is an external command,
 
203
use command_is_external=True''')%(self.command,)
 
204
                cmd=cmd_node
 
205
        if self.cwd is None:
 
206
                cwd=None
 
207
        else:
 
208
                assert isinstance(cwd,CmdDirArg)
 
209
                self.cwd.find_node(self.path)
 
210
        args=[]
 
211
        inputs=[]
 
212
        outputs=[]
 
213
        for arg in self.argv:
 
214
                if isinstance(arg,cmd_arg):
 
215
                        arg.find_node(self.path)
 
216
                        if isinstance(arg,input_file):
 
217
                                inputs.append(arg.node)
 
218
                        if isinstance(arg,output_file):
 
219
                                outputs.append(arg.node)
 
220
        if self.stdout is None:
 
221
                stdout=None
 
222
        else:
 
223
                assert isinstance(self.stdout,str)
 
224
                stdout=self.path.find_or_declare(self.stdout)
 
225
                if stdout is None:
 
226
                        raise Errors.WafError("File %s not found"%(self.stdout,))
 
227
                outputs.append(stdout)
 
228
        if self.stderr is None:
 
229
                stderr=None
 
230
        else:
 
231
                assert isinstance(self.stderr,str)
 
232
                stderr=self.path.find_or_declare(self.stderr)
 
233
                if stderr is None:
 
234
                        raise Errors.WafError("File %s not found"%(self.stderr,))
 
235
                outputs.append(stderr)
 
236
        if self.stdin is None:
 
237
                stdin=None
 
238
        else:
 
239
                assert isinstance(self.stdin,str)
 
240
                stdin=self.path.find_resource(self.stdin)
 
241
                if stdin is None:
 
242
                        raise Errors.WafError("File %s not found"%(self.stdin,))
 
243
                inputs.append(stdin)
 
244
        for hidden_input in self.to_list(self.hidden_inputs):
 
245
                node=self.path.find_resource(hidden_input)
 
246
                if node is None:
 
247
                        raise Errors.WafError("File %s not found in dir %s"%(hidden_input,self.path))
 
248
                inputs.append(node)
 
249
        for hidden_output in self.to_list(self.hidden_outputs):
 
250
                node=self.path.find_or_declare(hidden_output)
 
251
                if node is None:
 
252
                        raise Errors.WafError("File %s not found in dir %s"%(hidden_output,self.path))
 
253
                outputs.append(node)
 
254
        if not(inputs or getattr(self,'no_inputs',None)):
 
255
                raise Errors.WafError('command-output objects must have at least one input file or give self.no_inputs')
 
256
        if not(outputs or getattr(self,'no_outputs',None)):
 
257
                raise Errors.WafError('command-output objects must have at least one output file or give self.no_outputs')
 
258
        cwd=self.bld.variant_dir
 
259
        task=command_output(self.env,cmd,cmd_node,self.argv,stdin,stdout,cwd,self.os_env,stderr)
 
260
        task.generator=self
 
261
        copy_attrs(self,task,'before after ext_in ext_out',only_if_set=True)
 
262
        self.tasks.append(task)
 
263
        task.inputs=inputs
 
264
        task.outputs=outputs
 
265
        task.dep_vars=self.to_list(self.dep_vars)
 
266
        for dep in self.dependencies:
 
267
                assert dep is not self
 
268
                dep.post()
 
269
                for dep_task in dep.tasks:
 
270
                        task.set_run_after(dep_task)
 
271
        if not task.inputs:
 
272
                task.runnable_status=type(Task.TaskBase.run)(runnable_status,task,task.__class__)
 
273
                task.post_run=type(Task.TaskBase.run)(post_run,task,task.__class__)
 
274
def post_run(self):
 
275
        for x in self.outputs:
 
276
                x.sig=Utils.h_file(x.abspath())
 
277
def runnable_status(self):
 
278
        return self.RUN_ME
 
279
Task.task_factory('copy',vars=[],func=action_process_file_func)
 
280
 
 
281
feature('cmd')(apply_cmd)
 
282
feature('copy')(apply_copy)
 
283
before_method('process_source')(apply_copy)
 
284
feature('subst')(apply_subst)
 
285
before_method('process_source')(apply_subst)
 
286
feature('command-output')(init_cmd_output)
 
287
feature('command-output')(apply_cmd_output)
 
288
after_method('init_cmd_output')(apply_cmd_output)
 
 
b'\\ No newline at end of file'