switched to general logic that branches locally depending on shell type.

now bash and csh derivatives should work.
This commit is contained in:
Philip Eisenlohr 2012-10-18 09:55:54 +00:00
parent dd5f453994
commit 0e33725395
1 changed files with 65 additions and 60 deletions

View File

@ -3,9 +3,9 @@
import os,sys,string,re import os,sys,string,re
from optparse import OptionParser from optparse import OptionParser
validShells = {\ validShellRC = {\
'bash':['.bashrc','.bash_profile','.bash_login','.profile','new'], 'bash':['.bashrc','.bash_profile','.bash_login','.profile','new'],
'csh': ['.cshrc'], 'csh': ['.cshrc','new'],
} }
env_order = ['DAMASK_ROOT','DAMASK_BIN','PATH','PYTHONPATH','LD_LIBRARY_PATH'] env_order = ['DAMASK_ROOT','DAMASK_BIN','PATH','PYTHONPATH','LD_LIBRARY_PATH']
@ -62,7 +62,7 @@ Sets up your shell resource to be compatible with DAMASK.
parser.add_option("-s","--shell", type="string", parser.add_option("-s","--shell", type="string",
dest = "shell", \ dest = "shell", \
help = "type of shell, e.g. "+', '.join(validShells.keys())+" [%default]") help = "type of shell, e.g. "+', '.join(validShellRC.keys())+" [%default]")
parser.set_defaults(shell = 'bash') parser.set_defaults(shell = 'bash')
@ -87,75 +87,80 @@ except:
theShell = options.shell.lower() theShell = options.shell.lower()
theHome = os.getenv('USERPROFILE') or os.getenv('HOME') theHome = os.getenv('USERPROFILE') or os.getenv('HOME')
if theShell == 'bash': for theRC in validShellRC[theShell]:
for theRC in validShells[theShell]: if theRC == 'new' and not hitSomething:
if theRC =='new' and not hitSomething: theRC = validShellRC[theShell][0]
print 'Could not find a file to store bash information, creating .bashrc in home directory' print 'Could not find a file to store bash information, creating "%s" in home directory'%theRC
theRC='.bashrc' open(os.path.join(theHome,theRC), 'w').close()
open(os.path.join(theHome,theRC), 'w').close() thePath = os.path.join(theHome,theRC)
thePath = os.path.join(theHome,theRC) if os.path.exists(thePath):
if os.path.exists(thePath): print thePath
print thePath hitSomething = True
hitSomething = True rc = open(os.path.join(theHome,theRC))
rc = open(os.path.join(theHome,theRC)) content = map(string.strip,rc.readlines())
content = map(string.strip,rc.readlines()) rc.close()
rc.close()
match = {} match = {}
for var in env_order: match[var] = False for var in env_order: match[var] = False
output = [] output = []
for line in content: for line in content:
for var in env_order:
m = re.search(r'^(.*? %s=)([^;]*)(.*)$'%var,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(':')
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]
items += eval(piece['substitute'])
else:
items = eval(piece['substitute'])
except:
pass
line = m.group(1)+':'.join(items)+m.group(3)
output.append(line)
for var in env_order: for var in env_order:
if not match[var]: m = re.search(\
items = ['${%s}'%var] {\
'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(':')
for piece in environment[var]: for piece in environment[var]:
try: try:
if 'activate' not in piece or eval(piece['activate']) != '': if 'activate' not in piece or eval(piece['activate']) != '':
if piece['append']: 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]
items += eval(piece['substitute']) items += eval(piece['substitute'])
else: else:
items = eval(piece['substitute']) items = eval(piece['substitute'])
except: except:
pass pass
output.append('export %s=%s'%(var,':'.join(items))) line = m.group(1)+':'.join(items)+m.group(3)
rc = open(os.path.join(theHome,theRC),'w') output.append(line)
rc.write('\n'.join(output)+'\n')
rc.close()
elif theShell == 'csh': for var in env_order:
print 'csh not supported yet...' 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]))
if not hitSomething: print 'no %s found..!'%(', '.join(validShells[theShell]))