ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.6
Committed: Tue Nov 20 15:22:22 2007 UTC (17 years, 5 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
Changes since 1.5: +4 -3 lines
Log Message:
added -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 fanzago 1.6 ### FEDE added -publish
26     if '-create' or '-publish' in sys.argv:
27 farinafa 1.5 reverseList.remove('PATH')
28     reverseList.remove('PYTHONPATH')
29     preserveList = preserveList + ['PATH', 'PYTHONPATH']
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 fanzago 1.6 ### FEDE added -publish
102     if '-create' 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