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 self.copies = [] 50 return 51 52 def setupHelp(self, help): 53 import nargs 54 script.Script.setupHelp(self, help) 55 help.addArgument('Installer', '-destDir=<path>', nargs.Arg(None, None, 'Destination Directory for install')) 56 return 57 58 59 def setupModules(self): 60 self.setCompilers = self.framework.require('config.setCompilers', None) 61 self.arch = self.framework.require('PETSc.utilities.arch', None) 62 self.petscdir = self.framework.require('PETSc.utilities.petscdir', None) 63 self.makesys = self.framework.require('PETSc.utilities.Make', None) 64 self.compilers = self.framework.require('config.compilers', None) 65 return 66 67 def setup(self): 68 script.Script.setup(self) 69 self.framework = self.loadConfigure() 70 self.setupModules() 71 return 72 73 def setupDirectories(self): 74 self.rootDir = self.petscdir.dir 75 self.destDir = os.path.abspath(self.argDB['destDir']) 76 self.installDir = self.framework.argDB['prefix'] 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.installConfDir = os.path.join(self.installDir, 'conf') 91 self.installLibDir = os.path.join(self.installDir, 'lib') 92 self.installBinDir = os.path.join(self.installDir, 'bin') 93 94 self.make = self.makesys.make+' '+self.makesys.flags 95 self.ranlib = self.compilers.RANLIB 96 self.libSuffix = self.compilers.AR_LIB_SUFFIX 97 return 98 99 def copytree(self, src, dst, symlinks = False, copyFunc = shutil.copy2): 100 """Recursively copy a directory tree using copyFunc, which defaults to shutil.copy2(). 101 102 The destination directory must not already exist. 103 If exception(s) occur, an shutil.Error is raised with a list of reasons. 104 105 If the optional symlinks flag is true, symbolic links in the 106 source tree result in symbolic links in the destination tree; if 107 it is false, the contents of the files pointed to by symbolic 108 links are copied. 109 """ 110 copies = [] 111 names = os.listdir(src) 112 if not os.path.exists(dst): 113 os.makedirs(dst) 114 elif not os.path.isdir(dst): 115 raise shutil.Error, 'Destination is not a directory' 116 errors = [] 117 for name in names: 118 srcname = os.path.join(src, name) 119 dstname = os.path.join(dst, name) 120 try: 121 if symlinks and os.path.islink(srcname): 122 linkto = os.readlink(srcname) 123 os.symlink(linkto, dstname) 124 elif os.path.isdir(srcname): 125 copies.extend(self.copytree(srcname, dstname, symlinks)) 126 else: 127 copyFunc(srcname, dstname) 128 copies.append((srcname, dstname)) 129 # XXX What about devices, sockets etc.? 130 except (IOError, os.error), why: 131 errors.append((srcname, dstname, str(why))) 132 # catch the Error from the recursive copytree so that we can 133 # continue with other files 134 except shutil.Error, err: 135 errors.extend(err.args[0]) 136 try: 137 shutil.copystat(src, dst) 138 except OSError, e: 139 if WindowsError is not None and isinstance(e, WindowsError): 140 # Copying file access times may fail on Windows 141 pass 142 else: 143 errors.extend((src, dst, str(e))) 144 if errors: 145 raise shutil.Error, errors 146 return copies 147 148 def installIncludes(self): 149 self.copies.extend(self.copytree(self.rootIncludeDir, self.destIncludeDir)) 150 self.copies.extend(self.copytree(self.archIncludeDir, self.destIncludeDir)) 151 return 152 153 def copyConf(self, src, dst): 154 if os.path.isdir(dst): 155 dst = os.path.join(dst, os.path.basename(src)) 156 lines = [] 157 oldFile = open(src, 'r') 158 for line in oldFile.readlines(): 159 # paths generated by configure could be different link-path than whats used by user, so fix both 160 line = re.sub(re.escape(os.path.join(self.rootDir, self.arch)), self.installDir, line) 161 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, self.arch))), self.installDir, line) 162 line = re.sub(re.escape(os.path.join(self.rootDir, 'bin')), self.installBinDir, line) 163 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'bin'))), self.installBinDir, line) 164 line = re.sub(re.escape(os.path.join(self.rootDir, 'include')), self.installIncludeDir, line) 165 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'include'))), self.installIncludeDir, line) 166 # remove PETSC_DIR/PETSC_ARCH variables from conf-makefiles. They are no longer necessary 167 line = re.sub('\$\{PETSC_DIR\}/\$\{PETSC_ARCH\}', self.installDir, line) 168 line = re.sub('PETSC_ARCH=\$\{PETSC_ARCH\}', '', line) 169 line = re.sub('\$\{PETSC_DIR\}', self.installDir, line) 170 lines.append(line) 171 oldFile.close() 172 newFile = open(dst, 'w') 173 newFile.write(''.join(lines)) 174 newFile.close() 175 shutil.copystat(src, dst) 176 return 177 178 def installConf(self): 179 # rootConfDir can have a duplicate petscvariables - so processing it first removes the appropriate duplicate file. 180 self.copies.extend(self.copytree(self.rootConfDir, self.destConfDir, copyFunc = self.copyConf)) 181 self.copies.extend(self.copytree(self.archConfDir, self.destConfDir)) 182 # Just copyConf() a couple of files manually [as the rest of the files should not be modified] 183 for file in ['petscrules', 'petscvariables']: 184 self.copyConf(os.path.join(self.archConfDir,file),os.path.join(self.destConfDir,file)) 185 return 186 187 def installBin(self): 188 self.copies.extend(self.copytree(self.rootBinDir, self.destBinDir)) 189 self.copies.extend(self.copytree(self.archBinDir, self.destBinDir)) 190 return 191 192 def copyLib(self, src, dst): 193 '''Run ranlib on the destination library if it is an archive. Also run install_name_tool on dylib on Mac''' 194 shutil.copy2(src, dst) 195 if os.path.splitext(dst)[1] == '.'+self.libSuffix: 196 self.executeShellCommand(self.ranlib+' '+dst) 197 if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'): 198 installName = re.sub(self.destDir, self.installDir, dst) 199 self.executeShellCommand('/usr/bin/install_name_tool -id ' + installName + ' ' + dst) 200 return 201 202 def installLib(self): 203 self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib)) 204 return 205 206 def createUninstaller(self): 207 uninstallscript = os.path.join(self.destConfDir, 'uninstall.py') 208 f = open(uninstallscript, 'w') 209 # Could use the Python AST to do this 210 f.write('#!'+sys.executable+'\n') 211 f.write('import os\n') 212 213 f.write('copies = '+re.sub(self.destDir,self.installDir,repr(self.copies))) 214 f.write(''' 215for src, dst in copies: 216 if os.path.exists(dst): 217 os.remove(dst) 218''') 219 f.close() 220 os.chmod(uninstallscript,0744) 221 return 222 223 def outputHelp(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 run(self): 234 self.setup() 235 self.setupDirectories() 236 if os.path.exists(self.destDir) and os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)): 237 print '********************************************************************' 238 print 'Install directory is current directory; nothing needs to be done' 239 print '********************************************************************' 240 return 241 print '*** Installing PETSc at',self.destDir, ' ***' 242 if not os.path.exists(self.destDir): 243 try: 244 os.makedirs(self.destDir) 245 except: 246 print '********************************************************************' 247 print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"' 248 print '********************************************************************' 249 return 250 if not os.path.isdir(os.path.realpath(self.destDir)): 251 print '********************************************************************' 252 print 'Specified destDir', self.destDir, 'is not a directory. Cannot proceed!' 253 print '********************************************************************' 254 return 255 if not os.access(self.destDir, os.W_OK): 256 print '********************************************************************' 257 print 'Unable to write to ', self.destDir, 'Perhaps you need to do "sudo make install"' 258 print '********************************************************************' 259 return 260 self.installIncludes() 261 self.installConf() 262 self.installBin() 263 self.installLib() 264 # this file will mess up the make test run since it resets PETSC_ARCH when PETSC_ARCH needs to be null now 265 os.unlink(os.path.join(self.rootDir,'conf','petscvariables')) 266 fd = file(os.path.join('conf','petscvariables'),'w') 267 fd.close() 268 # if running as root then change file ownership back to user 269 if os.environ.has_key('SUDO_USER'): 270 os.chown(os.path.join(self.rootDir,'conf','petscvariables'),int(os.environ['SUDO_UID']),int(os.environ['SUDO_GID'])) 271 self.createUninstaller() 272 self.outputHelp() 273 return 274 275if __name__ == '__main__': 276 Installer(sys.argv[1:]).run() 277 # temporary hack - delete log files created by BuildSystem - when 'sudo make install' is invoked 278 delfiles=['RDict.db','RDict.log','build.log','default.log','build.log.bkp','default.log.bkp'] 279 for delfile in delfiles: 280 if os.path.exists(delfile) and (os.stat(delfile).st_uid==0): 281 os.remove(delfile) 282