ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.4
Committed: Tue Nov 6 21:54:30 2007 UTC (17 years, 5 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.3: +9 -18 lines
Log Message:
prioritarize the UI env and manage paths at creation level

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 spiga 1.4 ## cache to reuse at creation Level .DS
57     if shKind == 'sh':
58     print 'export AUX_SCRAMPATH="%s";\n'%str(pre_env['PATH'])
59     print 'export AUX_SCRAMPATH_LD="%s";\n'%str(os.environ['LD_LIBRARY_PATH'])
60     print 'export AUX_SCRAMPATH_py="%s";\n'%str(pre_env['PYTHONPATH'])
61     else:
62     print 'setenv AUX_SCRAMPATH "%s";\n'%str(pre_env['PATH'])
63     print 'export AUX_SCRAMPATH_LD "%s";\n'%str(os.environ['LD_LIBRARY_PATH'])
64     print 'setenv AUX_SCRAMPATH_py "%s";\n'%str(pre_env['PYTHONPATH'])
65 farinafa 1.1
66     for v in os.environ:
67     if v in preserveList:
68     continue
69     if 'SCRAMRT' in v:
70     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
71     continue
72     if v not in pre_env:
73     continue
74    
75     entry = ''+str(os.environ[v])
76     purgedList = [ i for i in entry.split(':') if i not in pre_env[v] ]
77     # manage reverseList special cases (ie move entries at the end of the env var)
78     if v in reverseList:
79     purgedList = purgedList + pre_env[v]
80    
81     if len(purgedList)==0:
82     unfold_cmd = unfold_cmd + 'unset %s;\n'%v
83     continue
84     entry = str(purgedList).replace('[','').replace(']','')
85     entry = entry.replace('\'','').replace(', ',':')
86    
87     if shKind == 'sh':
88     unfold_cmd = unfold_cmd + 'export %s="%s";\n'%(v, entry)
89     else:
90     unfold_cmd = unfold_cmd + 'setenv %s "%s";\n'%(v, entry)
91    
92     # export the environment
93     print drop_out_cmd + unfold_cmd
94     sys.exit(0)
95    
96