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 |
# identify the shell kind
|
19 |
sout, sin, serr = popen2.popen3('echo $BASH')
|
20 |
l = sout.readlines()[0]
|
21 |
sout.close()
|
22 |
sin.close()
|
23 |
serr.close()
|
24 |
|
25 |
if len(l)!=0: #'BASH' in os.environ:
|
26 |
shKind = 'sh'
|
27 |
else:
|
28 |
shKind = 'csh'
|
29 |
|
30 |
# -create -submit special case: first part # Fabio
|
31 |
cachedPath = os.environ['PATH']
|
32 |
cachedPyPath = os.environ['PYTHONPATH']
|
33 |
|
34 |
if '-create' in sys.argv or '-publish' in sys.argv:
|
35 |
reverseList.remove('PATH')
|
36 |
reverseList.remove('PYTHONPATH')
|
37 |
preserveList = preserveList + ['PATH', 'PYTHONPATH']
|
38 |
#
|
39 |
#
|
40 |
|
41 |
# extract the env setting introduced by scram
|
42 |
curDir = os.getcwd()
|
43 |
os.chdir(str(os.environ['CMSSW_BASE'])+'/src')
|
44 |
sout, sin, serr = popen2.popen3('echo `scramv1 runtime -%s`'%str(shKind))
|
45 |
os.chdir(curDir)
|
46 |
out = sout.readlines()[0]
|
47 |
err = serr.readlines()
|
48 |
sout.close()
|
49 |
sin.close()
|
50 |
serr.close()
|
51 |
|
52 |
if len(err) != 0:
|
53 |
print 'echo "Error while summoning scramv1: %s"'%out
|
54 |
sys.exit(stat)
|
55 |
|
56 |
out = out.replace('export ','').replace('=',' ').replace('setenv ', '')
|
57 |
outl = out.split(';')[:-1]
|
58 |
|
59 |
pre_env = {}
|
60 |
drop_out_cmd = ''
|
61 |
fakePath = '' # needed for source ordering in -create # Fabio
|
62 |
|
63 |
for e in outl:
|
64 |
vl = e.strip()
|
65 |
k = str(vl.split(' ')[0])
|
66 |
v = str(vl.split(' ')[1].replace('"','')).split(':')
|
67 |
|
68 |
if k == 'PATH':
|
69 |
fakePath = vl.split(' ')[1].replace('"','')
|
70 |
|
71 |
if 'SCRAMRT' not in k:
|
72 |
pre_env[k] = []+v
|
73 |
else:
|
74 |
drop_out_cmd = drop_out_cmd + 'unset %s;\n'%k
|
75 |
|
76 |
# unfold the current env from the scram attributes
|
77 |
unfold_cmd = 'setenv CRAB_UNFOLD_ENV "1";\n'
|
78 |
if shKind == 'sh':
|
79 |
unfold_cmd = 'export CRAB_UNFOLD_ENV="1";\n'
|
80 |
|
81 |
## cache to reuse at creation Level .DS
|
82 |
for v in os.environ:
|
83 |
if v in preserveList:
|
84 |
continue
|
85 |
if 'SCRAMRT' in v:
|
86 |
unfold_cmd = unfold_cmd + 'unset %s;\n'%v
|
87 |
continue
|
88 |
if v not in pre_env:
|
89 |
continue
|
90 |
|
91 |
entry = ''+str(os.environ[v])
|
92 |
purgedList = [ i for i in entry.split(':') if i not in pre_env[v] ]
|
93 |
# manage reverseList special cases (ie move entries at the end of the env var)
|
94 |
if v in reverseList:
|
95 |
purgedList = purgedList + pre_env[v]
|
96 |
|
97 |
if len(purgedList)==0:
|
98 |
unfold_cmd = unfold_cmd + 'unset %s;\n'%v
|
99 |
continue
|
100 |
entry = str(purgedList).replace('[','').replace(']','')
|
101 |
entry = entry.replace('\'','').replace(', ',':')
|
102 |
|
103 |
if shKind == 'sh':
|
104 |
unfold_cmd = unfold_cmd + 'export %s="%s";\n'%(v, entry)
|
105 |
else:
|
106 |
unfold_cmd = unfold_cmd + 'setenv %s "%s";\n'%(v, entry)
|
107 |
|
108 |
# export the environment
|
109 |
print drop_out_cmd + unfold_cmd
|
110 |
|
111 |
#
|
112 |
# -create special case: second part
|
113 |
# reverse and export as AUXs both the PATH and the PYTHONPATH
|
114 |
# this value will be used by crab.py whenever both -create and -submit are used
|
115 |
#
|
116 |
if '-create' in sys.argv or '-publish' in sys.argv:
|
117 |
# source order bug # Fabio
|
118 |
# reproduce a PATH as lcg->cmssw would produce and compare it with the cachedPath
|
119 |
# if they differs then override the cached path with the prepared one (brute force ordering)
|
120 |
#
|
121 |
fakePath = fakePath[:-len(':$SCRAMRT_PATH')]
|
122 |
newPath = fakePath + cachedPath.replace(fakePath,'')
|
123 |
if shKind == 'sh':
|
124 |
print 'export PATH="%s";\n'%newPath
|
125 |
else:
|
126 |
print 'setenv PATH "%s";\n'%newPath
|
127 |
|
128 |
# PATH
|
129 |
purgedList = [ i for i in cachedPath.split(':') if i not in pre_env['PATH'] ]
|
130 |
purgedList = purgedList + pre_env['PATH']
|
131 |
entry = str(purgedList).replace('[','').replace(']','').replace('\'','').replace(', ',':')
|
132 |
if shKind == 'sh':
|
133 |
print 'export AUX_SUBM_PATH="%s";\n'%entry
|
134 |
else:
|
135 |
print 'setenv AUX_SUBM_PATH "%s";\n'%entry
|
136 |
# PYTHONPATH
|
137 |
purgedList = [ i for i in cachedPyPath.split(':') if i not in pre_env['PYTHONPATH'] ]
|
138 |
purgedList = purgedList + pre_env['PYTHONPATH']
|
139 |
entry = str(purgedList).replace('[','').replace(']','').replace('\'','').replace(', ',':')
|
140 |
if shKind == 'sh':
|
141 |
print 'export AUX_SUBM_PY="%s";\n'%entry
|
142 |
else:
|
143 |
print 'setenv AUX_SUBM_PY "%s";\n'%entry
|
144 |
|
145 |
#
|
146 |
#
|
147 |
sys.exit(0)
|
148 |
|
149 |
|