1#!/usr/bin/env python 2import re, os, sys, shutil 3 4if os.environ.has_key('PETSC_DIR'): 5 PETSC_DIR = os.environ['PETSC_DIR'] 6else: 7 fd = file(os.path.join('conf','petscvariables')) 8 a = fd.readline() 9 a = fd.readline() 10 PETSC_DIR = a.split('=')[1][0:-1] 11 fd.close() 12 13if os.environ.has_key('PETSC_ARCH'): 14 PETSC_ARCH = os.environ['PETSC_ARCH'] 15else: 16 fd = file(os.path.join('conf','petscvariables')) 17 a = fd.readline() 18 PETSC_ARCH = a.split('=')[1][0:-1] 19 fd.close() 20 21print '*** using PETSC_DIR='+PETSC_DIR+' PETSC_ARCH='+PETSC_ARCH+' ***' 22sys.path.insert(0, os.path.join(PETSC_DIR, 'config')) 23sys.path.insert(0, os.path.join(PETSC_DIR, 'config', 'BuildSystem')) 24 25import script 26 27try: 28 WindowsError 29except NameError: 30 WindowsError = None 31 32class Installer(script.Script): 33 def __init__(self, clArgs = None): 34 import RDict 35 argDB = RDict.RDict(None, None, 0, 0, readonly = True) 36 if os.environ.has_key('PETSC_DIR'): 37 PETSC_DIR = os.environ['PETSC_DIR'] 38 else: 39 fd = file(os.path.join('conf','petscvariables')) 40 a = fd.readline() 41 a = fd.readline() 42 PETSC_DIR = a.split('=')[1][0:-1] 43 fd.close() 44 argDB.saveFilename = os.path.join(PETSC_DIR, PETSC_ARCH, 'conf', 'RDict.db') 45 argDB.load() 46 script.Script.__init__(self, argDB = argDB) 47 if not clArgs is None: self.clArgs = clArgs 48 self.copies = [] 49 return 50 51 def setupHelp(self, help): 52 import nargs 53 script.Script.setupHelp(self, help) 54 help.addArgument('Installer', '-destDir=<path>', nargs.Arg(None, None, 'Destination Directory for install')) 55 return 56 57 58 def setupModules(self): 59 self.setCompilers = self.framework.require('config.setCompilers', None) 60 self.arch = self.framework.require('PETSc.utilities.arch', None) 61 self.petscdir = self.framework.require('PETSc.utilities.petscdir', None) 62 self.makesys = self.framework.require('config.programs', None) 63 self.compilers = self.framework.require('config.compilers', None) 64 return 65 66 def setup(self): 67 script.Script.setup(self) 68 self.framework = self.loadConfigure() 69 self.setupModules() 70 return 71 72 def setupDirectories(self): 73 self.rootDir = self.petscdir.dir 74 self.destDir = os.path.abspath(self.argDB['destDir']) 75 self.installDir = self.framework.argDB['prefix'] 76 print self.installDir 77 self.arch = self.arch.arch 78 self.rootIncludeDir = os.path.join(self.rootDir, 'include') 79 self.archIncludeDir = os.path.join(self.rootDir, self.arch, 'include') 80 self.rootConfDir = os.path.join(self.rootDir, 'conf') 81 self.archConfDir = os.path.join(self.rootDir, self.arch, 'conf') 82 self.rootBinDir = os.path.join(self.rootDir, 'bin') 83 self.archBinDir = os.path.join(self.rootDir, self.arch, 'bin') 84 self.archLibDir = os.path.join(self.rootDir, self.arch, 'lib') 85 self.destIncludeDir = os.path.join(self.destDir, 'include') 86 self.destConfDir = os.path.join(self.destDir, 'conf') 87 self.destLibDir = os.path.join(self.destDir, 'lib') 88 self.destBinDir = os.path.join(self.destDir, 'bin') 89 self.installIncludeDir = os.path.join(self.installDir, 'include') 90 self.installBinDir = os.path.join(self.installDir, 'bin') 91 92 self.make = self.makesys.make+' '+self.makesys.flags 93 self.ranlib = self.compilers.RANLIB 94 self.libSuffix = self.compilers.AR_LIB_SUFFIX 95 return 96 97 def copytree(self, src, dst, symlinks = False, copyFunc = shutil.copy2): 98 """Recursively copy a directory tree using copyFunc, which defaults to shutil.copy2(). 99 100 The destination directory must not already exist. 101 If exception(s) occur, an shutil.Error is raised with a list of reasons. 102 103 If the optional symlinks flag is true, symbolic links in the 104 source tree result in symbolic links in the destination tree; if 105 it is false, the contents of the files pointed to by symbolic 106 links are copied. 107 """ 108 copies = [] 109 names = os.listdir(src) 110 if not os.path.exists(dst): 111 os.makedirs(dst) 112 elif not os.path.isdir(dst): 113 raise shutil.Error, 'Destination is not a directory' 114 errors = [] 115 for name in names: 116 srcname = os.path.join(src, name) 117 dstname = os.path.join(dst, name) 118 try: 119 if symlinks and os.path.islink(srcname): 120 linkto = os.readlink(srcname) 121 os.symlink(linkto, dstname) 122 elif os.path.isdir(srcname): 123 copies.extend(self.copytree(srcname, dstname, symlinks)) 124 else: 125 copyFunc(srcname, dstname) 126 copies.append((srcname, dstname)) 127 # XXX What about devices, sockets etc.? 128 except (IOError, os.error), why: 129 errors.append((srcname, dstname, str(why))) 130 # catch the Error from the recursive copytree so that we can 131 # continue with other files 132 except shutil.Error, err: 133 errors.extend((srcname,dstname,str(err.args[0]))) 134 try: 135 shutil.copystat(src, dst) 136 except OSError, e: 137 if WindowsError is not None and isinstance(e, WindowsError): 138 # Copying file access times may fail on Windows 139 pass 140 else: 141 errors.extend((src, dst, str(e))) 142 if errors: 143 raise shutil.Error, errors 144 return copies 145 146 147 def fixConfFile(self, src): 148 lines = [] 149 oldFile = open(src, 'r') 150 for line in oldFile.readlines(): 151 # paths generated by configure could be different link-path than whats used by user, so fix both 152 line = re.sub(re.escape(os.path.join(self.rootDir, self.arch)), self.installDir, line) 153 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, self.arch))), self.installDir, line) 154 line = re.sub(re.escape(os.path.join(self.rootDir, 'bin')), self.installBinDir, line) 155 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'bin'))), self.installBinDir, line) 156 line = re.sub(re.escape(os.path.join(self.rootDir, 'include')), self.installIncludeDir, line) 157 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'include'))), self.installIncludeDir, line) 158 # remove PETSC_DIR/PETSC_ARCH variables from conf-makefiles. They are no longer necessary 159 line = re.sub('\$\{PETSC_DIR\}/\$\{PETSC_ARCH\}', self.installDir, line) 160 line = re.sub('PETSC_ARCH=\$\{PETSC_ARCH\}', '', line) 161 line = re.sub('\$\{PETSC_DIR\}', self.installDir, line) 162 lines.append(line) 163 oldFile.close() 164 newFile = open(src, 'w') 165 newFile.write(''.join(lines)) 166 newFile.close() 167 return 168 169 def fixConf(self): 170 import shutil 171 # copy standard rules and variables file so that we can change them in place 172 for file in ['rules', 'variables']: 173 shutil.copy2(os.path.join(self.rootConfDir,file),os.path.join(self.archConfDir,file)) 174 for file in ['rules', 'variables','petscrules', 'petscvariables']: 175 self.fixConfFile(os.path.join(self.archConfDir,file)) 176 177 def createUninstaller(self): 178 uninstallscript = os.path.join(self.archConfDir, 'uninstall.py') 179 f = open(uninstallscript, 'w') 180 # Could use the Python AST to do this 181 f.write('#!'+sys.executable+'\n') 182 f.write('import os\n') 183 184 f.write('copies = '+re.sub(self.destDir,self.installDir,repr(self.copies))) 185 f.write(''' 186for src, dst in copies: 187 if os.path.exists(dst): 188 os.remove(dst) 189''') 190 f.close() 191 os.chmod(uninstallscript,0744) 192 return 193 194 def installIncludes(self): 195 self.copies.extend(self.copytree(self.rootIncludeDir, self.destIncludeDir)) 196 self.copies.extend(self.copytree(self.archIncludeDir, self.destIncludeDir)) 197 return 198 199 def installConf(self): 200 self.copies.extend(self.copytree(self.rootConfDir, self.destConfDir)) 201 self.copies.extend(self.copytree(self.archConfDir, self.destConfDir)) 202 203 def installBin(self): 204 self.copies.extend(self.copytree(self.rootBinDir, self.destBinDir)) 205 self.copies.extend(self.copytree(self.archBinDir, self.destBinDir)) 206 return 207 208 def copyLib(self, src, dst): 209 '''Run ranlib on the destination library if it is an archive. Also run install_name_tool on dylib on Mac''' 210 shutil.copy2(src, dst) 211 if os.path.splitext(dst)[1] == '.'+self.libSuffix: 212 self.executeShellCommand(self.ranlib+' '+dst) 213 if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'): 214 installName = re.sub(self.destDir, self.installDir, dst) 215 self.executeShellCommand('/usr/bin/install_name_tool -id ' + installName + ' ' + dst) 216 return 217 218 def installLib(self): 219 self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib)) 220 return 221 222 223 def outputDone(self): 224 print '''\ 225==================================== 226Install complete. It is useable with PETSC_DIR=%s [and no more PETSC_ARCH]. 227Now to check if the libraries are working do (in current directory): 228make PETSC_DIR=%s test 229====================================\ 230''' % (self.installDir,self.installDir) 231 return 232 233 def runfix(self): 234 self.setup() 235 self.setupDirectories() 236 self.createUninstaller() 237 self.fixConf() 238 239 def runcopy(self): 240 if os.path.exists(self.destDir) and os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)): 241 print '********************************************************************' 242 print 'Install directory is current directory; nothing needs to be done' 243 print '********************************************************************' 244 return 245 print '*** Installing PETSc at',self.destDir, ' ***' 246 if not os.path.exists(self.destDir): 247 try: 248 os.makedirs(self.destDir) 249 except: 250 print '********************************************************************' 251 print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"' 252 print '********************************************************************' 253 return 254 if not os.path.isdir(os.path.realpath(self.destDir)): 255 print '********************************************************************' 256 print 'Specified destDir', self.destDir, 'is not a directory. Cannot proceed!' 257 print '********************************************************************' 258 return 259 if not os.access(self.destDir, os.W_OK): 260 print '********************************************************************' 261 print 'Unable to write to ', self.destDir, 'Perhaps you need to do "sudo make install"' 262 print '********************************************************************' 263 return 264 265 self.installIncludes() 266 self.installConf() 267 self.installBin() 268 self.installLib() 269 self.outputDone() 270 271 return 272 273 def run(self): 274 self.runfix() 275 self.runcopy() 276 277if __name__ == '__main__': 278 Installer(sys.argv[1:]).run() 279 # temporary hack - delete log files created by BuildSystem - when 'sudo make install' is invoked 280 delfiles=['RDict.db','RDict.log','build.log','default.log','build.log.bkp','default.log.bkp'] 281 for delfile in delfiles: 282 if os.path.exists(delfile) and (os.stat(delfile).st_uid==0): 283 os.remove(delfile) 284