summaryrefslogtreecommitdiff
blob: 126f225fe58cdcff47654c91d81be2f6ccbbb58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#=====================================================
# Copyright (C) 2012 Andrea Arteaga <andyspiros@gmail.com>
#=====================================================
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
import os, shlex
from os.path import isfile, join as pjoin
from ..benchconfig import libdir

def transformPaths(root, value):
    paths = value.split(':')
    newpaths = ''
    for p in paths:
        if p[0] == '/':
            newpaths += ':' + (root + p)
        else:
            newpaths += ':' + (p)
    return newpaths
        

def envread(test):
    # Set default paths
    path = pjoin(test['root'], 'bin') + ':' +  pjoin(test['root'], 'usr/bin')
    libpath = pjoin(test['root'], libdir)
    addenv = dict( PATH=path, LIBRARY_PATH=libpath, LD_LIBRARY_PATH=libpath )
    
    # Merge environment
    for k,v in addenv.items():
        if test['compileenv'].has_key(k):
            test['compileenv'][k] += ':' + v
        else:
            test['compileenv'][k] = v
            
        if test['runenv'].has_key(k):
            test['runenv'][k] += ':' + v
        else:
            test['runenv'][k] = v


    # Read environment files in (root)/etc/env.d
    envdir = pjoin(test['root'], 'etc', 'env.d')
    files = [pjoin(envdir, i) for i in os.listdir(envdir)]
    files = [i for i in files if isfile(i)]
    files.sort()
    
    for envf in files:
        fs = file(envf, 'r')
        for l in fs.readlines():
            l = l.strip().split('=', 1)
            vname = l[0]
            vvalue = shlex.split(l[1])[0]
            
            if vname[-4:] == 'PATH':
                # Append paths in colon-separated list
                newpaths = transformPaths(test['root'], vvalue)
                
                # Compile environment
                if test['compileenv'].has_key(vname):
                    newvar = test['compileenv'][vname] + newpaths
                else:
                    newvar = newpaths[1:]
                test['compileenv'][vname] = newvar
                
                # Run-time environment
                if test['runenv'].has_key(vname):
                    newvar = test['runenv'][vname] + newpaths
                else:
                    newvar = newpaths[1:]
                test['runenv'][vname] = newvar
                
            else:
                # Substitute other variables
                test['compileenv'][vname] = vvalue
                test['runenv'][vname] = vvalue
        
    # Add LDPATH to LIBRARY_PATH and LD_LIBRARY_PATH
    if test['compileenv'].has_key('LDPATH'):
        test['compileenv']['LIBRARY_PATH'] += ':' + test['compileenv']['LDPATH']
        test['compileenv']['LD_LIBRARY_PATH']+= ':'+test['compileenv']['LDPATH']
    if test['runenv'].has_key('LDPATH'):
        test['runenv']['LIBRARY_PATH'] += ':' + test['runenv']['LDPATH']
        test['runenv']['LD_LIBRARY_PATH'] += ':' + test['runenv']['LDPATH']