ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/unfoldEnv.py
Revision: 1.1
Committed: Fri Oct 26 14:27:01 2007 UTC (17 years, 6 months ago) by farinafa
Content type: text/x-python
Branch: MAIN
Log Message:
Changes for the environments interactions

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