1 | """Universe configuration builder.""" |
---|
2 | import sys, os, logging, logging.config, ConfigParser |
---|
3 | from optparse import OptionParser |
---|
4 | from galaxy.util import string_as_bool |
---|
5 | |
---|
6 | log = logging.getLogger( __name__ ) |
---|
7 | |
---|
8 | def resolve_path( path, root ): |
---|
9 | """If 'path' is relative make absolute by prepending 'root'""" |
---|
10 | if not( os.path.isabs( path ) ): |
---|
11 | path = os.path.join( root, path ) |
---|
12 | return path |
---|
13 | |
---|
14 | class ConfigurationError( Exception ): |
---|
15 | pass |
---|
16 | |
---|
17 | class Configuration( object ): |
---|
18 | def __init__( self, **kwargs ): |
---|
19 | self.config_dict = kwargs |
---|
20 | self.root = kwargs.get( 'root_dir', '.' ) |
---|
21 | # Database related configuration |
---|
22 | self.database = resolve_path( kwargs.get( "database_file", "database/universe.d" ), self.root ) |
---|
23 | self.database_connection = kwargs.get( "database_connection", False ) |
---|
24 | self.database_engine_options = get_database_engine_options( kwargs ) |
---|
25 | # Where dataset files are stored |
---|
26 | self.file_path = resolve_path( kwargs.get( "file_path", "database/files" ), self.root ) |
---|
27 | self.new_file_path = resolve_path( kwargs.get( "new_file_path", "database/tmp" ), self.root ) |
---|
28 | self.id_secret = kwargs.get( "id_secret", "USING THE DEFAULT IS NOT SECURE!" ) |
---|
29 | self.use_remote_user = string_as_bool( kwargs.get( "use_remote_user", "False" ) ) |
---|
30 | self.require_login = string_as_bool( kwargs.get( "require_login", "False" ) ) |
---|
31 | self.template_path = resolve_path( kwargs.get( "template_path", "templates" ), self.root ) |
---|
32 | self.template_cache = resolve_path( kwargs.get( "template_cache_path", "database/compiled_templates/reports" ), self.root ) |
---|
33 | self.sendmail_path = kwargs.get('sendmail_path',"/usr/sbin/sendmail") |
---|
34 | self.log_actions = string_as_bool( kwargs.get( 'log_actions', 'False' ) ) |
---|
35 | self.brand = kwargs.get( 'brand', None ) |
---|
36 | self.wiki_url = kwargs.get( 'wiki_url', 'http://bitbucket.org/galaxy/galaxy-central/wiki/Home' ) |
---|
37 | self.bugs_email = kwargs.get( 'bugs_email', None ) |
---|
38 | self.blog_url = kwargs.get( 'blog_url', None ) |
---|
39 | self.screencasts_url = kwargs.get( 'screencasts_url', None ) |
---|
40 | self.log_events = False |
---|
41 | self.cookie_path = kwargs.get( "cookie_path", "/" ) |
---|
42 | #Parse global_conf |
---|
43 | global_conf = kwargs.get( 'global_conf', None ) |
---|
44 | global_conf_parser = ConfigParser.ConfigParser() |
---|
45 | if global_conf and "__file__" in global_conf: |
---|
46 | global_conf_parser.read(global_conf['__file__']) |
---|
47 | def get( self, key, default ): |
---|
48 | return self.config_dict.get( key, default ) |
---|
49 | def check( self ): |
---|
50 | # Check that required directories exist |
---|
51 | for path in self.root, self.file_path, self.template_path: |
---|
52 | if not os.path.isdir( path ): |
---|
53 | raise ConfigurationError("Directory does not exist: %s" % path ) |
---|
54 | |
---|
55 | def get_database_engine_options( kwargs ): |
---|
56 | """ |
---|
57 | Allow options for the SQLAlchemy database engine to be passed by using |
---|
58 | the prefix "database_engine_option_". |
---|
59 | """ |
---|
60 | conversions = { |
---|
61 | 'convert_unicode': string_as_bool, |
---|
62 | 'pool_timeout': int, |
---|
63 | 'echo': string_as_bool, |
---|
64 | 'echo_pool': string_as_bool, |
---|
65 | 'pool_recycle': int, |
---|
66 | 'pool_size': int, |
---|
67 | 'max_overflow': int, |
---|
68 | 'pool_threadlocal': string_as_bool |
---|
69 | } |
---|
70 | prefix = "database_engine_option_" |
---|
71 | prefix_len = len( prefix ) |
---|
72 | rval = {} |
---|
73 | for key, value in kwargs.iteritems(): |
---|
74 | if key.startswith( prefix ): |
---|
75 | key = key[prefix_len:] |
---|
76 | if key in conversions: |
---|
77 | value = conversions[key](value) |
---|
78 | rval[ key ] = value |
---|
79 | return rval |
---|
80 | |
---|
81 | def configure_logging( config ): |
---|
82 | """ |
---|
83 | Allow some basic logging configuration to be read from the cherrpy |
---|
84 | config. |
---|
85 | """ |
---|
86 | format = config.get( "log_format", "%(name)s %(levelname)s %(asctime)s %(message)s" ) |
---|
87 | level = logging._levelNames[ config.get( "log_level", "DEBUG" ) ] |
---|
88 | destination = config.get( "log_destination", "stdout" ) |
---|
89 | log.info( "Logging at '%s' level to '%s'" % ( level, destination ) ) |
---|
90 | # Get root logger |
---|
91 | root = logging.getLogger() |
---|
92 | # Set level |
---|
93 | root.setLevel( level ) |
---|
94 | # Turn down paste httpserver logging |
---|
95 | if level <= logging.DEBUG: |
---|
96 | logging.getLogger( "paste.httpserver.ThreadPool" ).setLevel( logging.WARN ) |
---|
97 | # Remove old handlers |
---|
98 | for h in root.handlers[:]: |
---|
99 | root.removeHandler(h) |
---|
100 | # Create handler |
---|
101 | if destination == "stdout": |
---|
102 | handler = logging.StreamHandler( sys.stdout ) |
---|
103 | else: |
---|
104 | handler = logging.FileHandler( destination ) |
---|
105 | # Create formatter |
---|
106 | formatter = logging.Formatter( format ) |
---|
107 | # Hook everything up |
---|
108 | handler.setFormatter( formatter ) |
---|
109 | root.addHandler( handler ) |
---|
110 | |
---|
111 | |
---|