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 argDB.saveFilename = os.path.join(PETSC_DIR, PETSC_ARCH, 'conf', 'RDict.db') 37 argDB.load() 38 script.Script.__init__(self, argDB = argDB) 39 if not clArgs is None: self.clArgs = clArgs 40 self.copies = [] 41 return 42 43 def setupHelp(self, help): 44 import nargs 45 script.Script.setupHelp(self, help) 46 help.addArgument('Installer', '-destDir=<path>', nargs.Arg(None, None, 'Destination Directory for install')) 47 return 48 49 50 def setupModules(self): 51 self.setCompilers = self.framework.require('config.setCompilers', None) 52 self.arch = self.framework.require('PETSc.utilities.arch', None) 53 self.petscdir = self.framework.require('PETSc.utilities.petscdir', None) 54 self.makesys = self.framework.require('config.programs', None) 55 self.compilers = self.framework.require('config.compilers', None) 56 return 57 58 def setup(self): 59 script.Script.setup(self) 60 self.framework = self.loadConfigure() 61 self.setupModules() 62 return 63 64 def setupDirectories(self): 65 self.rootDir = self.petscdir.dir 66 self.destDir = os.path.abspath(self.argDB['destDir']) 67 self.installDir = self.framework.argDB['prefix'] 68 self.arch = self.arch.arch 69 self.rootIncludeDir = os.path.join(self.rootDir, 'include') 70 self.archIncludeDir = os.path.join(self.rootDir, self.arch, 'include') 71 self.rootConfDir = os.path.join(self.rootDir, 'conf') 72 self.archConfDir = os.path.join(self.rootDir, self.arch, 'conf') 73 self.rootBinDir = os.path.join(self.rootDir, 'bin') 74 self.archBinDir = os.path.join(self.rootDir, self.arch, 'bin') 75 self.archLibDir = os.path.join(self.rootDir, self.arch, 'lib') 76 self.destIncludeDir = os.path.join(self.destDir, 'include') 77 self.destConfDir = os.path.join(self.destDir, 'conf') 78 self.destLibDir = os.path.join(self.destDir, 'lib') 79 self.destBinDir = os.path.join(self.destDir, 'bin') 80 self.installIncludeDir = os.path.join(self.installDir, 'include') 81 self.installBinDir = os.path.join(self.installDir, 'bin') 82 self.rootShareDir = os.path.join(self.rootDir, 'share') 83 self.destShareDir = os.path.join(self.destDir, 'share') 84 85 self.make = self.makesys.make+' '+self.makesys.flags 86 self.ranlib = self.compilers.RANLIB 87 self.arLibSuffix = self.compilers.AR_LIB_SUFFIX 88 return 89 90 def checkPrefix(self): 91 if not self.installDir: 92 print '********************************************************************' 93 print 'PETSc is built without prefix option. So "make install" is not appropriate.' 94 print 'If you need a prefix install of PETSc - rerun configure with --prefix option.' 95 print '********************************************************************' 96 sys.exit(1) 97 return 98 99 def checkDestdir(self): 100 if os.path.exists(self.destDir): 101 if os.path.samefile(self.destDir, self.rootDir): 102 print '********************************************************************' 103 print 'Incorrect prefix usage. Specified destDir same as current PETSC_DIR' 104 print '********************************************************************' 105 sys.exit(1) 106 if os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)): 107 print '********************************************************************' 108 print 'Incorrect prefix usage. Specified destDir same as current PETSC_DIR/PETSC_ARCH' 109 print '********************************************************************' 110 sys.exit(1) 111 if not os.path.isdir(os.path.realpath(self.destDir)): 112 print '********************************************************************' 113 print 'Specified destDir', self.destDir, 'is not a directory. Cannot proceed!' 114 print '********************************************************************' 115 sys.exit(1) 116 if not os.access(self.destDir, os.W_OK): 117 print '********************************************************************' 118 print 'Unable to write to ', self.destDir, 'Perhaps you need to do "sudo make install"' 119 print '********************************************************************' 120 sys.exit(1) 121 return 122 123 def copytree(self, src, dst, symlinks = False, copyFunc = shutil.copy2): 124 """Recursively copy a directory tree using copyFunc, which defaults to shutil.copy2(). 125 126 The destination directory must not already exist. 127 If exception(s) occur, an shutil.Error is raised with a list of reasons. 128 129 If the optional symlinks flag is true, symbolic links in the 130 source tree result in symbolic links in the destination tree; if 131 it is false, the contents of the files pointed to by symbolic 132 links are copied. 133 """ 134 copies = [] 135 names = os.listdir(src) 136 if not os.path.exists(dst): 137 os.makedirs(dst) 138 elif not os.path.isdir(dst): 139 raise shutil.Error, 'Destination is not a directory' 140 errors = [] 141 for name in names: 142 srcname = os.path.join(src, name) 143 dstname = os.path.join(dst, name) 144 try: 145 if symlinks and os.path.islink(srcname): 146 linkto = os.readlink(srcname) 147 os.symlink(linkto, dstname) 148 elif os.path.isdir(srcname): 149 copies.extend(self.copytree(srcname, dstname, symlinks)) 150 else: 151 copyFunc(srcname, dstname) 152 copies.append((srcname, dstname)) 153 # XXX What about devices, sockets etc.? 154 except (IOError, os.error), why: 155 errors.append((srcname, dstname, str(why))) 156 # catch the Error from the recursive copytree so that we can 157 # continue with other files 158 except shutil.Error, err: 159 errors.extend((srcname,dstname,str(err.args[0]))) 160 try: 161 shutil.copystat(src, dst) 162 except OSError, e: 163 if WindowsError is not None and isinstance(e, WindowsError): 164 # Copying file access times may fail on Windows 165 pass 166 else: 167 errors.extend((src, dst, str(e))) 168 if errors: 169 raise shutil.Error, errors 170 return copies 171 172 173 def fixConfFile(self, src): 174 lines = [] 175 oldFile = open(src, 'r') 176 for line in oldFile.readlines(): 177 # paths generated by configure could be different link-path than whats used by user, so fix both 178 line = re.sub(re.escape(os.path.join(self.rootDir, self.arch)), self.installDir, line) 179 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, self.arch))), self.installDir, line) 180 line = re.sub(re.escape(os.path.join(self.rootDir, 'bin')), self.installBinDir, line) 181 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'bin'))), self.installBinDir, line) 182 line = re.sub(re.escape(os.path.join(self.rootDir, 'include')), self.installIncludeDir, line) 183 line = re.sub(re.escape(os.path.realpath(os.path.join(self.rootDir, 'include'))), self.installIncludeDir, line) 184 # remove PETSC_DIR/PETSC_ARCH variables from conf-makefiles. They are no longer necessary 185 line = re.sub('\$\{PETSC_DIR\}/\$\{PETSC_ARCH\}', self.installDir, line) 186 line = re.sub('PETSC_ARCH=\$\{PETSC_ARCH\}', '', line) 187 line = re.sub('\$\{PETSC_DIR\}', self.installDir, line) 188 lines.append(line) 189 oldFile.close() 190 newFile = open(src, 'w') 191 newFile.write(''.join(lines)) 192 newFile.close() 193 return 194 195 def fixConf(self): 196 import shutil 197 for file in ['rules', 'variables','petscrules', 'petscvariables']: 198 self.fixConfFile(os.path.join(self.destConfDir,file)) 199 self.fixConfFile(os.path.join(self.destLibDir,'pkgconfig','PETSc.pc')) 200 return 201 202 def createUninstaller(self): 203 uninstallscript = os.path.join(self.destConfDir, 'uninstall.py') 204 f = open(uninstallscript, 'w') 205 # Could use the Python AST to do this 206 f.write('#!'+sys.executable+'\n') 207 f.write('import os\n') 208 209 f.write('copies = '+re.sub(self.destDir,self.installDir,repr(self.copies))) 210 f.write(''' 211for src, dst in copies: 212 if os.path.exists(dst): 213 os.remove(dst) 214''') 215 f.close() 216 os.chmod(uninstallscript,0744) 217 return 218 219 def installIncludes(self): 220 self.copies.extend(self.copytree(self.rootIncludeDir, self.destIncludeDir)) 221 self.copies.extend(self.copytree(self.archIncludeDir, self.destIncludeDir)) 222 return 223 224 def installConf(self): 225 self.copies.extend(self.copytree(self.rootConfDir, self.destConfDir)) 226 self.copies.extend(self.copytree(self.archConfDir, self.destConfDir)) 227 return 228 229 def installBin(self): 230 self.copies.extend(self.copytree(self.rootBinDir, self.destBinDir)) 231 self.copies.extend(self.copytree(self.archBinDir, self.destBinDir)) 232 return 233 234 def installShare(self): 235 self.copies.extend(self.copytree(self.rootShareDir, self.destShareDir)) 236 return 237 238 def copyLib(self, src, dst): 239 '''Run ranlib on the destination library if it is an archive. Also run install_name_tool on dylib on Mac''' 240 # Do not install object files 241 if not os.path.splitext(src)[1] == '.o': 242 shutil.copy2(src, dst) 243 if os.path.splitext(dst)[1] == '.'+self.arLibSuffix: 244 self.executeShellCommand(self.ranlib+' '+dst) 245 if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'): 246 installName = re.sub(self.destDir, self.installDir, dst) 247 self.executeShellCommand('/usr/bin/install_name_tool -id ' + installName + ' ' + dst) 248 # preserve the original timestamps - so that the .a vs .so time order is preserved 249 shutil.copystat(src,dst) 250 return 251 252 def installLib(self): 253 self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib)) 254 return 255 256 257 def outputInstallDone(self): 258 print '''\ 259==================================== 260Install complete. It is useable with PETSC_DIR=%s [and no more PETSC_ARCH]. 261Now to check if the libraries are working do (in current directory): 262make PETSC_DIR=%s test 263====================================\ 264''' % (self.installDir,self.installDir) 265 return 266 267 def outputDestDirDone(self): 268 print '''\ 269==================================== 270Copy to DESTDIR %s is now complete. 271Before use - please copy/install over to specified prefix: %s 272====================================\ 273''' % (self.destDir,self.installDir) 274 return 275 276 def runsetup(self): 277 self.setup() 278 self.setupDirectories() 279 self.checkPrefix() 280 self.checkDestdir() 281 return 282 283 def runcopy(self): 284 if self.destDir == self.installDir: 285 print '*** Installing PETSc at prefix location:',self.destDir, ' ***' 286 else: 287 print '*** Copying PETSc to DESTDIR location:',self.destDir, ' ***' 288 if not os.path.exists(self.destDir): 289 try: 290 os.makedirs(self.destDir) 291 except: 292 print '********************************************************************' 293 print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"' 294 print '********************************************************************' 295 sys.exit(1) 296 self.installIncludes() 297 self.installConf() 298 self.installBin() 299 self.installLib() 300 self.installShare() 301 return 302 303 def runfix(self): 304 self.fixConf() 305 return 306 307 def rundone(self): 308 self.createUninstaller() 309 if self.destDir == self.installDir: 310 self.outputInstallDone() 311 else: 312 self.outputDestDirDone() 313 return 314 315 def run(self): 316 self.runsetup() 317 self.runcopy() 318 self.runfix() 319 self.rundone() 320 return 321 322if __name__ == '__main__': 323 Installer(sys.argv[1:]).run() 324 # temporary hack - delete log files created by BuildSystem - when 'sudo make install' is invoked 325 delfiles=['RDict.db','RDict.log','build.log','default.log','build.log.bkp','default.log.bkp'] 326 for delfile in delfiles: 327 if os.path.exists(delfile) and (os.stat(delfile).st_uid==0): 328 os.remove(delfile) 329