source: cows_wps/trunk/cows_wps/utils/setenv.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/utils/setenv.py@5615
Revision 5615, 2.7 KB checked in by spascoe, 10 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

RevLine 
[5615]1"""
2Set environment variables according to pylons configuration.
3
4Bootstraping the WPS configuration can be difficult as various environment
5variables need to be set.  This module allows all configuration to be centralised
6in pylons.config without extra wrapper scripts.
7
8"""
9
10import os, sys, commands
11
12import vncctrl
13
14import logging
15log = logging.getLogger(__name__)
16
17def setenv_wps(wps_config_dict=None,
18               withGraphics=True):
19    """
20    Set os.environ according to values in wps_config_dict
21   
22    """
23    if wps_config_dict is None:
24        from parse_wps_config import wps_config_dict
25   
26    if withGraphics:
27        setenv_vnc(wps_config_dict)
28        setenv_mpl(wps_config_dict)
29        setenv_R(wps_config_dict)
30    setenv_sge(wps_config_dict)
31    setenv_nappy(wps_config_dict)
32
33def setenv_vnc(wps_config_dict):
34    # USER isn't set within mod_wsgi
35    if 'USER' not in os.environ:
36        os.environ['USER'] = wps_config_dict['user']
37    vnc_home = wps_config_dict['vnc_user_dir']
38    vncctrl.initDisplay(vncUserDir=vnc_home)
39
40def setenv_mpl(wps_config_dict):
41    os.environ['MPLCONFIGDIR'] = wps_config_dict['mpl_configdir']
42   
43   
44def setenv_sge(wps_config_dict):   
45    sge_root = wps_config_dict['sge_root']
46    os.environ['SGE_ROOT'] = sge_root
47    os.environ['SGE_CELL'] = wps_config_dict.get('sge_cell', 'default')
48
49    # Set path for calling qstat
50    arch = commands.getoutput('%s/util/arch' % sge_root)
51    log.debug('SGE ARCH = %s' % arch)
52    path = os.environ['PATH'].split(':')
53    path[:0] = ['%s/bin/%s' % (sge_root, arch)]
54
55    log.debug('PATH set to %s' % ':'.join(path))
56    os.environ['PATH'] = ':'.join(path)
57
58def setenv_nappy(wps_config_dict):
59    # Check nappy hasn't been imported yet.  If it has warn
60    # nappy cannot be configured.
61    if sys.modules.has_key('nappy'):
62        log.warn('nappy already imported.  Setting nappy environment may have no effect')
63   
64    mapping = (('NAPPY_LOCAL_ATTRIBUTES', 'nappy_locals'),
65               ('NAPPY_ANNOTATIONS', 'nappy_annotations'),
66               )
67    for env_var, config_var in mapping:
68        try:
69            v = wps_config_dict[config_var]
70        except KeyError:
71            if env_var not in os.environ:
72                log.warn('nappy environment variable %s not present in environment or wps_config_dict' % env_var)
73        else:
74            log.debug('setenv %s=%s' % (env_var, v))
75            os.environ[env_var] = v
76                 
77def setenv_R(wps_config_dict):
78    #!TODO: make this configurable.
79    # This is explicitly for ddp-app at the moment
80    if 'PATH' in os.environ:
81        os.environ['PATH'] = '%s:/common/bin' % os.environ['PATH']
82    else:
83        os.environ['PATH'] = '/common/bin'
84
Note: See TracBrowser for help on using the repository browser.