xref: /petsc/config/PETSc/options/installDir.py (revision be5c6b3365a59b76b428ac9e9b82e8c6e458c99b)
19d310bb7SBarry Smith#!/usr/bin/env python
29d310bb7SBarry Smithfrom __future__ import generators
39d310bb7SBarry Smithimport config.base
49d310bb7SBarry Smithimport os
59d310bb7SBarry Smith
69d310bb7SBarry Smithclass Configure(config.base.Configure):
79d310bb7SBarry Smith  def __init__(self, framework):
89d310bb7SBarry Smith    config.base.Configure.__init__(self, framework)
99d310bb7SBarry Smith    self.headerPrefix = ''
109d310bb7SBarry Smith    self.substPrefix  = ''
115188cb68SSatish Balay    self.dir = ''
129d310bb7SBarry Smith    return
139d310bb7SBarry Smith
149d310bb7SBarry Smith  def __str__(self):
159d310bb7SBarry Smith    return ''
169d310bb7SBarry Smith
179d310bb7SBarry Smith  def setupHelp(self, help):
189d310bb7SBarry Smith    import nargs
199d310bb7SBarry Smith    help.addArgument('PETSc', '-with-clean=<bool>',         nargs.ArgBool(None, 0, 'Delete prior build files including externalpackages'))
209d310bb7SBarry Smith    return
219d310bb7SBarry Smith
229d310bb7SBarry Smith  def setupDependencies(self, framework):
239d310bb7SBarry Smith    config.base.Configure.setupDependencies(self, framework)
245bb5b263SMatthew G. Knepley    self.petscdir = framework.require('PETSc.options.petscdir', self)
259d310bb7SBarry Smith    self.arch     = framework.require('PETSc.options.arch', self)
269d310bb7SBarry Smith    return
279d310bb7SBarry Smith
289d310bb7SBarry Smith  def printSudoPasswordMessage(self,needsudo = 1):
299d310bb7SBarry Smith    '''Prints a message that sudo password will be needed for installs of packages'''
309d310bb7SBarry Smith    '''Packages like sowing and make that are never installed in an sudo location would pass 0 for needsudo'''
319d310bb7SBarry Smith    if needsudo and self.installSudoMessage:
329d310bb7SBarry Smith      self.logPrintBox(self.installSudoMessage)
339d310bb7SBarry Smith      self.installSudoMessage = ''
349d310bb7SBarry Smith
359d310bb7SBarry Smith  def setInstallDir(self):
369d310bb7SBarry Smith    ''' setup installDir to either prefix or if that is not set to PETSC_DIR/PETSC_ARCH'''
379d310bb7SBarry Smith    self.installSudo        = ''
389d310bb7SBarry Smith    self.installSudoMessage = ''
399d310bb7SBarry Smith    if self.framework.argDB['prefix']:
404a08bad0SBarry Smith      self.dir = os.path.abspath(os.path.expanduser(self.framework.argDB['prefix']))
415188cb68SSatish Balay      self.petscDir = self.dir
425188cb68SSatish Balay      self.petscArch = ''
439d310bb7SBarry Smith      try:
449d310bb7SBarry Smith        os.makedirs(os.path.join(self.dir,'PETScTestDirectory'))
459d310bb7SBarry Smith        os.rmdir(os.path.join(self.dir,'PETScTestDirectory'))
46*be5c6b33SBarry Smith      except Exception as e:
47*be5c6b33SBarry Smith        self.logPrint('Error trying to to test write permissions on directory '+str(e))
489d310bb7SBarry Smith        self.installSudoMessage = 'You do not have write permissions to the --prefix directory '+self.dir+'\nYou will be prompted for the sudo password for any external package installs'
499d310bb7SBarry Smith        self.installSudo = 'sudo '
509d310bb7SBarry Smith    else:
515bb5b263SMatthew G. Knepley      self.dir = os.path.abspath(os.path.join(self.petscdir.dir, self.arch.arch))
525188cb68SSatish Balay      self.petscDir = self.petscdir.dir
535188cb68SSatish Balay      self.petscArch = self.arch.arch
548ca03633SSatish Balay    self.addMakeMacro('PREFIXDIR',self.dir)
555bb5b263SMatthew G. Knepley    self.confDir = os.path.abspath(os.path.join(self.petscdir.dir, self.arch.arch))
569d310bb7SBarry Smith
579d310bb7SBarry Smith  def configureInstallDir(self):
589d310bb7SBarry Smith    '''Makes  installDir subdirectories if it does not exist for both prefix install location and PETSc work install location'''
595bb5b263SMatthew G. Knepley    dir = os.path.abspath(os.path.join(self.petscdir.dir, self.arch.arch))
609d310bb7SBarry Smith    if not os.path.exists(dir):
619d310bb7SBarry Smith      os.makedirs(dir)
62af0996ceSBarry Smith    for i in ['include','lib','bin',os.path.join('lib','petsc','conf')]:
639d310bb7SBarry Smith      newdir = os.path.join(dir,i)
649d310bb7SBarry Smith      if not os.path.exists(newdir):
659d310bb7SBarry Smith        os.makedirs(newdir)
669d310bb7SBarry Smith    if os.path.isfile(self.framework.argDB.saveFilename):
679d310bb7SBarry Smith      os.remove(self.framework.argDB.saveFilename)
68af0996ceSBarry Smith    confdir = os.path.join(dir,'lib','petsc','conf')
699d310bb7SBarry Smith    self.framework.argDB.saveFilename = os.path.abspath(os.path.join(confdir, 'RDict.db'))
707ad10985SMatthew G. Knepley    self.logPrint('Changed persistence directory to '+confdir)
719d310bb7SBarry Smith    return
729d310bb7SBarry Smith
735004c6edSSatish Balay  def cleanConfDir(self):
749d310bb7SBarry Smith    import shutil
755004c6edSSatish Balay    if self.framework.argDB['with-clean'] and os.path.isdir(self.confDir):
765004c6edSSatish Balay      self.logPrintBox('Warning: "with-clean" is specified. Removing all build files from '+ self.confDir)
775004c6edSSatish Balay      shutil.rmtree(self.confDir)
789d310bb7SBarry Smith    return
799d310bb7SBarry Smith
809d310bb7SBarry Smith  def saveReconfigure(self):
81af0996ceSBarry Smith    self.reconfigure_file = os.path.join(self.dir,'lib','petsc','conf','reconfigure-'+self.arch.arch+'.py')
829d310bb7SBarry Smith    self.save_reconfigure_file = None
839d310bb7SBarry Smith    if self.framework.argDB['with-clean'] and os.path.exists(self.reconfigure_file):
849d310bb7SBarry Smith      self.save_reconfigure_file = '.save.reconfigure-'+self.arch.arch+'.py'
859d310bb7SBarry Smith      try:
869d310bb7SBarry Smith        if os.path.exists(self.save_reconfigure_file): os.unlink(self.save_reconfigure_file)
879d310bb7SBarry Smith        os.rename(self.reconfigure_file,self.save_reconfigure_file)
885b6bfdb9SJed Brown      except Exception as e:
899d310bb7SBarry Smith        self.save_reconfigure_file = None
907ad10985SMatthew G. Knepley        self.logPrint('error in saveReconfigure(): '+ str(e))
919d310bb7SBarry Smith    return
929d310bb7SBarry Smith
939d310bb7SBarry Smith  def restoreReconfigure(self):
949d310bb7SBarry Smith    if self.framework.argDB['with-clean'] and self.save_reconfigure_file:
959d310bb7SBarry Smith      try:
969d310bb7SBarry Smith        os.rename(self.save_reconfigure_file,self.reconfigure_file)
975b6bfdb9SJed Brown      except Exception as e:
987ad10985SMatthew G. Knepley        self.logPrint('error in restoreReconfigure(): '+ str(e))
999d310bb7SBarry Smith    return
1009d310bb7SBarry Smith
1019d310bb7SBarry Smith  def configure(self):
1029d310bb7SBarry Smith    self.executeTest(self.setInstallDir)
1039d310bb7SBarry Smith    self.executeTest(self.saveReconfigure)
1045004c6edSSatish Balay    self.executeTest(self.cleanConfDir)
1059d310bb7SBarry Smith    self.executeTest(self.configureInstallDir)
1069d310bb7SBarry Smith    self.executeTest(self.restoreReconfigure)
1079d310bb7SBarry Smith    return
108