DAMASK_EICMD/configure

139 lines
5.2 KiB
Python
Executable File

#!/usr/bin/env python
import os,re,sys,string,subprocess,shutil
from optparse import OptionParser, Option
# -----------------------------
class extendableOption(Option):
# -----------------------------
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
# taken from online tutorial http://docs.python.org/library/optparse.html
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(self, action, dest, opt, value, values, parser)
########################################################
# MAIN
########################################################
parser = OptionParser(option_class=extendableOption, usage='%prog options', description = """
Configures the compilation and installation of DAMASK
""")
defaults={'DAMASK_BIN':'depending on access rights',
'FFTW_ROOT':'/usr',
'MSC_ROOT' :'/msc',
'DAMASK_NUM_THREADS':4,
'MARC_VERSION':'2015',
'spectralOptions':{},
}
#--- if local config file exists, read, otherwise assume global config file ------------------------
configFile = os.path.join(os.getenv('HOME'),'.damask/damask.conf') \
if os.path.isfile(os.path.join(os.getenv('HOME'),'.damask/damask.conf')) \
else '/etc/damask.conf'
#--- set default values according to read in values ------------------------------------------------
try:
with open(configFile,'r') as f:
print('\n<<<<< reading default values from %s\n'%configFile)
for line in f:
line = line.strip()
if line.startswith('#') or line == '':
pass
[key,value] = (re.split('[= ]',line)+[None,None])[:2]
if key == 'DAMASK_NUM_THREADS':
defaults['DAMASK_NUM_THREADS'] = int(value)
if key == 'DAMASK_BIN':
defaults['DAMASK_BIN'] = value
except IOError:
pass
parser.add_option('--prefix', dest='prefix', metavar='string',
help='location of (links to) DAMASK executables [%default]')
parser.add_option('--with-OMP-threads','--with-omp-threads',
dest='threads', type='int', metavar='int',
help='number of openMP threads [%default]')
parser.add_option('--with-spectral-options', dest='spectraloptions', action='extend', metavar='<string LIST>',
help='options for compilation of spectral solver')
parser.set_defaults(prefix = defaults['DAMASK_BIN'])
parser.set_defaults(mscRoot = defaults['MSC_ROOT'])
parser.set_defaults(marcVersion = defaults['MARC_VERSION'])
parser.set_defaults(threads = defaults['DAMASK_NUM_THREADS'])
parser.set_defaults(spectraloptions = [])
(options,filenames) = parser.parse_args()
#--- read config file if present to keep comments and order ---------------------------------------
output = []
try:
with open(configFile,'r') as f:
for line in f:
line = line.strip()
items = re.split('[= ]',line)
if (not line or items[0].startswith('#')):
pass
if items[0] == 'DAMASK_BIN':
line = '%s=%s'%(items[0],options.prefix)
options.prefix ='depending on access rights'
if items[0] == 'MSC_ROOT':
line = '%s=%s'%(items[0],options.mscRoot)
options.mscRoot =''
if items[0] == 'MARC_VERSION':
line = '%s=%s'%(items[0],options.marcVersion)
options.marcVersion =''
if items[0] == 'DAMASK_NUM_THREADS':
line = '%s=%s'%(items[0],options.threads)
options.threads =''
for spectralOption in options.spectraloptions:
[key,value] = re.split('[= ]',spectralOption)[0:2]
if key == items[0]:
line = '%s=%s'%(items[0],value)
options.spectraloptions.remove(spectralOption)
output.append(line)
except IOError:
pass
#--- write remaining options --------------------------------------------------------------------------
for opt, value in options.__dict__.items():
if opt == 'prefix' and value != 'depending on access rights':
output.append('DAMASK_BIN=%s'%value)
if opt == 'mscRoot' and value != '':
output.append('MSC_ROOT=%s'%value)
if opt == 'marcVersion' and value != '':
output.append('MARC_VERSION=%s'%value)
if opt == 'threads' and value != '':
output.append('DAMASK_NUM_THREADS=%s'%value)
for spectralOption in options.spectraloptions:
output.append(spectralOption)
#--- decide where do save the data -------------------------------------------------------------------
configDir = '/etc' if os.access('/etc/', os.W_OK) \
else os.path.join(os.getenv('HOME'),'.damask') # use system-wide config if possible
configFileNew = os.path.join(configDir,'damask.conf')
if not os.path.isdir(configDir):
os.mkdir(configDir)
print('\n>>>>> writing values to %s\n'%configFileNew)
with open(configFileNew,'w') as f:
for line in output:
print(line)
f.write(line+'\n')