PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/webapps/community/config.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 174 lines | 164 code | 4 blank | 6 comment | 1 complexity | cc1db2bb9c452fa8414aea7ce824adc8 MD5 | raw file
  1"""
  2Universe configuration builder.
  3"""
  4
  5import sys, os
  6import logging, logging.config
  7from optparse import OptionParser
  8import ConfigParser
  9from galaxy.util import string_as_bool
 10
 11from galaxy import eggs
 12import pkg_resources
 13
 14log = logging.getLogger( __name__ )
 15
 16def resolve_path( path, root ):
 17    """If 'path' is relative make absolute by prepending 'root'"""
 18    if not( os.path.isabs( path ) ):
 19        path = os.path.join( root, path )
 20    return path
 21
 22class ConfigurationError( Exception ):
 23    pass
 24
 25class Configuration( object ):
 26    def __init__( self, **kwargs ):
 27        self.config_dict = kwargs
 28        self.root = kwargs.get( 'root_dir', '.' )
 29        # Collect the umask and primary gid from the environment
 30        self.umask = os.umask( 077 ) # get the current umask
 31        os.umask( self.umask ) # can't get w/o set, so set it back
 32        self.gid = os.getgid() # if running under newgrp(1) we'll need to fix the group of data created on the cluster
 33        # Database related configuration
 34        self.database = resolve_path( kwargs.get( "database_file", "database/universe.d" ), self.root )
 35        self.database_connection =  kwargs.get( "database_connection", False )
 36        self.database_engine_options = get_database_engine_options( kwargs )                        
 37        self.database_create_tables = string_as_bool( kwargs.get( "database_create_tables", "True" ) )
 38        # Where dataset files are stored
 39        self.file_path = resolve_path( kwargs.get( "file_path", "database/files" ), self.root )
 40        self.new_file_path = resolve_path( kwargs.get( "new_file_path", "database/tmp" ), self.root )
 41        self.cookie_path = kwargs.get( "cookie_path", "/" )
 42        # web API
 43        self.enable_api = string_as_bool( kwargs.get( 'enable_api', False ) )
 44        self.datatypes_config = kwargs.get( 'datatypes_config_file', 'datatypes_conf.xml' )
 45        self.test_conf = resolve_path( kwargs.get( "test_conf", "" ), self.root )
 46        self.id_secret = kwargs.get( "id_secret", "USING THE DEFAULT IS NOT SECURE!" )
 47        # Tool stuff
 48        self.tool_secret = kwargs.get( "tool_secret", "" )
 49        self.tool_data_path = resolve_path( kwargs.get( "tool_data_path", "tool-data" ), os.getcwd() )
 50        self.tool_data_table_config_path = resolve_path( kwargs.get( 'tool_data_table_config_path', 'tool_data_table_conf.xml' ), self.root )
 51        self.ftp_upload_dir = kwargs.get( 'ftp_upload_dir', None )
 52        # Location for dependencies
 53        if 'tool_dependency_dir' in kwargs:
 54            self.tool_dependency_dir = resolve_path( kwargs.get( "tool_dependency_dir" ), self.root )
 55            self.use_tool_dependencies = True
 56        else:
 57            self.tool_dependency_dir = None
 58            self.use_tool_dependencies = False
 59        self.use_remote_user = string_as_bool( kwargs.get( "use_remote_user", "False" ) )
 60        self.remote_user_maildomain = kwargs.get( "remote_user_maildomain", None )
 61        self.remote_user_logout_href = kwargs.get( "remote_user_logout_href", None )
 62        self.require_login = string_as_bool( kwargs.get( "require_login", "False" ) )
 63        self.allow_user_creation = string_as_bool( kwargs.get( "allow_user_creation", "True" ) )
 64        self.enable_openid = string_as_bool( kwargs.get( 'enable_openid', False ) )
 65        self.template_path = resolve_path( kwargs.get( "template_path", "templates" ), self.root )
 66        self.template_cache = resolve_path( kwargs.get( "template_cache_path", "database/compiled_templates/community" ), self.root )
 67        self.admin_users = kwargs.get( "admin_users", "" )
 68        self.sendmail_path = kwargs.get('sendmail_path',"/usr/sbin/sendmail")
 69        self.mailing_join_addr = kwargs.get('mailing_join_addr',"galaxy-user-join@bx.psu.edu")
 70        self.error_email_to = kwargs.get( 'error_email_to', None )
 71        self.smtp_server = kwargs.get( 'smtp_server', None )
 72        self.smtp_username = kwargs.get( 'smtp_username', None )
 73        self.smtp_password = kwargs.get( 'smtp_password', None )
 74        self.start_job_runners = kwargs.get( 'start_job_runners', None )
 75        self.email_from = kwargs.get( 'email_from', None )
 76        self.nginx_upload_path = kwargs.get( 'nginx_upload_path', False )
 77        self.log_actions = string_as_bool( kwargs.get( 'log_actions', 'False' ) )
 78        self.brand = kwargs.get( 'brand', None )
 79        self.support_url = kwargs.get( 'support_url', 'http://wiki.g2.bx.psu.edu/Support' )
 80        self.wiki_url = kwargs.get( 'wiki_url', 'http://wiki.g2.bx.psu.edu/FrontPage' )
 81        self.blog_url = kwargs.get( 'blog_url', None )
 82        self.screencasts_url = kwargs.get( 'screencasts_url', None )
 83        self.log_events = False
 84        self.cloud_controller_instance = False
 85        # Proxy features
 86        self.apache_xsendfile = kwargs.get( 'apache_xsendfile', False )
 87        self.nginx_x_accel_redirect_base = kwargs.get( 'nginx_x_accel_redirect_base', False )
 88        # Parse global_conf and save the parser
 89        global_conf = kwargs.get( 'global_conf', None )
 90        global_conf_parser = ConfigParser.ConfigParser()
 91        self.global_conf_parser = global_conf_parser
 92        if global_conf and "__file__" in global_conf:
 93            global_conf_parser.read(global_conf['__file__'])
 94    def get( self, key, default ):
 95        return self.config_dict.get( key, default )
 96    def get_bool( self, key, default ):
 97        if key in self.config_dict:
 98            return string_as_bool( self.config_dict[key] )
 99        else:
100            return default
101    def check( self ):
102        # Check that required directories exist
103        for path in self.root, self.file_path, self.template_path:
104            if not os.path.isdir( path ):
105                raise ConfigurationError("Directory does not exist: %s" % path )
106    def is_admin_user( self, user ):
107        """
108        Determine if the provided user is listed in `admin_users`.
109        """
110        admin_users = self.get( "admin_users", "" ).split( "," )
111        return user is not None and user.email in admin_users
112
113def get_database_engine_options( kwargs ):
114    """
115    Allow options for the SQLAlchemy database engine to be passed by using
116    the prefix "database_engine_option_".
117    """
118    conversions =  {
119        'convert_unicode': string_as_bool,
120        'pool_timeout': int,
121        'echo': string_as_bool,
122        'echo_pool': string_as_bool,
123        'pool_recycle': int,
124        'pool_size': int,
125        'max_overflow': int,
126        'pool_threadlocal': string_as_bool,
127        'server_side_cursors': string_as_bool
128    }
129    prefix = "database_engine_option_"
130    prefix_len = len( prefix )
131    rval = {}
132    for key, value in kwargs.iteritems():
133        if key.startswith( prefix ):
134            key = key[prefix_len:]
135            if key in conversions:
136                value = conversions[key](value)
137            rval[ key  ] = value
138    return rval
139
140def configure_logging( config ):
141    """
142    Allow some basic logging configuration to be read from the cherrpy
143    config.
144    """
145    # PasteScript will have already configured the logger if the appropriate
146    # sections were found in the config file, so we do nothing if the
147    # config has a loggers section, otherwise we do some simple setup
148    # using the 'log_*' values from the config.
149    if config.global_conf_parser.has_section( "loggers" ):
150        return
151    format = config.get( "log_format", "%(name)s %(levelname)s %(asctime)s %(message)s" )
152    level = logging._levelNames[ config.get( "log_level", "DEBUG" ) ]
153    destination = config.get( "log_destination", "stdout" )
154    log.info( "Logging at '%s' level to '%s'" % ( level, destination ) )
155    # Get root logger
156    root = logging.getLogger()
157    # Set level
158    root.setLevel( level )
159    # Turn down paste httpserver logging
160    if level <= logging.DEBUG:
161        logging.getLogger( "paste.httpserver.ThreadPool" ).setLevel( logging.WARN )
162    # Remove old handlers
163    for h in root.handlers[:]: 
164        root.removeHandler(h)
165    # Create handler
166    if destination == "stdout":
167        handler = logging.StreamHandler( sys.stdout )
168    else:
169        handler = logging.FileHandler( destination )
170    # Create formatter
171    formatter = logging.Formatter( format )    
172    # Hook everything up
173    handler.setFormatter( formatter )
174    root.addHandler( handler )