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 # Do not install object files 216 if not os.path.splitext(src)[1] == '.o': 217 shutil.copy2(src, dst) 218 if os.path.splitext(dst)[1] == '.'+self.libSuffix: 219 self.executeShellCommand(self.ranlib+' '+dst) 220 if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'): 221 installName = re.sub(self.destDir, self.installDir, dst) 222 self.executeShellCommand('/usr/bin/install_name_tool -id ' + installName + ' ' + dst) 223 # preserve the original timestamps - so that the .a vs .so time order is preserved 224 shutil.copystat(src,dst) 225 return 226 227 def installLib(self): 228 self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib)) 229 return 230 231 232 def outputDone(self): 233 print '''\ 234==================================== 235Install complete. It is useable with PETSC_DIR=%s [and no more PETSC_ARCH]. 236Now to check if the libraries are working do (in current directory): 237make PETSC_DIR=%s test 238====================================\ 239''' % (self.installDir,self.installDir) 240 return 241 242 def runfix(self): 243 self.setup() 244 self.setupDirectories() 245 self.createUninstaller() 246 self.fixConf() 247 248 def runcopy(self): 249 if os.path.exists(self.destDir) and os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)): 250 print '********************************************************************' 251 print 'Install directory is current directory; nothing needs to be done' 252 print '********************************************************************' 253 return 254 print '*** Installing PETSc at',self.destDir, ' ***' 255 if not os.path.exists(self.destDir): 256 try: 257 os.makedirs(self.destDir) 258 except: 259 print '********************************************************************' 260 print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"' 261 print '********************************************************************' 262 return 263 if not os.path.isdir(os.path.realpath(self.destDir)): 264 print '********************************************************************' 265 print 'Specified destDir', self.destDir, 'is not a directory. Cannot proceed!' 266 print '********************************************************************' 267 return 268 if not os.access(self.destDir, os.W_OK): 269 print '********************************************************************' 270 print 'Unable to write to ', self.destDir, 'Perhaps you need to do "sudo make install"' 271 print '********************************************************************' 272 return 273 274 self.installIncludes() 275 self.installConf() 276 self.installBin() 277 self.installLib() 278 self.installShare() 279 self.outputDone() 280 281 return 282 283 def run(self): 284 self.runfix() 285 self.runcopy() 286 287if __name__ == '__main__': 288 Installer(sys.argv[1:]).run() 289 # temporary hack - delete log files created by BuildSystem - when 'sudo make install' is invoked 290 delfiles=['RDict.db','RDict.log','build.log','default.log','build.log.bkp','default.log.bkp'] 291 for delfile in delfiles: 292 if os.path.exists(delfile) and (os.stat(delfile).st_uid==0): 293 os.remove(delfile) 294