DAMASK_EICMD/configure

139 lines
5.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
2014-01-24 18:00:27 +05:30
import os,re,sys,string,subprocess,shutil
2014-01-14 02:47:28 +05:30
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)
2014-01-11 19:58:07 +05:30
########################################################
# MAIN
########################################################
2014-01-11 19:58:07 +05:30
parser = OptionParser(option_class=extendableOption, usage='%prog options', description = """
2014-01-24 18:00:27 +05:30
Configures the compilation and installation of DAMASK
2016-03-24 16:19:23 +05:30
""")
defaults={'DAMASK_BIN':'depending on access rights',
'FFTW_ROOT':'/usr',
'MSC_ROOT' :'/msc',
'DAMASK_NUM_THREADS':4,
2015-12-15 03:01:47 +05:30
'MARC_VERSION':'2015',
2015-11-10 21:32:20 +05:30
'spectralOptions':{},
}
2014-01-24 18:00:27 +05:30
#--- 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'
2014-01-24 18:00:27 +05:30
#--- set default values according to read in values ------------------------------------------------
try:
with open(configFile,'r') as f:
print('\n<<<<< reading default values from %s\n'%configFile)
2014-01-24 18:00:27 +05:30
for line in f:
2014-08-27 20:49:04 +05:30
line = line.strip()
if line.startswith('#') or line == '':
2014-01-24 18:00:27 +05:30
pass
2014-08-27 20:49:04 +05:30
[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
2014-01-24 18:00:27 +05:30
except IOError:
pass
2015-12-15 03:01:47 +05:30
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',
2014-01-24 18:00:27 +05:30
help='number of openMP threads [%default]')
2015-12-15 03:01:47 +05:30
parser.add_option('--with-spectral-options', dest='spectraloptions', action='extend', metavar='<string LIST>',
2014-01-24 18:00:27 +05:30
help='options for compilation of spectral solver')
2015-12-15 03:01:47 +05:30
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 = [])
2014-01-11 19:58:07 +05:30
(options,filenames) = parser.parse_args()
2014-01-24 18:00:27 +05:30
#--- read config file if present to keep comments and order ---------------------------------------
output = []
2014-01-24 18:00:27 +05:30
try:
with open(configFile,'r') as f:
for line in f:
line = line.strip()
2014-01-24 18:00:27 +05:30
items = re.split('[= ]',line)
if (not line or items[0].startswith('#')):
2014-01-24 18:00:27 +05:30
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 =''
2015-12-15 03:01:47 +05:30
if items[0] == 'MARC_VERSION':
line = '%s=%s'%(items[0],options.marcVersion)
options.marcVersion =''
2014-01-24 18:00:27 +05:30
if items[0] == 'DAMASK_NUM_THREADS':
line = '%s=%s'%(items[0],options.threads)
2014-01-24 18:00:27 +05:30
options.threads =''
for spectralOption in options.spectraloptions:
[key,value] = re.split('[= ]',spectralOption)[0:2]
if key == items[0]:
line = '%s=%s'%(items[0],value)
2014-01-24 18:00:27 +05:30
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)
2015-12-15 03:01:47 +05:30
if opt == 'marcVersion' and value != '':
output.append('MARC_VERSION=%s'%value)
if opt == 'threads' and value != '':
output.append('DAMASK_NUM_THREADS=%s'%value)
2014-01-24 18:00:27 +05:30
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')
2014-01-24 18:00:27 +05:30
if not os.path.isdir(configDir):
os.mkdir(configDir)
print('\n>>>>> writing values to %s\n'%configFileNew)
2014-01-24 18:00:27 +05:30
with open(configFileNew,'w') as f:
for line in output:
print(line)
f.write(line+'\n')