akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # encoding: utf-8 |
| 3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
| 4 | |
| 5 | import os,imp,sys |
| 6 | from waflib import Utils,Errors,Logs |
| 7 | import waflib.Node |
| 8 | HEXVERSION=0x1070b00 |
| 9 | WAFVERSION="1.7.11" |
| 10 | WAFREVISION="50f631bc5e00bdda966c68094229b99be9a21084" |
| 11 | ABI=98 |
| 12 | DBFILE='.wafpickle-%s-%d-%d'%(sys.platform,sys.hexversion,ABI) |
| 13 | APPNAME='APPNAME' |
| 14 | VERSION='VERSION' |
| 15 | TOP='top' |
| 16 | OUT='out' |
| 17 | WSCRIPT_FILE='wscript' |
| 18 | launch_dir='' |
| 19 | run_dir='' |
| 20 | top_dir='' |
| 21 | out_dir='' |
| 22 | waf_dir='' |
| 23 | local_repo='' |
| 24 | remote_repo='http://waf.googlecode.com/git/' |
| 25 | remote_locs=['waflib/extras','waflib/Tools'] |
| 26 | g_module=None |
| 27 | STDOUT=1 |
| 28 | STDERR=-1 |
| 29 | BOTH=0 |
| 30 | classes=[] |
| 31 | def create_context(cmd_name,*k,**kw): |
| 32 | global classes |
| 33 | for x in classes: |
| 34 | if x.cmd==cmd_name: |
| 35 | return x(*k,**kw) |
| 36 | ctx=Context(*k,**kw) |
| 37 | ctx.fun=cmd_name |
| 38 | return ctx |
| 39 | class store_context(type): |
| 40 | def __init__(cls,name,bases,dict): |
| 41 | super(store_context,cls).__init__(name,bases,dict) |
| 42 | name=cls.__name__ |
| 43 | if name=='ctx'or name=='Context': |
| 44 | return |
| 45 | try: |
| 46 | cls.cmd |
| 47 | except AttributeError: |
| 48 | raise Errors.WafError('Missing command for the context class %r (cmd)'%name) |
| 49 | if not getattr(cls,'fun',None): |
| 50 | cls.fun=cls.cmd |
| 51 | global classes |
| 52 | classes.insert(0,cls) |
| 53 | ctx=store_context('ctx',(object,),{}) |
| 54 | class Context(ctx): |
| 55 | errors=Errors |
| 56 | tools={} |
| 57 | def __init__(self,**kw): |
| 58 | try: |
| 59 | rd=kw['run_dir'] |
| 60 | except KeyError: |
| 61 | global run_dir |
| 62 | rd=run_dir |
| 63 | class node_class(waflib.Node.Node): |
| 64 | pass |
| 65 | self.node_class=node_class |
| 66 | self.node_class.__module__="waflib.Node" |
| 67 | self.node_class.__name__="Nod3" |
| 68 | self.node_class.ctx=self |
| 69 | self.root=self.node_class('',None) |
| 70 | self.cur_script=None |
| 71 | self.path=self.root.find_dir(rd) |
| 72 | self.stack_path=[] |
| 73 | self.exec_dict={'ctx':self,'conf':self,'bld':self,'opt':self} |
| 74 | self.logger=None |
| 75 | def __hash__(self): |
| 76 | return id(self) |
| 77 | def load(self,tool_list,*k,**kw): |
| 78 | tools=Utils.to_list(tool_list) |
| 79 | path=Utils.to_list(kw.get('tooldir','')) |
| 80 | for t in tools: |
| 81 | module=load_tool(t,path) |
| 82 | fun=getattr(module,kw.get('name',self.fun),None) |
| 83 | if fun: |
| 84 | fun(self) |
| 85 | def execute(self): |
| 86 | global g_module |
| 87 | self.recurse([os.path.dirname(g_module.root_path)]) |
| 88 | def pre_recurse(self,node): |
| 89 | self.stack_path.append(self.cur_script) |
| 90 | self.cur_script=node |
| 91 | self.path=node.parent |
| 92 | def post_recurse(self,node): |
| 93 | self.cur_script=self.stack_path.pop() |
| 94 | if self.cur_script: |
| 95 | self.path=self.cur_script.parent |
| 96 | def recurse(self,dirs,name=None,mandatory=True,once=True): |
| 97 | try: |
| 98 | cache=self.recurse_cache |
| 99 | except AttributeError: |
| 100 | cache=self.recurse_cache={} |
| 101 | for d in Utils.to_list(dirs): |
| 102 | if not os.path.isabs(d): |
| 103 | d=os.path.join(self.path.abspath(),d) |
| 104 | WSCRIPT=os.path.join(d,WSCRIPT_FILE) |
| 105 | WSCRIPT_FUN=WSCRIPT+'_'+(name or self.fun) |
| 106 | node=self.root.find_node(WSCRIPT_FUN) |
| 107 | if node and(not once or node not in cache): |
| 108 | cache[node]=True |
| 109 | self.pre_recurse(node) |
| 110 | try: |
| 111 | function_code=node.read('rU') |
| 112 | exec(compile(function_code,node.abspath(),'exec'),self.exec_dict) |
| 113 | finally: |
| 114 | self.post_recurse(node) |
| 115 | elif not node: |
| 116 | node=self.root.find_node(WSCRIPT) |
| 117 | tup=(node,name or self.fun) |
| 118 | if node and(not once or tup not in cache): |
| 119 | cache[tup]=True |
| 120 | self.pre_recurse(node) |
| 121 | try: |
| 122 | wscript_module=load_module(node.abspath()) |
| 123 | user_function=getattr(wscript_module,(name or self.fun),None) |
| 124 | if not user_function: |
| 125 | if not mandatory: |
| 126 | continue |
| 127 | raise Errors.WafError('No function %s defined in %s'%(name or self.fun,node.abspath())) |
| 128 | user_function(self) |
| 129 | finally: |
| 130 | self.post_recurse(node) |
| 131 | elif not node: |
| 132 | if not mandatory: |
| 133 | continue |
| 134 | raise Errors.WafError('No wscript file in directory %s'%d) |
| 135 | def exec_command(self,cmd,**kw): |
| 136 | subprocess=Utils.subprocess |
| 137 | kw['shell']=isinstance(cmd,str) |
| 138 | Logs.debug('runner: %r'%cmd) |
| 139 | Logs.debug('runner_env: kw=%s'%kw) |
| 140 | if self.logger: |
| 141 | self.logger.info(cmd) |
| 142 | if'stdout'not in kw: |
| 143 | kw['stdout']=subprocess.PIPE |
| 144 | if'stderr'not in kw: |
| 145 | kw['stderr']=subprocess.PIPE |
| 146 | try: |
| 147 | if kw['stdout']or kw['stderr']: |
| 148 | p=subprocess.Popen(cmd,**kw) |
| 149 | (out,err)=p.communicate() |
| 150 | ret=p.returncode |
| 151 | else: |
| 152 | out,err=(None,None) |
| 153 | ret=subprocess.Popen(cmd,**kw).wait() |
| 154 | except Exception ,e: |
| 155 | raise Errors.WafError('Execution failure: %s'%str(e),ex=e) |
| 156 | if out: |
| 157 | if not isinstance(out,str): |
| 158 | out=out.decode(sys.stdout.encoding or'iso8859-1') |
| 159 | if self.logger: |
| 160 | self.logger.debug('out: %s'%out) |
| 161 | else: |
| 162 | sys.stdout.write(out) |
| 163 | if err: |
| 164 | if not isinstance(err,str): |
| 165 | err=err.decode(sys.stdout.encoding or'iso8859-1') |
| 166 | if self.logger: |
| 167 | self.logger.error('err: %s'%err) |
| 168 | else: |
| 169 | sys.stderr.write(err) |
| 170 | return ret |
| 171 | def cmd_and_log(self,cmd,**kw): |
| 172 | subprocess=Utils.subprocess |
| 173 | kw['shell']=isinstance(cmd,str) |
| 174 | Logs.debug('runner: %r'%cmd) |
| 175 | if'quiet'in kw: |
| 176 | quiet=kw['quiet'] |
| 177 | del kw['quiet'] |
| 178 | else: |
| 179 | quiet=None |
| 180 | if'output'in kw: |
| 181 | to_ret=kw['output'] |
| 182 | del kw['output'] |
| 183 | else: |
| 184 | to_ret=STDOUT |
| 185 | kw['stdout']=kw['stderr']=subprocess.PIPE |
| 186 | if quiet is None: |
| 187 | self.to_log(cmd) |
| 188 | try: |
| 189 | p=subprocess.Popen(cmd,**kw) |
| 190 | (out,err)=p.communicate() |
| 191 | except Exception ,e: |
| 192 | raise Errors.WafError('Execution failure: %s'%str(e),ex=e) |
| 193 | if not isinstance(out,str): |
| 194 | out=out.decode(sys.stdout.encoding or'iso8859-1') |
| 195 | if not isinstance(err,str): |
| 196 | err=err.decode(sys.stdout.encoding or'iso8859-1') |
| 197 | if out and quiet!=STDOUT and quiet!=BOTH: |
| 198 | self.to_log('out: %s'%out) |
| 199 | if err and quiet!=STDERR and quiet!=BOTH: |
| 200 | self.to_log('err: %s'%err) |
| 201 | if p.returncode: |
| 202 | e=Errors.WafError('Command %r returned %r'%(cmd,p.returncode)) |
| 203 | e.returncode=p.returncode |
| 204 | e.stderr=err |
| 205 | e.stdout=out |
| 206 | raise e |
| 207 | if to_ret==BOTH: |
| 208 | return(out,err) |
| 209 | elif to_ret==STDERR: |
| 210 | return err |
| 211 | return out |
| 212 | def fatal(self,msg,ex=None): |
| 213 | if self.logger: |
| 214 | self.logger.info('from %s: %s'%(self.path.abspath(),msg)) |
| 215 | try: |
| 216 | msg='%s\n(complete log in %s)'%(msg,self.logger.handlers[0].baseFilename) |
| 217 | except Exception: |
| 218 | pass |
| 219 | raise self.errors.ConfigurationError(msg,ex=ex) |
| 220 | def to_log(self,msg): |
| 221 | if not msg: |
| 222 | return |
| 223 | if self.logger: |
| 224 | self.logger.info(msg) |
| 225 | else: |
| 226 | sys.stderr.write(str(msg)) |
| 227 | sys.stderr.flush() |
| 228 | def msg(self,msg,result,color=None): |
| 229 | self.start_msg(msg) |
| 230 | if not isinstance(color,str): |
| 231 | color=result and'GREEN'or'YELLOW' |
| 232 | self.end_msg(result,color) |
| 233 | def start_msg(self,msg): |
| 234 | try: |
| 235 | if self.in_msg: |
| 236 | self.in_msg+=1 |
| 237 | return |
| 238 | except AttributeError: |
| 239 | self.in_msg=0 |
| 240 | self.in_msg+=1 |
| 241 | try: |
| 242 | self.line_just=max(self.line_just,len(msg)) |
| 243 | except AttributeError: |
| 244 | self.line_just=max(40,len(msg)) |
| 245 | for x in(self.line_just*'-',msg): |
| 246 | self.to_log(x) |
| 247 | Logs.pprint('NORMAL',"%s :"%msg.ljust(self.line_just),sep='') |
| 248 | def end_msg(self,result,color=None): |
| 249 | self.in_msg-=1 |
| 250 | if self.in_msg: |
| 251 | return |
| 252 | defcolor='GREEN' |
| 253 | if result==True: |
| 254 | msg='ok' |
| 255 | elif result==False: |
| 256 | msg='not found' |
| 257 | defcolor='YELLOW' |
| 258 | else: |
| 259 | msg=str(result) |
| 260 | self.to_log(msg) |
| 261 | Logs.pprint(color or defcolor,msg) |
| 262 | def load_special_tools(self,var,ban=[]): |
| 263 | global waf_dir |
| 264 | lst=self.root.find_node(waf_dir).find_node('waflib/extras').ant_glob(var) |
| 265 | for x in lst: |
| 266 | if not x.name in ban: |
| 267 | load_tool(x.name.replace('.py','')) |
| 268 | cache_modules={} |
| 269 | def load_module(path): |
| 270 | try: |
| 271 | return cache_modules[path] |
| 272 | except KeyError: |
| 273 | pass |
| 274 | module=imp.new_module(WSCRIPT_FILE) |
| 275 | try: |
| 276 | code=Utils.readf(path,m='rU') |
| 277 | except(IOError,OSError): |
| 278 | raise Errors.WafError('Could not read the file %r'%path) |
| 279 | module_dir=os.path.dirname(path) |
| 280 | sys.path.insert(0,module_dir) |
| 281 | exec(compile(code,path,'exec'),module.__dict__) |
| 282 | sys.path.remove(module_dir) |
| 283 | cache_modules[path]=module |
| 284 | return module |
| 285 | def load_tool(tool,tooldir=None): |
| 286 | if tool=='java': |
| 287 | tool='javaw' |
| 288 | elif tool=='compiler_cc': |
| 289 | tool='compiler_c' |
| 290 | else: |
| 291 | tool=tool.replace('++','xx') |
| 292 | if tooldir: |
| 293 | assert isinstance(tooldir,list) |
| 294 | sys.path=tooldir+sys.path |
| 295 | try: |
| 296 | __import__(tool) |
| 297 | ret=sys.modules[tool] |
| 298 | Context.tools[tool]=ret |
| 299 | return ret |
| 300 | finally: |
| 301 | for d in tooldir: |
| 302 | sys.path.remove(d) |
| 303 | else: |
| 304 | global waf_dir |
| 305 | try: |
| 306 | os.stat(os.path.join(waf_dir,'waflib','extras',tool+'.py')) |
| 307 | except OSError: |
| 308 | try: |
| 309 | os.stat(os.path.join(waf_dir,'waflib','Tools',tool+'.py')) |
| 310 | except OSError: |
| 311 | d=tool |
| 312 | else: |
| 313 | d='waflib.Tools.%s'%tool |
| 314 | else: |
| 315 | d='waflib.extras.%s'%tool |
| 316 | __import__(d) |
| 317 | ret=sys.modules[d] |
| 318 | Context.tools[tool]=ret |
| 319 | return ret |