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