DAMASK_EICMD/installation/setup_shellrc.py

169 lines
7.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import os,sys,string,re
from optparse import OptionParser
validShellRC = {\
'bash':['.bashrc','.bash_profile','.bash_login','.profile','new'],
'csh': ['.cshrc','new'],
}
2011-12-20 16:22:58 +05:30
env_order = ['DAMASK_ROOT','DAMASK_BIN','PATH','PYTHONPATH','LD_LIBRARY_PATH']
environment = { 'DAMASK_ROOT':
2012-01-11 22:08:23 +05:30
[{'delete':'',
2011-12-20 16:22:58 +05:30
'substitute':'[DamaskRoot]',
'append': False,
},
],
'DAMASK_BIN':
2012-01-11 22:08:23 +05:30
[{'delete':'',
2011-12-20 16:22:58 +05:30
'substitute':'[os.path.join(DamaskRoot,"bin")]',
'append': False,
},
],
'PATH':
[{'delete':'"${DAMASK_BIN}"',
'substitute':'["${DAMASK_BIN}"]',
'append': True,
},
],
'PYTHONPATH':
[{'delete':'"${DAMASK_ROOT}/lib"',
'substitute':'["${DAMASK_ROOT}/lib"]',
'append': True,
},
],
'LD_LIBRARY_PATH':
2012-01-11 22:08:23 +05:30
[{'activate': 'pathInfo["acml"]',
2013-02-01 18:52:40 +05:30
'delete': '"acml"', # what keywords trigger item deletion from existing path
2011-12-20 16:22:58 +05:30
'substitute':'[os.path.join(pathInfo["acml"],"ifort64_mp/lib"),\
2013-02-01 18:52:40 +05:30
os.path.join(pathInfo["acml"],"ifort64/lib"),\
os.path.join(pathInfo["acml"],"gfortran64_mp/lib"),\
os.path.join(pathInfo["acml"],"gfortran64/lib")]', # what to substitute for deleted path items
'append': True, # whether new entries append to existing ${env}
2011-12-20 16:22:58 +05:30
},
2012-01-11 22:08:23 +05:30
{'activate': 'pathInfo["lapack"]',
'delete': '[os.path.join(pathInfo["lapack"],"lib"),\
2013-02-01 18:52:40 +05:30
os.path.join(pathInfo["lapack"],"lib64")]', # deleted current (same) entry
'substitute':'[os.path.join(pathInfo["lapack"],"lib"),\
2013-02-01 18:52:40 +05:30
os.path.join(pathInfo["lapack"],"lib64")]', # what to substitute for deleted path
'append': True, # whether new entries append to existing ${env}
},
2012-01-11 22:08:23 +05:30
{'activate': 'pathInfo["fftw"]',
2013-02-01 18:52:40 +05:30
'delete': '[os.path.join(pathInfo["fftw"],"lib")]', # deleted current (same) entry
'substitute':'[os.path.join(pathInfo["fftw"],"lib")]', # what to substitute for deleted path items
'append': True, # whether new entries append to existing ${env}
2011-12-20 16:22:58 +05:30
},
],
}
parser = OptionParser(usage="%prog [options]", description = """
Sets up your shell resource to be compatible with DAMASK.
""" + string.replace('$Id$','\n','\\n')
)
parser.add_option("-s","--shell", type="string",
dest = "shell", \
help = "type of shell, e.g. "+', '.join(validShellRC.keys())+" [%default]")
parser.set_defaults(shell = 'bash')
(options, args) = parser.parse_args()
2011-12-20 16:22:58 +05:30
pathInfo = {}
hitSomething = False
2011-12-20 16:22:58 +05:30
DamaskRoot = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),'../'))
try: # check for user-defined pathinfo
file = open(os.path.join(DamaskRoot,'lib/pathinfo'))
2011-12-20 16:34:29 +05:30
content = map(lambda string: string.strip(),file.readlines())
file.close()
for line in content:
2011-12-20 16:34:29 +05:30
if not (line.startswith('#') or line == ''):
2012-01-11 22:08:23 +05:30
items = line.split() + ['','']
pathInfo[items[0].lower()] = {False: os.path.normpath(os.path.join(DamaskRoot,'lib/',items[1])),
True: items[1]}[items[1] == '']
except:
pass
theShell = options.shell.lower()
theHome = os.getenv('USERPROFILE') or os.getenv('HOME')
for theRC in validShellRC[theShell]:
if theRC == 'new' and not hitSomething:
theRC = validShellRC[theShell][0]
print 'Could not find a file to store bash information, creating "%s" in home directory'%theRC
open(os.path.join(theHome,theRC), 'w').close()
thePath = os.path.join(theHome,theRC)
if os.path.exists(thePath):
print thePath
hitSomething = True
rc = open(os.path.join(theHome,theRC))
content = map(string.strip,rc.readlines())
rc.close()
match = {}
for var in env_order: match[var] = False
output = []
2011-12-20 16:22:58 +05:30
for line in content:
2011-12-20 16:22:58 +05:30
for var in env_order:
m = re.search(\
{\
'bash': r'^(.*? %s=)([^;]*)(.*)$'%var,
'csh': r'^(\s*setenv\s+%s\s+)([^;]*)(.*)$'%var
}[theShell],line)
if m:
n = re.search(r'(%s)'%var, line)
o = re.search(r'(#)', line)
if o:
if o.start() < n.start():
print 'skipped potential comment line, please check!'
continue
match[var] = True
items = m.group(2).split(':')
2011-12-20 16:22:58 +05:30
for piece in environment[var]:
try:
if 'activate' not in piece or eval(piece['activate']) != '':
if piece['append']:
if 'delete' in piece:
killer = eval(piece['delete'])
if type(killer) == str:
items = [path for path in items if killer not in path]
if type(killer) == list:
items = [path for path in items if path not in killer]
2011-12-20 16:22:58 +05:30
items += eval(piece['substitute'])
else:
items = eval(piece['substitute'])
2011-12-20 16:22:58 +05:30
except:
pass
line = m.group(1)+':'.join(items)+m.group(3)
output.append(line)
for var in env_order:
if not match[var]:
items = ['${%s}'%var]
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({\
'bash':'export %s=%s'%(var,':'.join(items)),
'csh': 'setenv %s %s'%(var,':'.join(items)),
}[theShell])
rc = open(os.path.join(theHome,theRC),'w')
rc.write('\n'.join(output)+'\n')
rc.close()
if not hitSomething: print 'none of %s found..!'%(', '.join(validShellRC[theShell]))