ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.7
Committed: Tue Nov 20 21:03:18 2007 UTC (17 years, 5 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_0_3, CRAB_2_0_3_pre1
Changes since 1.6: +4 -4 lines
Log Message:
bug fix on -publish option

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