00001 """
00002 Base rsf converter class
00003 """
00004
00005 __copyright__ = """
00006 Copyright 2008 Sean Ross-Ross
00007 """
00008 __license__ = """
00009 This file is part of SLIMpy .
00010
00011 SLIMpy is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU Lesser General Public License as published by
00013 the Free Software Foundation, either version 3 of the License, or
00014 (at your option) any later version.
00015
00016 SLIMpy is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU Lesser General Public License for more details.
00020
00021 You should have received a copy of the GNU Lesser General Public License
00022 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025
00026 from slimpy_base.Core.Command.Converter import Converter
00027 from slimpy_base.Core.Command.Drivers.ooc_driver import OutOfCoreDriver
00028 from slimpy_base.api.Plugins.slim2rsf.AddComand import rsfAddCommands
00029
00030 from os.path import join, isfile, isabs, normpath, abspath
00031 from stat import S_IMODE,ST_MODE
00032 from os import pathsep ,stat, environ
00033 from string import Template
00034 from slimpy_base.Environment.InstanceManager import InstanceManager
00035
00036
00037
00038 class sfConverter( Converter ):
00039 """
00040 Base rsf converter class used when specific
00041 command can not be found
00042 @postcondition: tansform returns voidspace
00043 """
00044 env = InstanceManager()
00045
00046
00047 @classmethod
00048 def place_adder( cls, command ):
00049 command.adder = rsfAddCommands()
00050 return command
00051
00052 @classmethod
00053 def map( cls, source, command ):
00054
00055 cmnd = cls.default_function( command )
00056
00057 return cls.pack( cmnd )
00058
00059 @classmethod
00060 def guess_exe_path(cls, name, error=0):
00061
00062 if name.startswith('sf'):
00063 sfname = name
00064 else:
00065 sfname = "sf"+name
00066 slimvars = cls.env['slimvars']
00067 sfname = join( slimvars['RSFBIN'] , sfname )
00068
00069 if is_executable(name):
00070 return abspath( name )
00071
00072 elif is_executable(sfname):
00073 return sfname
00074
00075 elif WhereIs(name):
00076 return WhereIs(name)
00077
00078 elif error:
00079 raise EnvironmentError( "No files '%(name1)s' or '%(name2)s'" %vars() )
00080 else:
00081 return name
00082
00083
00084
00085
00086
00087 @classmethod
00088 def default_function( cls, command, name=None ):
00089 slimvars = cls.env['slimvars']
00090 if name is None:
00091 name = command.tag
00092 else:
00093 name = str( name )
00094
00095 exe = cls.guess_exe_path(name, error=slimvars['check_path'] )
00096
00097 command.func = OutOfCoreDriver( exe )
00098 command.adder = rsfAddCommands()
00099
00100 return command
00101
00102 @classmethod
00103 def mpi_function( cls, command, name=None, num_proc='all' ):
00104 slimvars = cls.env['slimvars']
00105 MPICOM = Template( slimvars['MPIFLAGS'] )
00106
00107 if name is None:
00108 name1 = command.tag
00109 else:
00110 name1 = name
00111
00112 name2 = join( slimvars['RSFBIN'] , 'xsf'+name1 )
00113
00114 if isfile( name1 ):
00115 if isabs( name1 ):
00116 command_name = name1
00117 else:
00118 command_name = join( ".", name1 )
00119 else:
00120 command_name = name2
00121
00122 if slimvars['check_path'] and not isfile( command_name ):
00123 raise EnvironmentError( "No files '%(name1)s' or '%(name2)s'" %vars() )
00124
00125
00126
00127
00128 if "$NODEFILE" in slimvars['MPIFLAGS'] and slimvars['NODEFILE'] is None:
00129 raise Exception("SLIMpy detected an mpi process to be run needs a nodefile: "
00130 "please set 'NODEFILE' global variable")
00131 mpi_cmd = MPICOM.substitute( **slimvars.slimGlobals )
00132
00133 cmd = "%(mpi_cmd)s %(command_name)s" % vars()
00134 command.func = OutOfCoreDriver( cmd )
00135 command.adder = rsfAddCommands()
00136
00137 command.command_type = 'multi_process'
00138 command.num_proc = num_proc
00139
00140 return command
00141 @classmethod
00142 def constr( cls, command, *Spaces ):
00143 return True
00144
00145
00146 def WhereIs( file ):
00147
00148 path = environ['PATH']
00149 for d in path.split( pathsep ):
00150 f = join(d, file)
00151 if is_executable(f):
00152 return normpath( f )
00153 else:
00154 continue
00155
00156 return None
00157
00158 def is_executable( f ):
00159 if isfile(f):
00160 try:
00161 st = stat(f)
00162 except OSError:
00163 return False
00164
00165 if S_IMODE( st[ ST_MODE] ) & 0111:
00166 return True
00167
00168 return False
00169
00170