2010-12-19 20:47:01 +02:00
|
|
|
# config.py - Common configuration file and environment variable handling for
|
|
|
|
# the ubuntu-dev-tools package.
|
|
|
|
#
|
2010-12-20 01:19:09 +02:00
|
|
|
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
|
2010-12-19 20:47:01 +02:00
|
|
|
#
|
2010-12-20 01:19:09 +02:00
|
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
2010-12-19 20:47:01 +02:00
|
|
|
#
|
2010-12-20 01:19:09 +02:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
# PERFORMANCE OF THIS SOFTWARE.
|
2010-12-19 20:47:01 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2010-12-19 20:47:37 +02:00
|
|
|
import pwd
|
2010-12-19 20:47:01 +02:00
|
|
|
import re
|
2010-12-20 23:36:17 +02:00
|
|
|
import shlex
|
2010-12-19 20:47:37 +02:00
|
|
|
import socket
|
2010-12-22 02:09:04 +02:00
|
|
|
import sys
|
2010-12-19 20:47:01 +02:00
|
|
|
|
2010-12-22 21:24:35 +02:00
|
|
|
from ubuntutools.logger import Logger
|
|
|
|
|
2010-12-20 22:08:12 +02:00
|
|
|
class UDTConfig(object):
|
2010-12-20 22:38:53 +02:00
|
|
|
"""Ubuntu Dev Tools configuration file (devscripts config file) and
|
|
|
|
environment variable parsing.
|
|
|
|
"""
|
|
|
|
no_conf = False
|
2010-12-21 01:56:28 +02:00
|
|
|
# Package wide configuration variables.
|
|
|
|
# These are reqired to be used by at least two scripts.
|
2010-12-20 22:08:12 +02:00
|
|
|
defaults = {
|
|
|
|
'BUILDER': 'pbuilder',
|
|
|
|
'LPINSTANCE': 'production',
|
2010-12-21 01:56:28 +02:00
|
|
|
'UPDATE_BUILDER': False,
|
|
|
|
'WORKDIR': None,
|
2010-12-20 22:08:12 +02:00
|
|
|
}
|
2010-12-21 01:14:57 +02:00
|
|
|
# Populated from the configuration files:
|
|
|
|
config = {}
|
2010-12-20 22:08:12 +02:00
|
|
|
|
|
|
|
def __init__(self, no_conf=False, prefix=None):
|
|
|
|
self.no_conf = no_conf
|
|
|
|
if prefix is None:
|
2010-12-22 02:09:04 +02:00
|
|
|
prefix = os.path.basename(sys.argv[0]).upper().replace('-', '_')
|
2010-12-20 22:08:12 +02:00
|
|
|
self.prefix = prefix
|
|
|
|
if not no_conf:
|
|
|
|
self.config = self.parse_devscripts_config()
|
|
|
|
|
|
|
|
def parse_devscripts_config(self):
|
|
|
|
"""Read the devscripts configuration files, and return the values as a
|
|
|
|
dictionary
|
|
|
|
"""
|
|
|
|
config = {}
|
|
|
|
for fn in ('/etc/devscripts.conf', '~/.devscripts'):
|
2010-12-20 23:36:17 +02:00
|
|
|
try:
|
|
|
|
f = open(os.path.expanduser(fn), 'r')
|
|
|
|
except IOError:
|
|
|
|
continue
|
2010-12-20 22:08:12 +02:00
|
|
|
for line in f:
|
2010-12-20 23:36:17 +02:00
|
|
|
parsed = shlex.split(line, comments=True)
|
2010-12-22 00:07:48 +02:00
|
|
|
if len(parsed) > 1:
|
2010-12-22 21:24:35 +02:00
|
|
|
Logger.warn('Cannot parse variable assignment in %s: %s',
|
|
|
|
getattr(f, 'name', '<config>'), line)
|
2010-12-20 23:36:17 +02:00
|
|
|
if len(parsed) >= 1 and '=' in parsed[0]:
|
|
|
|
key, value = parsed[0].split('=', 1)
|
|
|
|
config[key] = value
|
2010-12-20 22:08:12 +02:00
|
|
|
f.close()
|
|
|
|
return config
|
2010-12-20 01:08:07 +02:00
|
|
|
|
2010-12-21 01:51:10 +02:00
|
|
|
def get_value(self, key, default=None, boolean=False, compat_keys=[]):
|
2010-12-20 22:08:12 +02:00
|
|
|
"""Retrieve a value from the environment or configuration files.
|
|
|
|
keys are prefixed with the script name, falling back to UBUNTUTOOLS for
|
|
|
|
package-wide keys.
|
2010-12-19 20:47:01 +02:00
|
|
|
|
2010-12-20 22:08:12 +02:00
|
|
|
Variable Priority: PREFIX_KEY, UBUNTUTOOLS_KEY, compat_keys
|
2010-12-21 00:28:31 +02:00
|
|
|
Store Priority: Environment variables, user conf, system conf
|
2010-12-20 22:08:12 +02:00
|
|
|
|
|
|
|
Historical variable names can be supplied via compat_keys, no prefix is
|
|
|
|
applied to them.
|
|
|
|
"""
|
|
|
|
if default is None and key in self.defaults:
|
|
|
|
default = self.defaults[key]
|
|
|
|
|
2010-12-21 00:13:39 +02:00
|
|
|
keys = [self.prefix + '_' + key]
|
|
|
|
if key in self.defaults:
|
|
|
|
keys.append('UBUNTUTOOLS_' + key)
|
|
|
|
keys += compat_keys
|
2010-12-20 22:08:12 +02:00
|
|
|
|
2010-12-21 00:28:31 +02:00
|
|
|
for k in keys:
|
|
|
|
for store in (os.environ, self.config):
|
2010-12-20 22:08:12 +02:00
|
|
|
if k in store:
|
|
|
|
value = store[k]
|
2010-12-21 01:51:10 +02:00
|
|
|
if boolean:
|
|
|
|
if value in ('yes', 'no'):
|
|
|
|
value = value == 'yes'
|
|
|
|
else:
|
|
|
|
continue
|
2010-12-22 00:07:48 +02:00
|
|
|
if k in compat_keys:
|
2010-12-22 02:09:04 +02:00
|
|
|
replacements = self.prefix + '_' + key
|
2010-12-22 00:14:28 +02:00
|
|
|
if key in self.defaults:
|
2010-12-22 02:09:04 +02:00
|
|
|
replacements += 'or UBUNTUTOOLS_' + key
|
2010-12-22 21:24:35 +02:00
|
|
|
Logger.warn(
|
2010-12-22 02:09:04 +02:00
|
|
|
'Using deprecated configuration variable %s. '
|
|
|
|
'You should use %s.',
|
|
|
|
k, replacements)
|
2010-12-20 22:08:12 +02:00
|
|
|
return value
|
|
|
|
return default
|
2010-12-19 22:08:25 +02:00
|
|
|
|
2010-12-19 20:47:37 +02:00
|
|
|
|
|
|
|
def ubu_email(name=None, email=None, export=True):
|
|
|
|
"""Find the developer's Ubuntu e-mail address, and export it in
|
|
|
|
DEBFULLNAME, DEBEMAIL if necessary (and export isn't False).
|
|
|
|
|
2010-12-21 12:59:45 +02:00
|
|
|
e-mail Priority: arguments, UBUMAIL, DEBEMAIL, EMAIL, user@mailname
|
2010-12-19 20:47:37 +02:00
|
|
|
name Priority: arguments, UBUMAIL, DEBFULLNAME, DEBEMAIL, NAME, /etc/passwd
|
|
|
|
|
|
|
|
Name and email are only exported if provided as arguments or found in
|
|
|
|
UBUMAIL. Otherwise, wrapped devscripts scripts can be expected to determine
|
|
|
|
the values themselves.
|
|
|
|
|
2010-12-20 23:56:54 +02:00
|
|
|
Return name, email.
|
2010-12-19 20:47:37 +02:00
|
|
|
"""
|
|
|
|
name_email_re = re.compile(r'^\s*(.+?)\s*<(.+@.+)>\s*$')
|
|
|
|
|
|
|
|
if email:
|
|
|
|
m = name_email_re.match(email)
|
2010-12-20 23:56:54 +02:00
|
|
|
if m and not name:
|
|
|
|
name = m.group(1)
|
2010-12-19 20:47:37 +02:00
|
|
|
email = m.group(2)
|
|
|
|
|
|
|
|
if export and not name and not email and 'UBUMAIL' not in os.environ:
|
|
|
|
export = False
|
|
|
|
|
2010-12-21 00:18:47 +02:00
|
|
|
for var, target in (('UBUMAIL', 'email'),
|
2010-12-19 20:47:37 +02:00
|
|
|
('DEBFULLNAME', 'name'),
|
2010-12-20 23:56:54 +02:00
|
|
|
('DEBEMAIL', 'email'),
|
2010-12-21 12:59:45 +02:00
|
|
|
('EMAIL', 'email'),
|
2010-12-19 20:47:37 +02:00
|
|
|
('NAME', 'name'),
|
|
|
|
):
|
|
|
|
if name and email:
|
|
|
|
break
|
2010-12-21 00:18:47 +02:00
|
|
|
if var in os.environ:
|
|
|
|
m = name_email_re.match(os.environ[var])
|
|
|
|
if m:
|
|
|
|
if not name:
|
|
|
|
name = m.group(1)
|
|
|
|
if not email:
|
|
|
|
email = m.group(2)
|
|
|
|
elif target == 'name' and not name:
|
2010-12-19 20:47:37 +02:00
|
|
|
name = os.environ[var].strip()
|
2010-12-21 00:18:47 +02:00
|
|
|
elif target == 'email' and not email:
|
|
|
|
email = os.environ[var].strip()
|
2010-12-19 20:47:37 +02:00
|
|
|
|
|
|
|
if not name:
|
2010-12-21 00:24:03 +02:00
|
|
|
gecos_name = pwd.getpwuid(os.getuid()).pw_gecos.split(',')[0].strip()
|
2010-12-19 20:47:37 +02:00
|
|
|
if gecos_name:
|
|
|
|
name = gecos_name
|
|
|
|
|
|
|
|
if not email:
|
|
|
|
mailname = socket.getfqdn()
|
|
|
|
if os.path.isfile('/etc/mailname'):
|
|
|
|
mailname = open('/etc/mailname', 'r').read().strip()
|
2010-12-21 00:24:03 +02:00
|
|
|
email = pwd.getpwuid(os.getuid()).pw_name + '@' + mailname
|
2010-12-19 20:47:37 +02:00
|
|
|
|
|
|
|
if export:
|
|
|
|
os.environ['DEBFULLNAME'] = name
|
|
|
|
os.environ['DEBEMAIL'] = email
|
|
|
|
return name, email
|