From a335ef7949ae24e2b2966af7c0f69d3ae93ff036 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 19 Dec 2010 20:47:01 +0200 Subject: [PATCH] Support reading configuration variables from devscripts configuration files. (LP: #681693) --- debian/changelog | 6 +++-- ubuntutools/common.py | 10 +++++++ ubuntutools/config.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 ubuntutools/config.py diff --git a/debian/changelog b/debian/changelog index dae1a49..7a3b596 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,9 +1,11 @@ ubuntu-dev-tools (0.109) UNRELEASED; urgency=low * Convert debian/copyright to DEP5, make sure all scripts are listed - (LP: #692003) + (LP: #692003) + * Support reading configuration variables from devscripts configuration + files. (LP: #681693) - -- Stefano Rivera Sun, 19 Dec 2010 02:26:12 +0200 + -- Stefano Rivera Sun, 19 Dec 2010 20:41:06 +0200 ubuntu-dev-tools (0.108) experimental; urgency=low diff --git a/ubuntutools/common.py b/ubuntutools/common.py index 1be265f..490a518 100644 --- a/ubuntutools/common.py +++ b/ubuntutools/common.py @@ -32,3 +32,13 @@ import sys if os.environ.has_key('https_proxy'): print >> sys.stderr, "Ignoring https_proxy (no support in urllib/urllib2; see LP #122551)" del os.environ['https_proxy'] + + +def memoize_noargs(func): + "Simple memoization wrapper, for functions without arguments" + func.cache = None + def wrapper(): + if func.cache is None: + func.cache = func() + return func.cache + return wrapper diff --git a/ubuntutools/config.py b/ubuntutools/config.py new file mode 100644 index 0000000..391b058 --- /dev/null +++ b/ubuntutools/config.py @@ -0,0 +1,61 @@ +# config.py - Common configuration file and environment variable handling for +# the ubuntu-dev-tools package. +# +# Copyright (C) 2010 Stefano Rivera +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# See file /usr/share/common-licenses/GPL-2 for more details. +# + +import os +import os.path +import re +import sys + +from ubuntutools.common import memoize_noargs + +@memoize_noargs +def get_devscripts_config(): + """Read the devscripts configuration files, and return the values as a + dictionary + """ + config = {} + var_re = re.compile(r'^\s*([A-Z_]+?)=(.+)$') + for fn in ('/etc/devscripts.conf', '~/.devscripts'): + f = open(os.path.expanduser(fn), 'r') + for line in f: + m = var_re.match(line) + if m: + config[m.group(1)] = m.group(2) + f.close() + return config + +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 variable names can be supplied via compat_keys, no prefix is + applied to them. + """ + if prefix is None: + prefix = sys.argv[0].upper().replace('-', '_') + '_' + + keys = [prefix + key, 'UBUNTUTOOLS_' + key] + compat_keys + + value = default + 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 value