From 8d3c2569eaa5c669ccb5af51d684dfb1f245ea86 Mon Sep 17 00:00:00 2001 From: John Chilton Date: Mon, 22 Sep 2014 19:14:12 -0400 Subject: [PATCH] Fix ORM scripts for 42f677bab2e4880b62758614fcb2c7dc27013788. Updating common Python module used by ORM scripts (create_db.sh/migrate_db.sh/db_shell.py) to allow the same configuration via enviornment variables as well. Thanks to Bjoern for the bug report/feature request. --- lib/galaxy/config.py | 16 ++++--------- lib/galaxy/model/orm/scripts.py | 14 +++++------ lib/galaxy/util/properties.py | 41 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 lib/galaxy/util/properties.py diff --git a/lib/galaxy/config.py b/lib/galaxy/config.py index 571bf3e8557..bea6d458c7e 100644 --- a/lib/galaxy/config.py +++ b/lib/galaxy/config.py @@ -13,6 +13,7 @@ import logging.config import ConfigParser from datetime import timedelta from galaxy.web.formatting import expand_pretty_datetime_format +from galaxy.util.properties import load_app_properties from galaxy.util import string_as_bool from galaxy.util import listify from galaxy.util.dbkeys import GenomeBuilds @@ -20,9 +21,6 @@ from galaxy import eggs log = logging.getLogger( __name__ ) -CONFIG_DEFAULT_PREFIX = "GALAXY_CONFIG_" -CONFIG_OVERRIDE_PREFIX = "GALAXY_CONFIG_OVERRIDE_" - def resolve_path( path, root ): """If 'path' is relative make absolute by prepending 'root'""" @@ -39,15 +37,9 @@ class Configuration( object ): deprecated_options = ( 'database_file', ) def __init__( self, **kwargs ): - for key in os.environ: - if key.startswith( CONFIG_OVERRIDE_PREFIX ): - config_key = key[ len( CONFIG_OVERRIDE_PREFIX ): ].lower() - kwargs[ config_key ] = os.environ[ key ] - elif key.startswith( CONFIG_DEFAULT_PREFIX ): - config_key = key[ len( CONFIG_DEFAULT_PREFIX ): ].lower() - if config_key not in kwargs: - kwargs[ config_key ] = os.environ[ key ] - + kwargs = load_app_properties( + kwds=kwargs + ) self.config_dict = kwargs self.root = kwargs.get( 'root_dir', '.' ) diff --git a/lib/galaxy/model/orm/scripts.py b/lib/galaxy/model/orm/scripts.py index 0b399ac87fa..a05b161fd45 100644 --- a/lib/galaxy/model/orm/scripts.py +++ b/lib/galaxy/model/orm/scripts.py @@ -3,7 +3,6 @@ Code to support database helper scripts (create_db.py, manage_db.py, etc...). """ import logging import os.path -from ConfigParser import SafeConfigParser from galaxy import eggs @@ -12,6 +11,7 @@ eggs.require( "Tempita" ) eggs.require( "SQLAlchemy" ) eggs.require( "sqlalchemy_migrate" ) +from galaxy.util.properties import load_app_properties from galaxy.model.orm import dialect_to_egg import pkg_resources @@ -112,13 +112,13 @@ def get_config( argv, cwd=None ): if cwd: config_file = os.path.join( cwd, config_file ) - cp = SafeConfigParser() - cp.read( config_file ) + properties = load_app_properties( ini_file=config_file ) - if cp.has_option( "app:main", "%sdatabase_connection" % config_prefix): - db_url = cp.get( "app:main", "%sdatabase_connection" % config_prefix ) - elif cp.has_option( "app:main", "%sdatabase_file" % config_prefix ): - db_url = "sqlite:///%s?isolation_level=IMMEDIATE" % cp.get( "app:main", "database_file" ) + if ("%sdatabase_connection" % config_prefix) in properties: + db_url = properties[ "%sdatabase_connection" % config_prefix ] + elif ("%sdatabase_file" % config_prefix) in properties: + database_file = properties[ "%sdatabase_file" % config_prefix ] + db_url = "sqlite:///%s?isolation_level=IMMEDIATE" % database_file else: db_url = "sqlite:///%s?isolation_level=IMMEDIATE" % default_sqlite_file diff --git a/lib/galaxy/util/properties.py b/lib/galaxy/util/properties.py new file mode 100644 index 00000000000..1dad1130bed --- /dev/null +++ b/lib/galaxy/util/properties.py @@ -0,0 +1,41 @@ +""" Module used to blend ini, environment, and explicit dictionary properties +to determine application configuration. Some hard coded defaults for Galaxy but +this should be reusable by tool shed and pulsar as well. +""" +import os +import os.path + + +def load_app_properties( + kwds={}, + ini_file=None, + ini_section="app:main", + config_prefix="GALAXY_CONFIG_" +): + properties = kwds.copy() if kwds else {} + if ini_file: + defaults = { + 'here': os.path.dirname(os.path.abspath(ini_file)), + '__file__': os.path.abspath(ini_file) + } + # Mimic the way loadwsgi loads configuration files - needed for + # correctness given the very specific ways interpolation is handled. + from galaxy.util.pastescript.loadwsgi import NicerConfigParser + parser = NicerConfigParser(ini_file, defaults=defaults) + parser.optionxform = str # Don't lower-case keys + with open(ini_file) as f: + parser.read_file(f) + + properties.update( dict( parser.items( ini_section ) ) ) + + override_prefix = "%sOVERRIDE_" % config_prefix + for key in os.environ: + if key.startswith( override_prefix ): + config_key = key[ len( override_prefix ): ].lower() + properties[ config_key ] = os.environ[ key ] + elif key.startswith( config_prefix ): + config_key = key[ len( config_prefix ): ].lower() + if config_key not in properties: + properties[ config_key ] = os.environ[ key ] + + return properties