reworked script...

This commit is contained in:
Philip Eisenlohr 2011-12-20 10:52:58 +00:00
parent c5d64df287
commit 7dff3ba12c
1 changed files with 71 additions and 47 deletions

View File

@ -3,45 +3,50 @@
import os,sys,string,re import os,sys,string,re
from optparse import OptionParser from optparse import OptionParser
pathInfo = {\
'acml': '/opt/acml4.4.0',
'fftw': './fftw',
'msc': '/msc',
}
validShells = {\ validShells = {\
'bash':['.bashrc','.bash_profile'], 'bash':['.bashrc','.bash_profile','.bash_login','.profile'],
'csh': ['.cshrc'], 'csh': ['.cshrc'],
} }
environment = [\ env_order = ['DAMASK_ROOT','DAMASK_BIN','PATH','PYTHONPATH','LD_LIBRARY_PATH']
{ 'name': 'DAMASK_ROOT', environment = { 'DAMASK_ROOT':
'delete':'DamaskRoot', [{'delete':'DamaskRoot',
'substitute':'[DamaskRoot]', 'substitute':'[DamaskRoot]',
'append': False, 'append': False,
}, },
{ 'name': 'DAMASK_BIN', ],
'delete':'os.path.join(DamaskRoot,"bin")', 'DAMASK_BIN':
[{'delete':'os.path.join(DamaskRoot,"bin")',
'substitute':'[os.path.join(DamaskRoot,"bin")]', 'substitute':'[os.path.join(DamaskRoot,"bin")]',
'append': False, 'append': False,
}, },
{ 'name': 'PATH', ],
'delete':'"${DAMASK_BIN}"', 'PATH':
[{'delete':'"${DAMASK_BIN}"',
'substitute':'["${DAMASK_BIN}"]', 'substitute':'["${DAMASK_BIN}"]',
'append': True, 'append': True,
}, },
{ 'name': 'PYTHONPATH', ],
'delete':'"${DAMASK_ROOT}/lib"', 'PYTHONPATH':
[{'delete':'"${DAMASK_ROOT}/lib"',
'substitute':'["${DAMASK_ROOT}/lib"]', 'substitute':'["${DAMASK_ROOT}/lib"]',
'append': True, 'append': True,
}, },
{ 'name': 'LD_LIBRARY_PATH', ],
'LD_LIBRARY_PATH':
[{'activate': 'pathInfo["acml"]',
'delete':'"acml"', # what keywords trigger item deletion from existing path 'delete':'"acml"', # what keywords trigger item deletion from existing path
'substitute':'[os.path.join(pathInfo["acml"],"ifort64_mp/lib"),\ 'substitute':'[os.path.join(pathInfo["acml"],"ifort64_mp/lib"),\
os.path.join(pathInfo["acml"],"ifort64/lib")]', # what to substitute for deleted path items os.path.join(pathInfo["acml"],"ifort64/lib")]', # what to substitute for deleted path items
'append': True, # whether new entries append to existing ${env} 'append': True, # whether new entries append to existing ${env}
}, },
] {'activate': 'pathInfo["lapack"]',
'substitute':'[pathInfo["lapack"]]', # what to substitute for deleted path items
'append': True, # whether new entries append to existing ${env}
},
],
}
parser = OptionParser(usage="%prog [options]", description = """ parser = OptionParser(usage="%prog [options]", description = """
Sets up your shell resource to be compatible with DAMASK. Sets up your shell resource to be compatible with DAMASK.
""" + string.replace('$Id$','\n','\\n') """ + string.replace('$Id$','\n','\\n')
@ -55,6 +60,8 @@ parser.set_defaults(shell = 'bash')
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
pathInfo = {}
DamaskRoot = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),'../')) DamaskRoot = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),'../'))
try: # check for user-defined pathinfo try: # check for user-defined pathinfo
file = open(os.path.join(DamaskRoot,'lib/pathinfo')) file = open(os.path.join(DamaskRoot,'lib/pathinfo'))
@ -76,24 +83,41 @@ if theShell == 'bash':
content = map(string.strip,rc.readlines()) content = map(string.strip,rc.readlines())
rc.close() rc.close()
match = {}
for var in env_order: match[var] = False
output = [] output = []
for var in environment: var['matched'] = False
for line in content: for line in content:
for var in environment: for var in env_order:
m = re.search(r'^(.*? %s=)([^;]*)(.*)$'%var['name'],line) m = re.search(r'^(.*? %s=)([^;]*)(.*)$'%var,line)
if m: if m:
substitute = [path for path in m.group(2).split(':') if eval(var['delete']) not in path] + \ match[var] = True
eval(var['substitute']) items = m.group(2).split(':')
line = m.group(1)+':'.join(substitute)+m.group(3) for piece in environment[var]:
var['matched'] = True try:
if 'activate' not in piece or eval(piece['activate']) != '':
items = [path for path in items if 'delete' not in piece or eval(piece['delete']) not in path] + \
eval(piece['substitute'])
except:
pass
line = m.group(1)+':'.join(items)+m.group(3)
output.append(line) output.append(line)
for var in environment: for var in env_order:
if not var['matched']: if not match[var]:
output.append('export %s=%s'%(var['name'],':'.join({True:['${%s}'%var['name']],False:[]}[var['append']]+\ items = ['${%s}'%var]
eval(var['substitute'])))) for piece in environment[var]:
try:
if 'activate' not in piece or eval(piece['activate']) != '':
if piece['append']:
items += eval(piece['substitute'])
else:
items = eval(piece['substitute'])
except:
pass
output.append('export %s=%s'%(var,':'.join(items)))
rc = open(os.path.join(theHome,theRC),'w') rc = open(os.path.join(theHome,theRC),'w')
rc.write('\n'.join(output)+'\n') rc.write('\n'.join(output)+'\n')