00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 from SCons.Script import Mkdir
00020 from os.path import abspath,isdir,join,split,isfile
00021 from repro.functions.reproexports import reproexports
00022 from repro.BookKeeping import ReproProject
00023 import re
00024 import os
00025
00026
00027
00028
00029
00030
00031 def rsf_program_prefix( target, source, env, name ):
00032
00033 omp = env.get('OMP')
00034
00035 if omp is True:
00036 omp = env.WhereIs( 'sfomp' )
00037
00038 prog = env.WhereIs( 'sf' + name )
00039
00040 if not prog:
00041 prog = name
00042
00043 if omp:
00044 prog = "%s %s" %(omp,prog)
00045
00046 return prog
00047
00048
00049
00050
00051
00052 def rush_method( self, target=None, source=None, flow_command=None, *p,**kw ):
00053
00054 return self._Rush( target, source, flow_command=flow_command, *p, **kw )
00055
00056
00057
00058
00059
00060
00061 def get_in_eqs( contents ):
00062
00063 binary_file = None
00064 for line in contents.splitlines():
00065 if "in=" in line:
00066 binary_file = line.split("in=",1)[-1]
00067 binary_file = binary_file.strip()
00068 binary_file = binary_file.strip('"')
00069 return binary_file
00070
00071
00072
00073
00074
00075 def sfbin_method( env, targets, *p, **kw ):
00076
00077 targets = env.Split(targets)
00078
00079 pack = kw.get('rsfpack', env.get('rsfpack') )
00080 datapath = kw.get('DATAPATH', env.get('DATAPATH') )
00081 if pack:
00082 return targets
00083
00084 for target in targets:
00085
00086 if hasattr( target, 'suffix' ) and target.suffix == ".rsf":
00087 binary_part = get_in_eqs( target.get_contents() )
00088
00089 if not binary_part:
00090 binary_part = join(datapath ,str(target) + "@" )
00091 elif not isfile(binary_part) and isfile(target.path):
00092 env.AlwaysBuild( target )
00093 env.SideEffect(binary_part, target)
00094 env.Clean( target, binary_part )
00095 return targets
00096
00097
00098
00099
00100
00101 def rsf_emitter( target, source, env ):
00102
00103 datapath = env.get('DATAPATH','.')
00104 datapath = abspath(datapath)
00105
00106 cwd = os.getcwd()
00107 here = split(cwd)[-1]
00108
00109
00110 reproexports( env )
00111 datapath = join( datapath, "%s-hash-%s/" %(here,hash(cwd)) )
00112 if not isdir(datapath):
00113 if env.GetOption('help') or env.GetOption('clean') or env.GetOption('no_exec'):
00114 pass
00115 else:
00116 env.Execute( Mkdir(datapath) )
00117
00118 env['DATAPATH'] = datapath
00119 env['ENV']['DATAPATH'] = datapath
00120
00121 sfbin_method( env, target )
00122
00123 return target, source
00124
00125
00126
00127
00128
00129 def gen_suffix(env, sources):
00130
00131 suffixes = [src.suffix for src in sources if hasattr(src,'suffix')]
00132 if suffixes:
00133 suffix = suffixes[0]
00134 suffix = env.get('RUSHSUFFIX' , suffix )
00135 return suffix
00136 else:
00137 return ""
00138
00139
00140
00141
00142
00143 def flow_join(flow_command):
00144
00145 if isinstance(flow_command,str):
00146 return flow_command
00147 else:
00148 return " | ".join(flow_command)
00149
00150
00151
00152
00153
00154 def tilda_command_expantion( env, flow_command ):
00155
00156 c = re.compile('~~|~([\w.-]+)')
00157
00158 new_flow_command = []
00159 for flow in flow_command:
00160 all = [mch for mch in c.findall(flow) if mch]
00161
00162 for mch in all:
00163 flow = flow.replace( "~%(mch)s" %vars(), "${_SF('%(mch)s')}" %vars() )
00164
00165 new_flow_command.append(flow)
00166 return new_flow_command
00167
00168
00169
00170
00171
00172
00173 def defaultSF( target, source, env, name ):
00174
00175 return name
00176
00177 class PROG_SUFFIX_GUESS( object ):
00178 def __init__(self, target, source, env):
00179 self.target = target
00180 self.source = source
00181 self.env = env
00182 def __call__(self,name):
00183 SFDict = self.env.get('SF',defaultSF)
00184
00185 if isinstance(SFDict, dict):
00186 SF = SFDict.get( self.target[0].suffix, defaultSF )
00187 else:
00188 SF = SFDict
00189
00190 return SF( self.target, self.source,
00191 self.env, name )
00192
00193
00194
00195
00196
00197 def rush_gen( source, target, env, for_signature ):
00198
00199 flow_command = env.get( 'flow_command' )
00200
00201 if for_signature or env.GetOption("clean") or env.GetOption("no_exec"):
00202 flow_command = flow_command[:]
00203 for idx,flow in enumerate(flow_command):
00204 flow_command[idx] = flow.replace( ".read()", "" )
00205
00206 env['_flow_command'] = flow_command
00207 return ['$RUSHSTR']
00208
00209
00210
00211
00212
00213
00214 def add_to_books( target, source, env, flow_command ):
00215
00216 for flow in flow_command:
00217 try:
00218 command = env.subst(flow.split()[0])
00219
00220 ReproProject.record_flow(env, command, target[0].dir.path )
00221 except:
00222 print "WARNING: not adding command",repr(flow), 'to books'
00223 raise
00224 return
00225
00226
00227
00228
00229
00230 def add_dependancies( env,flow_command):
00231
00232 deps = []
00233 for flow in flow_command:
00234 command = env.subst(flow.split()[0])
00235 command = env.WhereIs(command)
00236 if command:
00237 deps.append( command )
00238 return deps
00239
00240
00241
00242
00243
00244 def RUSH_EMITTER( target, source, env ):
00245
00246 env.Alias('flow', target)
00247
00248 _SF = PROG_SUFFIX_GUESS(target, source, env)
00249 env['_SF'] = _SF
00250
00251 flow_command = env.get( 'flow_command' )
00252 if isinstance(flow_command, str):
00253 flow_command = flow_command.split("|")
00254
00255 flow_command = tilda_command_expantion( env, flow_command )
00256 add_to_books( target, source, env, flow_command)
00257
00258 deps = add_dependancies( env, flow_command )
00259 source.extend(deps)
00260
00261 env['flow_command'] = flow_command
00262
00263 suffixes = [src.suffix for src in source if hasattr(src,'suffix')]
00264 if not suffixes:
00265 suffixes = [tgt.suffix for tgt in target if hasattr(tgt,'suffix')]
00266
00267 for sfx in suffixes:
00268 if sfx in env['RUSH_EMITTERS']:
00269 return env['RUSH_EMITTERS'][sfx]( target, source, env )
00270
00271 return target, source
00272
00273
00274
00275
00276
00277
00278 def rsfprefix( name ):
00279
00280 pass
00281
00282