From d80f54dfa539632739d633031e961d293e19a1a9 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Mon, 20 Dec 2010 01:08:07 +0200 Subject: [PATCH] Revert r863, let's use environment variables again --- doc/backportpackage.1 | 2 +- doc/ubuntu-dev-tools.5 | 6 +++++- ubuntutools/config.py | 43 +++++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/doc/backportpackage.1 b/doc/backportpackage.1 index 4a44e06..be4407c 100644 --- a/doc/backportpackage.1 +++ b/doc/backportpackage.1 @@ -88,7 +88,7 @@ removed once the script finishes running. \fBbackportpackage\fR is only recommended for testing backports in a PPA, not uploading backports to the Ubuntu archive. .SH CONFIGURATION VARIABLES -The following variables can be set in the +The following variables can be set in the environment or in .BR ubuntu\-dev\-tools (5) configuration files: .TP diff --git a/doc/ubuntu-dev-tools.5 b/doc/ubuntu-dev-tools.5 index 5b6a1a5..2174b45 100644 --- a/doc/ubuntu-dev-tools.5 +++ b/doc/ubuntu-dev-tools.5 @@ -35,7 +35,11 @@ them by using \fB\-\-no\-conf\fR as the \fIfirst\fR command\-line option. .SH ENVIRONMENT -Several scripts use the following environment variables: +All \fBubuntu\-dev\-tools\fR configuration variables can be set (and +overridden) by setting them in the environment (unlike +\fBdevscripts\fR). + +In addition, several scripts use the following environment variables: .TP .B UBUMAIL diff --git a/ubuntutools/config.py b/ubuntutools/config.py index d9f611d..e4c4d2a 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -37,8 +37,6 @@ def get_devscripts_config(): dictionary """ config = {} - if len(sys.argv) > 1 and sys.argv[1] in ('--no-conf', '--noconf'): - return config var_re = re.compile(r'^\s*([A-Z_]+?)=(.+)$') for fn in ('/etc/devscripts.conf', '~/.devscripts'): f = open(os.path.expanduser(fn), 'r') @@ -49,32 +47,33 @@ def get_devscripts_config(): f.close() return config -def get_value(key, default=None, prefix=None, compat_vars=[]): - """Retrieve a value from a configuration file. +def get_value(key, default=None, prefix=None, compat_keys=[]): + """Retrieve a value from the environment or configuration files. keys are prefixed with the script name + _, or prefix. - Historical *environment variable* names can be supplied via compat_keys, no - prefix is applied to them. + + Store Priority: Environment variables, user config file, system config file + Variable Priority: PREFIX_KEY, UBUNTUTOOLS_KEY, compat_keys + + Historical variable names can be supplied via compat_keys, no prefix is + applied to them. """ + if default is None and key in defaults: + default = defaults[key] + if len(sys.argv) > 1 and sys.argv[1] in ('--no-conf', '--noconf'): + return default + if prefix is None: prefix = os.path.basename(sys.argv[0]).upper().replace('-', '_') + '_' - config = get_devscripts_config() - for k in (prefix + key, 'UBUNTUTOOLS_' + key): - if k in config: - value = config[k] - if value in ('yes', 'no'): - value = value == 'yes' - return value + keys = [prefix + key, 'UBUNTUTOOLS_' + key] + compat_keys - for k in compat_vars: - if k in os.environ: - value = os.environ[k] - if value in ('yes', 'no'): - value = value == 'yes' - return value - - if key in defaults: - return defaults[key] + for store in (os.environ, get_devscripts_config()): + for k in keys: + if k in store: + value = store[k] + if value in ('yes', 'no'): + value = value == 'yes' + return value return default def ubu_email(name=None, email=None, export=True):