00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 """
00022 set of tools to output SCons script
00023 """
00024
00025 from slimpy_base.Core.Graph.Builders.SLIMBuilder import SLIMBuilder
00026
00027
00028 class SconsFlow( object ):
00029
00030 def __init__( self, globals ):
00031
00032 self.glob = globals
00033
00034 self.flags = dict( stdin=0, stdout=-1 )
00035
00036 self.targets = set( [] )
00037
00038 self.insource = None
00039
00040 self.outtarget = None
00041
00042 self.sources = set( [] )
00043
00044 def setStdinSrc( self, src ):
00045
00046 if src:
00047
00048 self.insource = str( src )
00049
00050 self.flags.pop( 'stdin', None )
00051
00052 self.sources.discard( src )
00053
00054 def setStdoutTgt( self, tgt ):
00055
00056 if tgt:
00057
00058 self.outtarget = str( tgt )
00059
00060 self.flags.pop( 'stdout', None )
00061
00062 self.targets.discard( tgt )
00063
00064 def setTargets( self, Targets ):
00065
00066 self.targets.update( Targets )
00067 self.targets.discard( self.outtarget )
00068
00069 def setSources( self, sources ):
00070
00071 self.sources.update( sources )
00072
00073 self.sources.discard( self.insource )
00074
00075 def setCommands( self, commlist ):
00076
00077 self.commands = commlist
00078
00079 def __str__( self ):
00080 tgts = self.prosessOut()
00081 srcs = self.prosessIn()
00082 command = self.prosessCommand()
00083 flags = self.prosessFlags()
00084
00085 return "Flow( %(tgts)s , %(srcs)s , %(command)s %(flags)s )\n" %vars()
00086
00087 def prosessCommand( self ):
00088
00089 command = " |\n\t".join( [ com.function.format( com.params, com.kparams ) for com in self.commands] )
00090 command = "\n\t\"\"\"\n\t%(command)s\n\t\"\"\"" %vars()
00091
00092 for i , src in enumerate( self.sources ):
00093
00094 if self.insource:
00095 i += 1
00096 command = command.replace( src, '${SOURCES[%s]}'%i )
00097
00098 for i , tgt in enumerate( self.targets ):
00099
00100 if self.outtarget:
00101 i += 1
00102 command = command.replace( tgt, '${TARGETS[%s]}'%i )
00103 return command
00104
00105 def prosessFlags( self ):
00106
00107 l = lambda k : "%s=%s" %k
00108
00109 flags = self.flags.items()
00110
00111 if flags:
00112 return ","+" , ".join( map( l, flags ) )
00113 else:
00114 return ""
00115
00116 def prosessIn( self ):
00117 if self.insource:
00118 items = [self.insource] + list( self.sources )
00119
00120 itm = "[ '" + "' , '".join( map( str, items ) ) + "' ]"
00121
00122 elif self.sources:
00123 itm = "[ '" + "' , '".join( map( str, self.sources ) ) + "' ]"
00124 else:
00125 itm = 'None'
00126
00127 return itm
00128
00129 def prosessOut( self ):
00130
00131 if self.outtarget:
00132 items = [self.outtarget] + list( self.targets )
00133
00134 itm = "[ '" + "' , '".join( map( str, items ) ) + "' ]"
00135
00136 elif self.targets:
00137 itm = "[ '" + "' , '".join( map( str, self.targets ) ) + "' ]"
00138 else:
00139 itm = 'None'
00140
00141 return itm
00142
00143 class SConsBuilder( SLIMBuilder ):
00144
00145
00146 def __init__( self, g, sources, *targets ):
00147
00148
00149
00150 self.lst = []
00151
00152 for l in self.builder.lst:
00153
00154 sf = SconsFlow( {} )
00155
00156 com = add( [ self.table[node] for node in l['Command'] ] )
00157
00158 sf.setStdoutTgt( com.function.getTarget() )
00159
00160 sf.setStdinSrc( com.function.getTarget() )
00161
00162 sf.setCommands( com.function.getCmnd() )
00163
00164 sf.setSources( set( [str( self.table[node] ) for node in l['Source'] ] ) )
00165
00166 sf.setTargets( set( [str( self.table[node] ) for node in l['Target'] ] ) )
00167
00168 self.lst.append( sf )
00169
00170 def printSCons( self ):
00171
00172 sconstruct = "from rsfproj import *\n\ndef make( ):\n\n\t"
00173
00174 import os
00175
00176 rsfroot = os.path.join( os.environ['RSFROOT'] , 'bin', 'sf' )
00177
00178 curdir = os.path.abspath( os.curdir )
00179
00180 for l in self.lst:
00181
00182
00183 f = str( l )
00184
00185 f = f.replace( '\n', '\n\t' )
00186 f = f.replace( rsfroot , '' )
00187 f = f.replace( curdir +'/', '' )
00188
00189 sconstruct += f
00190
00191 return sconstruct
00192
00193
00194 def add( commands ):
00195 """
00196 add list of commands together using '+'
00197 """
00198 prev = commands.pop( 0 )
00199 while commands:
00200 next = commands.pop( 0 )
00201 prev = prev + next
00202
00203 return prev