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

« back to all changes in this revision

Viewing changes to waflib/Scripting.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 os,shutil,traceback,errno,sys,stat
 
6
from waflib import Utils,Configure,Logs,Options,ConfigSet,Context,Errors,Build,Node
 
7
build_dir_override=None
 
8
no_climb_commands=['configure']
 
9
default_cmd="build"
 
10
def waf_entry_point(current_directory,version,wafdir):
 
11
        Logs.init_log()
 
12
        if Context.WAFVERSION!=version:
 
13
                Logs.error('Waf script %r and library %r do not match (directory %r)'%(version,Context.WAFVERSION,wafdir))
 
14
                sys.exit(1)
 
15
        if'--version'in sys.argv:
 
16
                ctx=Context.create_context('options')
 
17
                ctx.curdir=current_directory
 
18
                ctx.parse_args()
 
19
                sys.exit(0)
 
20
        Context.waf_dir=wafdir
 
21
        Context.launch_dir=current_directory
 
22
        no_climb=os.environ.get('NOCLIMB',None)
 
23
        if not no_climb:
 
24
                for k in no_climb_commands:
 
25
                        if k in sys.argv:
 
26
                                no_climb=True
 
27
                                break
 
28
        cur=current_directory
 
29
        while cur:
 
30
                lst=os.listdir(cur)
 
31
                if Options.lockfile in lst:
 
32
                        env=ConfigSet.ConfigSet()
 
33
                        try:
 
34
                                env.load(os.path.join(cur,Options.lockfile))
 
35
                                ino=os.stat(cur)[stat.ST_INO]
 
36
                        except Exception:
 
37
                                pass
 
38
                        else:
 
39
                                for x in[env.run_dir,env.top_dir,env.out_dir]:
 
40
                                        if Utils.is_win32:
 
41
                                                if cur==x:
 
42
                                                        load=True
 
43
                                                        break
 
44
                                        else:
 
45
                                                try:
 
46
                                                        ino2=os.stat(x)[stat.ST_INO]
 
47
                                                except:
 
48
                                                        pass
 
49
                                                else:
 
50
                                                        if ino==ino2:
 
51
                                                                load=True
 
52
                                                                break
 
53
                                else:
 
54
                                        Logs.warn('invalid lock file in %s'%cur)
 
55
                                        load=False
 
56
                                if load:
 
57
                                        Context.run_dir=env.run_dir
 
58
                                        Context.top_dir=env.top_dir
 
59
                                        Context.out_dir=env.out_dir
 
60
                                        break
 
61
                if not Context.run_dir:
 
62
                        if Context.WSCRIPT_FILE in lst:
 
63
                                Context.run_dir=cur
 
64
                next=os.path.dirname(cur)
 
65
                if next==cur:
 
66
                        break
 
67
                cur=next
 
68
                if no_climb:
 
69
                        break
 
70
        if not Context.run_dir:
 
71
                if'-h'in sys.argv or'--help'in sys.argv:
 
72
                        Logs.warn('No wscript file found: the help message may be incomplete')
 
73
                        ctx=Context.create_context('options')
 
74
                        ctx.curdir=current_directory
 
75
                        ctx.parse_args()
 
76
                        sys.exit(0)
 
77
                Logs.error('Waf: Run from a directory containing a file named %r'%Context.WSCRIPT_FILE)
 
78
                sys.exit(1)
 
79
        try:
 
80
                os.chdir(Context.run_dir)
 
81
        except OSError:
 
82
                Logs.error('Waf: The folder %r is unreadable'%Context.run_dir)
 
83
                sys.exit(1)
 
84
        try:
 
85
                set_main_module(Context.run_dir+os.sep+Context.WSCRIPT_FILE)
 
86
        except Errors.WafError ,e:
 
87
                Logs.pprint('RED',e.verbose_msg)
 
88
                Logs.error(str(e))
 
89
                sys.exit(1)
 
90
        except Exception ,e:
 
91
                Logs.error('Waf: The wscript in %r is unreadable'%Context.run_dir,e)
 
92
                traceback.print_exc(file=sys.stdout)
 
93
                sys.exit(2)
 
94
        parse_options()
 
95
        try:
 
96
                run_commands()
 
97
        except Errors.WafError ,e:
 
98
                if Logs.verbose>1:
 
99
                        Logs.pprint('RED',e.verbose_msg)
 
100
                Logs.error(e.msg)
 
101
                sys.exit(1)
 
102
        except Exception ,e:
 
103
                traceback.print_exc(file=sys.stdout)
 
104
                sys.exit(2)
 
105
        except KeyboardInterrupt:
 
106
                Logs.pprint('RED','Interrupted')
 
107
                sys.exit(68)
 
108
def set_main_module(file_path):
 
109
        Context.g_module=Context.load_module(file_path)
 
110
        Context.g_module.root_path=file_path
 
111
        def set_def(obj):
 
112
                name=obj.__name__
 
113
                if not name in Context.g_module.__dict__:
 
114
                        setattr(Context.g_module,name,obj)
 
115
        for k in[update,dist,distclean,distcheck,update]:
 
116
                set_def(k)
 
117
        if not'init'in Context.g_module.__dict__:
 
118
                Context.g_module.init=Utils.nada
 
119
        if not'shutdown'in Context.g_module.__dict__:
 
120
                Context.g_module.shutdown=Utils.nada
 
121
        if not'options'in Context.g_module.__dict__:
 
122
                Context.g_module.options=Utils.nada
 
123
def parse_options():
 
124
        Context.create_context('options').execute()
 
125
        if not Options.commands:
 
126
                Options.commands=[default_cmd]
 
127
        Logs.verbose=Options.options.verbose
 
128
        Logs.init_log()
 
129
        if Options.options.zones:
 
130
                Logs.zones=Options.options.zones.split(',')
 
131
                if not Logs.verbose:
 
132
                        Logs.verbose=1
 
133
        elif Logs.verbose>0:
 
134
                Logs.zones=['runner']
 
135
        if Logs.verbose>2:
 
136
                Logs.zones=['*']
 
137
def run_command(cmd_name):
 
138
        ctx=Context.create_context(cmd_name)
 
139
        ctx.options=Options.options
 
140
        ctx.cmd=cmd_name
 
141
        ctx.execute()
 
142
        return ctx
 
143
def run_commands():
 
144
        run_command('init')
 
145
        while Options.commands:
 
146
                cmd_name=Options.commands.pop(0)
 
147
                timer=Utils.Timer()
 
148
                run_command(cmd_name)
 
149
                if not Options.options.progress_bar:
 
150
                        elapsed=' (%s)'%str(timer)
 
151
                        Logs.info('%r finished successfully%s'%(cmd_name,elapsed))
 
152
        run_command('shutdown')
 
153
def _can_distclean(name):
 
154
        for k in'.o .moc .exe'.split():
 
155
                if name.endswith(k):
 
156
                        return True
 
157
        return False
 
158
def distclean_dir(dirname):
 
159
        for(root,dirs,files)in os.walk(dirname):
 
160
                for f in files:
 
161
                        if _can_distclean(f):
 
162
                                fname=root+os.sep+f
 
163
                                try:
 
164
                                        os.unlink(fname)
 
165
                                except:
 
166
                                        Logs.warn('could not remove %r'%fname)
 
167
        for x in[Context.DBFILE,'config.log']:
 
168
                try:
 
169
                        os.unlink(x)
 
170
                except:
 
171
                        pass
 
172
        try:
 
173
                shutil.rmtree('c4che')
 
174
        except:
 
175
                pass
 
176
def distclean(ctx):
 
177
        '''removes the build directory'''
 
178
        lst=os.listdir('.')
 
179
        for f in lst:
 
180
                if f==Options.lockfile:
 
181
                        try:
 
182
                                proj=ConfigSet.ConfigSet(f)
 
183
                        except:
 
184
                                Logs.warn('could not read %r'%f)
 
185
                                continue
 
186
                        if proj['out_dir']!=proj['top_dir']:
 
187
                                try:
 
188
                                        shutil.rmtree(proj['out_dir'])
 
189
                                except IOError:
 
190
                                        pass
 
191
                                except OSError ,e:
 
192
                                        if e.errno!=errno.ENOENT:
 
193
                                                Logs.warn('project %r cannot be removed'%proj[Context.OUT])
 
194
                        else:
 
195
                                distclean_dir(proj['out_dir'])
 
196
                        for k in(proj['out_dir'],proj['top_dir'],proj['run_dir']):
 
197
                                try:
 
198
                                        os.remove(os.path.join(k,Options.lockfile))
 
199
                                except OSError ,e:
 
200
                                        if e.errno!=errno.ENOENT:
 
201
                                                Logs.warn('file %r cannot be removed'%f)
 
202
                if f.startswith('.waf-')and not Options.commands:
 
203
                        shutil.rmtree(f,ignore_errors=True)
 
204
class Dist(Context.Context):
 
205
        cmd='dist'
 
206
        fun='dist'
 
207
        algo='tar.bz2'
 
208
        ext_algo={}
 
209
        def execute(self):
 
210
                self.recurse([os.path.dirname(Context.g_module.root_path)])
 
211
                self.archive()
 
212
        def archive(self):
 
213
                import tarfile
 
214
                arch_name=self.get_arch_name()
 
215
                try:
 
216
                        self.base_path
 
217
                except:
 
218
                        self.base_path=self.path
 
219
                node=self.base_path.make_node(arch_name)
 
220
                try:
 
221
                        node.delete()
 
222
                except:
 
223
                        pass
 
224
                files=self.get_files()
 
225
                if self.algo.startswith('tar.'):
 
226
                        tar=tarfile.open(arch_name,'w:'+self.algo.replace('tar.',''))
 
227
                        for x in files:
 
228
                                tinfo=tar.gettarinfo(name=x.abspath(),arcname=self.get_tar_prefix()+'/'+x.path_from(self.base_path))
 
229
                                tinfo.uid=0
 
230
                                tinfo.gid=0
 
231
                                tinfo.uname='root'
 
232
                                tinfo.gname='root'
 
233
                                fu=None
 
234
                                try:
 
235
                                        fu=open(x.abspath(),'rb')
 
236
                                        tar.addfile(tinfo,fileobj=fu)
 
237
                                finally:
 
238
                                        fu.close()
 
239
                        tar.close()
 
240
                elif self.algo=='zip':
 
241
                        import zipfile
 
242
                        zip=zipfile.ZipFile(arch_name,'w',compression=zipfile.ZIP_DEFLATED)
 
243
                        for x in files:
 
244
                                archive_name=self.get_base_name()+'/'+x.path_from(self.base_path)
 
245
                                zip.write(x.abspath(),archive_name,zipfile.ZIP_DEFLATED)
 
246
                        zip.close()
 
247
                else:
 
248
                        self.fatal('Valid algo types are tar.bz2, tar.gz or zip')
 
249
                try:
 
250
                        from hashlib import sha1 as sha
 
251
                except ImportError:
 
252
                        from sha import sha
 
253
                try:
 
254
                        digest=" (sha=%r)"%sha(node.read()).hexdigest()
 
255
                except:
 
256
                        digest=''
 
257
                Logs.info('New archive created: %s%s'%(self.arch_name,digest))
 
258
        def get_tar_prefix(self):
 
259
                try:
 
260
                        return self.tar_prefix
 
261
                except:
 
262
                        return self.get_base_name()
 
263
        def get_arch_name(self):
 
264
                try:
 
265
                        self.arch_name
 
266
                except:
 
267
                        self.arch_name=self.get_base_name()+'.'+self.ext_algo.get(self.algo,self.algo)
 
268
                return self.arch_name
 
269
        def get_base_name(self):
 
270
                try:
 
271
                        self.base_name
 
272
                except:
 
273
                        appname=getattr(Context.g_module,Context.APPNAME,'noname')
 
274
                        version=getattr(Context.g_module,Context.VERSION,'1.0')
 
275
                        self.base_name=appname+'-'+version
 
276
                return self.base_name
 
277
        def get_excl(self):
 
278
                try:
 
279
                        return self.excl
 
280
                except:
 
281
                        self.excl=Node.exclude_regs+' **/waf-1.6.* **/.waf-1.6* **/*~ **/*.rej **/*.orig **/*.pyc **/*.pyo **/*.bak **/*.swp **/.lock-w*'
 
282
                        nd=self.root.find_node(Context.out_dir)
 
283
                        if nd:
 
284
                                self.excl+=' '+nd.path_from(self.base_path)
 
285
                        return self.excl
 
286
        def get_files(self):
 
287
                try:
 
288
                        files=self.files
 
289
                except:
 
290
                        files=self.base_path.ant_glob('**/*',excl=self.get_excl())
 
291
                return files
 
292
def dist(ctx):
 
293
        '''makes a tarball for redistributing the sources'''
 
294
        pass
 
295
class DistCheck(Dist):
 
296
        fun='distcheck'
 
297
        cmd='distcheck'
 
298
        def execute(self):
 
299
                self.recurse([os.path.dirname(Context.g_module.root_path)])
 
300
                self.archive()
 
301
                self.check()
 
302
        def check(self):
 
303
                import tempfile,tarfile
 
304
                t=None
 
305
                try:
 
306
                        t=tarfile.open(self.get_arch_name())
 
307
                        for x in t:
 
308
                                t.extract(x)
 
309
                finally:
 
310
                        if t:
 
311
                                t.close()
 
312
                instdir=tempfile.mkdtemp('.inst',self.get_base_name())
 
313
                ret=Utils.subprocess.Popen([sys.argv[0],'configure','install','uninstall','--destdir='+instdir],cwd=self.get_base_name()).wait()
 
314
                if ret:
 
315
                        raise Errors.WafError('distcheck failed with code %i'%ret)
 
316
                if os.path.exists(instdir):
 
317
                        raise Errors.WafError('distcheck succeeded, but files were left in %s'%instdir)
 
318
                shutil.rmtree(self.get_base_name())
 
319
def distcheck(ctx):
 
320
        '''checks if the project compiles (tarball from 'dist')'''
 
321
        pass
 
322
def update(ctx):
 
323
        '''updates the plugins from the *waflib/extras* directory'''
 
324
        lst=Options.options.files.split(',')
 
325
        if not lst:
 
326
                lst=[x for x in Utils.listdir(Context.waf_dir+'/waflib/extras')if x.endswith('.py')]
 
327
        for x in lst:
 
328
                tool=x.replace('.py','')
 
329
                try:
 
330
                        Configure.download_tool(tool,force=True,ctx=ctx)
 
331
                except Errors.WafError:
 
332
                        Logs.error('Could not find the tool %s in the remote repository'%x)
 
333
def autoconfigure(execute_method):
 
334
        def execute(self):
 
335
                if not Configure.autoconfig:
 
336
                        return execute_method(self)
 
337
                env=ConfigSet.ConfigSet()
 
338
                do_config=False
 
339
                try:
 
340
                        env.load(os.path.join(Context.top_dir,Options.lockfile))
 
341
                except Exception:
 
342
                        Logs.warn('Configuring the project')
 
343
                        do_config=True
 
344
                else:
 
345
                        if env.run_dir!=Context.run_dir:
 
346
                                do_config=True
 
347
                        else:
 
348
                                h=0
 
349
                                for f in env['files']:
 
350
                                        h=hash((h,Utils.readf(f,'rb')))
 
351
                                do_config=h!=env.hash
 
352
                if do_config:
 
353
                        Options.commands.insert(0,self.cmd)
 
354
                        Options.commands.insert(0,'configure')
 
355
                        return
 
356
                return execute_method(self)
 
357
        return execute
 
358
Build.BuildContext.execute=autoconfigure(Build.BuildContext.execute)