1#!/usr/bin/env python 2import re, os, sys, shutil 3 4print 'loading install' 5 6if os.environ.has_key('PETSC_DIR'): 7 PETSC_DIR = os.environ['PETSC_DIR'] 8else: 9 fd = file(os.path.join('conf','petscvariables')) 10 a = fd.readline() 11 a = fd.readline() 12 PETSC_DIR = a.split('=')[1][0:-1] 13 fd.close() 14 15if os.environ.has_key('PETSC_ARCH'): 16 PETSC_ARCH = os.environ['PETSC_ARCH'] 17else: 18 fd = file(os.path.join('conf','petscvariables')) 19 a = fd.readline() 20 PETSC_ARCH = a.split('=')[1][0:-1] 21 fd.close() 22 23print '*** using PETSC_DIR='+PETSC_DIR+' PETSC_ARCH='+PETSC_ARCH+' ***' 24sys.path.insert(0, os.path.join(PETSC_DIR, 'config')) 25sys.path.insert(0, os.path.join(PETSC_DIR, 'config', 'BuildSystem')) 26 27import script 28 29try: 30 WindowsError 31except NameError: 32 WindowsError = None 33 34class Installer(script.Script): 35 def __init__(self, clArgs = None): 36 import RDict 37 argDB = RDict.RDict(None, None, 0, 0, readonly = True) 38 if os.environ.has_key('PETSC_DIR'): 39 PETSC_DIR = os.environ['PETSC_DIR'] 40 else: 41 fd = file(os.path.join('conf','petscvariables')) 42 a = fd.readline() 43 a = fd.readline() 44 PETSC_DIR = a.split('=')[1][0:-1] 45 fd.close() 46 argDB.saveFilename = os.path.join(PETSC_DIR, PETSC_ARCH, 'conf', 'RDict.db') 47 argDB.load() 48 script.Script.__init__(self, argDB = argDB) 49 if not clArgs is None: self.clArgs = clArgs 50 self.copies = [] 51 return 52 53 def setupHelp(self, help): 54 import nargs 55 script.Script.setupHelp(self, help) 56 help.addArgument('Installer', '-destDir=<path>', nargs.Arg(None, None, 'Destination Directory for install')) 57 return 58 59 60 def setupModules(self): 61 self.setCompilers = self.framework.require('config.setCompilers', None) 62 self.arch = self.framework.require('PETSc.utilities.arch', None) 63 self.petscdir = self.framework.require('PETSc.utilities.petscdir', None) 64 self.makesys = self.framework.require('PETSc.utilities.Make', None) 65 self.compilers = self.framework.require('config.compilers', None) 66 return 67 68 def setup(self): 69 script.Script.setup(self) 70 self.framework = self.loadConfigure() 71 self.setupModules() 72 return 73 74 def setupDirectories(self): 75 self.rootDir = self.petscdir.dir 76 self.destDir = os.path.abspath(self.argDB['destDir']) 77 self.installDir = self.framework.argDB['prefix'] 78 self.arch = self.arch.arch 79 self.rootIncludeDir = os.path.join(self.rootDir, 'include') 80 self.archIncludeDir = os.path.join(self.rootDir, self.arch, 'include') 81 self.rootConfDir = os.path.join(self.rootDir, 'conf') 82 self.archConfDir = os.path.join(self.rootDir, self.arch, 'conf') 83 self.rootBinDir = os.path.join(self.rootDir, 'bin') 84 self.archBinDir = os.path.join(self.rootDir, self.arch, 'bin') 85 self.archLibDir = os.path.join(self.rootDir, self.arch, 'lib') 86 self.destIncludeDir = os.path.join(self.destDir, 'include') 87 self.destConfDir = os.path.join(self.destDir, 'conf') 88 self.destLibDir = os.path.join(self.destDir, 'lib') 89 self.destBinDir = os.path.join(self.destDir, 'bin') 90 self.installIncludeDir = os.path.join(self.installDir, 'include') 91 self.installBinDir = os.path.join(self.installDir, 'bin') 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(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 def installIncludes(self): 148 self.copies.extend(self.copytree(self.rootIncludeDir, self.destIncludeDir)) 149 self.copies.extend(self.copytree(self.archIncludeDir, self.destIncludeDir)) 150 return 151 152 def copyConf(self, src, dst): 153 if os.path.isdir(dst): 154 dst = os.path.join(dst, os.path.basename(src)) 155 lines = [] 156 oldFile = open(src, 'r') 157 for line in oldFile.readlines(): 158 # paths generated by configure could be different link-path than whats used by user, so fix both 159 line = re.sub(re.escape(os.path.join(self.rootDir, self.arch)), self.installDir, line) 160 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, self.arch))), self.installDir, line) 161 line = re.sub(re.escape(os.path.join(self.rootDir, 'bin')), self.installBinDir, line) 162 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'bin'))), self.installBinDir, line) 163 line = re.sub(re.escape(os.path.join(self.rootDir, 'include')), self.installIncludeDir, line) 164 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'include'))), self.installIncludeDir, line) 165 # remove PETSC_DIR/PETSC_ARCH variables from conf-makefiles. They are no longer necessary 166 line = re.sub('\$\{PETSC_DIR\}/\$\{PETSC_ARCH\}', self.installDir, line) 167 line = re.sub('PETSC_ARCH=\$\{PETSC_ARCH\}', '', line) 168 line = re.sub('\$\{PETSC_DIR\}', self.installDir, line) 169 lines.append(line) 170 oldFile.close() 171 newFile = open(dst, 'w') 172 newFile.write(''.join(lines)) 173 newFile.close() 174 shutil.copystat(src, dst) 175 return 176 177 def installConf(self): 178 # rootConfDir can have a duplicate petscvariables - so processing it first removes the appropriate duplicate file. 179 self.copies.extend(self.copytree(self.rootConfDir, self.destConfDir, copyFunc = self.copyConf)) 180 self.copies.extend(self.copytree(self.archConfDir, self.destConfDir)) 181 # Just copyConf() a couple of files manually [as the rest of the files should not be modified] 182 for file in ['petscrules', 'petscvariables']: 183 self.copyConf(os.path.join(self.archConfDir,file),os.path.join(self.destConfDir,file)) 184 return 185 186 def installBin(self): 187 self.copies.extend(self.copytree(self.rootBinDir, self.destBinDir)) 188 self.copies.extend(self.copytree(self.archBinDir, self.destBinDir)) 189 return 190 191 def copyLib(self, src, dst): 192 '''Run ranlib on the destination library if it is an archive. Also run install_name_tool on dylib on Mac''' 193 shutil.copy2(src, dst) 194 if os.path.splitext(dst)[1] == '.'+self.libSuffix: 195 self.executeShellCommand(self.ranlib+' '+dst) 196 if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'): 197 installName = re.sub(self.destDir, self.installDir, dst) 198 self.executeShellCommand('/usr/bin/install_name_tool -id ' + installName + ' ' + dst) 199 return 200 201 def installLib(self): 202 self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib)) 203 return 204 205 def createUninstaller(self): 206 uninstallscript = os.path.join(self.destConfDir, 'uninstall.py') 207 f = open(uninstallscript, 'w') 208 # Could use the Python AST to do this 209 f.write('#!'+sys.executable+'\n') 210 f.write('import os\n') 211 212 f.write('copies = '+re.sub(self.destDir,self.installDir,repr(self.copies))) 213 f.write(''' 214for src, dst in copies: 215 if os.path.exists(dst): 216 os.remove(dst) 217''') 218 f.close() 219 os.chmod(uninstallscript,0744) 220 return 221 222 def outputHelp(self): 223 print '''\ 224==================================== 225Install complete. It is useable with PETSC_DIR=%s [and no more PETSC_ARCH]. 226Now to check if the libraries are working do (in current directory): 227make PETSC_DIR=%s test 228====================================\ 229''' % (self.installDir,self.installDir) 230 return 231 232 def run(self): 233 self.setup() 234 self.setupDirectories() 235 if os.path.exists(self.destDir) and os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)): 236 print '********************************************************************' 237 print 'Install directory is current directory; nothing needs to be done' 238 print '********************************************************************' 239 return 240 print '*** Installing PETSc at',self.destDir, ' ***' 241 if not os.path.exists(self.destDir): 242 try: 243 os.makedirs(self.destDir) 244 except: 245 print '********************************************************************' 246 print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"' 247 print '********************************************************************' 248 return 249 if not os.path.isdir(os.path.realpath(self.destDir)): 250 print '********************************************************************' 251 print 'Specified destDir', self.destDir, 'is not a directory. Cannot proceed!' 252 print '********************************************************************' 253 return 254 if not os.access(self.destDir, os.W_OK): 255 print '********************************************************************' 256 print 'Unable to write to ', self.destDir, 'Perhaps you need to do "sudo make install"' 257 print '********************************************************************' 258 return 259 self.installIncludes() 260 self.installConf() 261 self.installBin() 262 self.installLib() 263 # this file will mess up the make test run since it resets PETSC_ARCH when PETSC_ARCH needs to be null now 264 os.unlink(os.path.join(self.rootDir,'conf','petscvariables')) 265 fd = file(os.path.join(self.rootDir,'conf','petscvariables'),'w') 266 fd.close() 267 # if running as root then change file ownership back to user 268 if os.environ.has_key('SUDO_USER'): 269 os.chown(os.path.join(self.rootDir,'conf','petscvariables'),int(os.environ['SUDO_UID']),int(os.environ['SUDO_GID'])) 270 self.createUninstaller() 271 self.outputHelp() 272 return 273 274if __name__ == '__main__': 275 Installer(sys.argv[1:]).run() 276 # temporary hack - delete log files created by BuildSystem - when 'sudo make install' is invoked 277 delfiles=['RDict.db','RDict.log','build.log','default.log','build.log.bkp','default.log.bkp'] 278 for delfile in delfiles: 279 if os.path.exists(delfile) and (os.stat(delfile).st_uid==0): 280 os.remove(delfile) 281