ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.3
Committed: Wed Oct 31 15:20:25 2007 UTC (17 years, 6 months ago) by farinafa
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +6 -0 lines
Log Message:
Fix to -create -submit tcsh env settings

File Contents

# User Rev Content
1 farinafa 1.1 import os, sys, popen2
2    
3     # env to be preserved
4     preserveList = ['SCRAMRT_LOCALRT', 'LOCALRT', 'BASE_PATH', 'CMSSW_SEARCH_PATH', 'CMSSW_VERSION',
5     'CMSSW_BASE','CMSSW_RELEASE_BASE', 'CMSSW_DATA_PATH']
6    
7     reverseList = ['LD_LIBRARY_PATH', 'PATH', 'PYTHONPATH']
8    
9     # init
10     if 'CRAB_UNFOLD_ENV' in os.environ:
11     sys.exit(0)
12    
13     if 'CMSSW_BASE' not in os.environ:
14     # CMSSW env likely not set, nothing to do
15     print 'echo "CMSSW_BASE not set"'
16     sys.exit(-1)
17    
18 farinafa 1.2 shKind = 'sh' # fix for wrapper shell, temporary #'csh' #Fabio
19     if 'BASH' in os.environ:
20 farinafa 1.1 shKind = 'sh'
21    
22     # extract the env setting introduced by scram
23     curDir = os.getcwd()
24     os.chdir(str(os.environ['CMSSW_BASE'])+'/src')
25     sout, sin, serr = popen2.popen3('echo `scramv1 runtime -%s`'%str(shKind))
26     os.chdir(curDir)
27     out = sout.readlines()[0]
28     err = serr.readlines()
29     sout.close()
30     sin.close()
31     serr.close()
32    
33     if len(err) != 0:
34     print 'echo "Error while summoning scramv1: %s"'%out
35     sys.exit(stat)
36    
37     out = out.replace('export ','').replace('=',' ').replace('setenv ', '')
38     outl = out.split(';')[:-1]
39    
40     pre_env = {}
41     drop_out_cmd = ''
42     for e in outl:
43     v = e.strip()
44     k = str(v.split(' ')[0])
45     v = str(v.split(' ')[1].replace('"','')).split(':')
46     if 'SCRAMRT' not in k:
47     pre_env[k] = []+v
48     else:
49     drop_out_cmd = drop_out_cmd + 'unset %s;\n'%k
50    
51     # unfold the current env from the scram attributes
52     unfold_cmd = 'setenv CRAB_UNFOLD_ENV "1";\n'
53     if shKind == 'sh':
54     unfold_cmd = 'export CRAB_UNFOLD_ENV="1";\n'
55    
56     if '-create' in sys.argv[1:]:
57     for ek in ['PATH', 'PYTHONPATH']:
58     entry = ''+str(os.environ[ek])
59     purgedList = [ i for i in entry.split(':') if i not in pre_env[ek] ]
60     purgedList = pre_env[ek] + purgedList
61     entry = str(purgedList).replace('[','').replace(']','')
62     entry = entry.replace('\'','').replace(', ',':')
63     if shKind == 'sh':
64     print 'export %s="%s";\n'%(ek, entry)
65     else:
66     print 'setenv %s "%s";\n'%(ek, entry)
67 farinafa 1.3 # auxiliary export for the -create -submit option
68     if '-submit' in sys.argv[1:]:
69     if shKind == 'sh':
70     print 'export AUX_SCRAMPATH="%s";\n'%str(pre_env['PATH'])
71     else:
72     print 'setenv AUX_SCRAMPATH "%s";\n'%str(pre_env['PATH'])
73 farinafa 1.1 sys.exit(0)
74    
75     for v in os.environ:
76     if v in preserveList:
77     continue
78     if 'SCRAMRT' in v:
79     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
80     continue
81     if v not in pre_env:
82     continue
83    
84     entry = ''+str(os.environ[v])
85     purgedList = [ i for i in entry.split(':') if i not in pre_env[v] ]
86     # manage reverseList special cases (ie move entries at the end of the env var)
87     if v in reverseList:
88     purgedList = purgedList + pre_env[v]
89    
90     if len(purgedList)==0:
91     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
92     continue
93     entry = str(purgedList).replace('[','').replace(']','')
94     entry = entry.replace('\'','').replace(', ',':')
95    
96     if shKind == 'sh':
97     unfold_cmd = unfold_cmd + 'export %s="%s";\n'%(v, entry)
98     else:
99     unfold_cmd = unfold_cmd + 'setenv %s "%s";\n'%(v, entry)
100    
101     # export the environment
102     print drop_out_cmd + unfold_cmd
103     sys.exit(0)
104    
105