ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.5
Committed: Wed Nov 7 13:42:11 2007 UTC (17 years, 5 months ago) by farinafa
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_0_2, CRAB_2_0_2_pre6, CRAB_2_0_2_pre5, CRAB_2_0_2_pre4, CRAB_2_0_2_pre3, CRAB_2_0_2_pre2
Changes since 1.4: +36 -9 lines
Log Message:
Fixes for -create -submit use case plus some code cleaning

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 farinafa 1.5 # -create -submit special case: first part # Fabio
23     cachedPath = os.environ['PATH']
24     cachedPyPath = os.environ['PYTHONPATH']
25     if '-create' in sys.argv:
26     reverseList.remove('PATH')
27     reverseList.remove('PYTHONPATH')
28     preserveList = preserveList + ['PATH', 'PYTHONPATH']
29     #
30     #
31    
32 farinafa 1.1 # extract the env setting introduced by scram
33     curDir = os.getcwd()
34     os.chdir(str(os.environ['CMSSW_BASE'])+'/src')
35     sout, sin, serr = popen2.popen3('echo `scramv1 runtime -%s`'%str(shKind))
36     os.chdir(curDir)
37     out = sout.readlines()[0]
38     err = serr.readlines()
39     sout.close()
40     sin.close()
41     serr.close()
42    
43     if len(err) != 0:
44     print 'echo "Error while summoning scramv1: %s"'%out
45     sys.exit(stat)
46    
47     out = out.replace('export ','').replace('=',' ').replace('setenv ', '')
48     outl = out.split(';')[:-1]
49    
50     pre_env = {}
51     drop_out_cmd = ''
52     for e in outl:
53     v = e.strip()
54     k = str(v.split(' ')[0])
55     v = str(v.split(' ')[1].replace('"','')).split(':')
56     if 'SCRAMRT' not in k:
57     pre_env[k] = []+v
58     else:
59     drop_out_cmd = drop_out_cmd + 'unset %s;\n'%k
60    
61     # unfold the current env from the scram attributes
62     unfold_cmd = 'setenv CRAB_UNFOLD_ENV "1";\n'
63     if shKind == 'sh':
64     unfold_cmd = 'export CRAB_UNFOLD_ENV="1";\n'
65    
66 spiga 1.4 ## cache to reuse at creation Level .DS
67 farinafa 1.1 for v in os.environ:
68     if v in preserveList:
69     continue
70     if 'SCRAMRT' in v:
71     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
72     continue
73     if v not in pre_env:
74     continue
75    
76     entry = ''+str(os.environ[v])
77     purgedList = [ i for i in entry.split(':') if i not in pre_env[v] ]
78     # manage reverseList special cases (ie move entries at the end of the env var)
79     if v in reverseList:
80     purgedList = purgedList + pre_env[v]
81    
82     if len(purgedList)==0:
83     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
84     continue
85     entry = str(purgedList).replace('[','').replace(']','')
86     entry = entry.replace('\'','').replace(', ',':')
87    
88     if shKind == 'sh':
89     unfold_cmd = unfold_cmd + 'export %s="%s";\n'%(v, entry)
90     else:
91     unfold_cmd = unfold_cmd + 'setenv %s "%s";\n'%(v, entry)
92    
93     # export the environment
94     print drop_out_cmd + unfold_cmd
95 farinafa 1.5
96     #
97     # -create special case: second part
98     # reverse and export as AUXs both the PATH and the PYTHONPATH
99     # this value will be used by crab.py whenever both -create and -submit are used
100     #
101     if '-create' in sys.argv:
102     # PATH
103     purgedList = [ i for i in cachedPath.split(':') if i not in pre_env['PATH'] ]
104     purgedList = purgedList + pre_env['PATH']
105     entry = str(purgedList).replace('[','').replace(']','').replace('\'','').replace(', ',':')
106     if shKind == 'sh':
107     print 'export AUX_SUBM_PATH="%s";\n'%entry
108     else:
109     print 'setenv AUX_SUBM_PATH "%s";\n'%entry
110     # PYTHONPATH
111     purgedList = [ i for i in cachedPyPath.split(':') if i not in pre_env['PYTHONPATH'] ]
112     purgedList = purgedList + pre_env['PYTHONPATH']
113     entry = str(purgedList).replace('[','').replace(']','').replace('\'','').replace(', ',':')
114     if shKind == 'sh':
115     print 'export AUX_SUBM_PY="%s";\n'%entry
116     else:
117     print 'setenv AUX_SUBM_PY "%s";\n'%entry
118    
119     #
120     #
121 farinafa 1.1 sys.exit(0)
122    
123