#NEWLINE# def exec_command( task , cmd , **kw ) : #NEWLINE# #INDENT# '\n    helper function to:\n     - run a command\n     - log stderr and stdout into worch_<taskname>.log.txt\n     - printout the content of that file when the command fails\n    ' #NEWLINE# bld = task . generator . bld #NEWLINE# cwd = getattr ( task , 'cwd' , bld . out_dir ) #NEWLINE# msg . debug ( ( 'orch: exec command: %s: "%s" in %s' % ( task . name , cmd , cwd ) ) ) #NEWLINE# if ( not osp . exists ( cwd ) ) : #NEWLINE# #INDENT# os . makedirs ( cwd ) #NEWLINE# #UNINDENT# log_dir = bld . root . make_node ( osp . join ( bld . out_dir , 'logs' ) ) #NEWLINE# log_dir . mkdir ( ) #NEWLINE# fnode = log_dir . make_node ( ( 'worch_%s.log.txt' % task . name ) ) #NEWLINE# flog = open ( fnode . abspath ( ) , 'w' ) #NEWLINE# cmd_dict = dict ( kw ) #NEWLINE# env = kw . get ( 'env' , task . env . env ) #NEWLINE# cmd_dict . update ( { #NEWLINE# #INDENT# 'cwd' : cwd , #NEWLINE# 'env' : env , #NEWLINE# 'stdout' : flog , #NEWLINE# 'stderr' : flog , #NEWLINE# #UNINDENT# } ) #NEWLINE# try : #NEWLINE# #INDENT# pdict = bld . env [ 'orch_package_dict' ] [ task . name . split ( '_' , 1 ) [ 0 ] ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# pdict = dict ( ) #NEWLINE# #UNINDENT# flog . write ( ( 'WORCH CMD: %s\n' % cmd ) ) #NEWLINE# flog . write ( ( 'WORCH CWD: %s\n' % cwd ) ) #NEWLINE# flog . write ( ( 'WORCH TSK: %s\n' % str ( task ) ) ) #NEWLINE# flog . write ( pprint . pformat ( task . __dict__ , indent = 2 , depth = 2 ) ) #NEWLINE# flog . write ( '\n' ) #NEWLINE# if pdict : #NEWLINE# #INDENT# flog . write ( 'WORCH PKG:\n' ) #NEWLINE# flog . write ( pprint . pformat ( pdict , indent = 2 , depth = 2 ) ) #NEWLINE# #UNINDENT# flog . write ( ( '\nWORCH ENV:\n\t%s' % '\n\t' . join ( [ ( '%s = %s' % kv ) for kv in sorted ( env . items ( ) ) ] ) ) ) #NEWLINE# flog . write ( '\nWORCH command output:\n' ) #NEWLINE# flog . flush ( ) #NEWLINE# try : #NEWLINE# #INDENT# ret = task . exec_command ( cmd , ** cmd_dict ) #NEWLINE# #UNINDENT# except KeyboardInterrupt : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# except WafError : #NEWLINE# #INDENT# msg . error ( ( 'Command failed with WafError: %s in %s' % ( cmd , cwd ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# flog . close ( ) #NEWLINE# #UNINDENT# if ( msg . verbose and ( ret == 0 ) and ( 'orchstep' in msg . zones ) ) : #NEWLINE# #INDENT# with open ( flog . abspath ( ) ) as f : #NEWLINE# #INDENT# msg . pprint ( 'NORMAL' , ( 'orchstep: %s (%s)\n%s' % ( task . name , flog . name , '' . join ( f . readlines ( ) ) ) ) ) #NEWLINE# pass #NEWLINE# #UNINDENT# pass #NEWLINE# #UNINDENT# if ( ret == 0 ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# msg . error ( ( 'command failed with code %s, log in %s' % ( ret , flog . name ) ) ) #NEWLINE# repo = osp . join ( cwd , ( 'worch_%s.repo.sh' % task . name ) ) #NEWLINE# with open ( repo , 'w' ) as fp : #NEWLINE# #INDENT# fp . write ( '#!/bin/bash\n' ) #NEWLINE# fp . write ( ( 'cd %s\n' % cwd ) ) #NEWLINE# for ( var , val ) in sorted ( env . items ( ) ) : #NEWLINE# #INDENT# if val . startswith ( '()' ) : #NEWLINE# #INDENT# fp . write ( ( '%s %s\n' % ( var , val ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fp . write ( ( 'export %s="%s"\n' % ( var , val ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# fp . write ( ( '\n\n%s\n' % cmd ) ) #NEWLINE# #UNINDENT# msg . error ( ( 'reproduce with: %s' % repo ) ) #NEWLINE# return ret