From 3a9ffe412c0eb42b2d31c03e055c57b06a25bb3a Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 19 Dec 2010 20:47:37 +0200 Subject: [PATCH] Support the combined "Name " format in UBUMAIL, DEBFULLNAME, and DEBEMAIL. (LP: #665202) --- debian/changelog | 4 ++- ubuntutools/config.py | 77 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 7a3b596..cb27ceb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,8 +4,10 @@ ubuntu-dev-tools (0.109) UNRELEASED; urgency=low (LP: #692003) * Support reading configuration variables from devscripts configuration files. (LP: #681693) + * Support the combined "Name " format in UBUMAIL, DEBFULLNAME, and + DEBEMAIL. (LP: #665202) - -- Stefano Rivera Sun, 19 Dec 2010 20:41:06 +0200 + -- Stefano Rivera Sun, 19 Dec 2010 20:47:22 +0200 ubuntu-dev-tools (0.108) experimental; urgency=low diff --git a/ubuntutools/config.py b/ubuntutools/config.py index 391b058..b6223de 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -18,7 +18,9 @@ import os import os.path +import pwd import re +import socket import sys from ubuntutools.common import memoize_noargs @@ -59,3 +61,78 @@ def get_value(key, default=None, prefix=None, compat_keys=[]): value = value == 'yes' return value return value + +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). + + e-mail Priority: arguments, UBUMAIL, DEBEMAIL, DEBFULLNAME, user@mailname + 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. + + Return email, name. + """ + name_email_re = re.compile(r'^\s*(.+?)\s*<(.+@.+)>\s*$') + + # First priority is to sanity-check command-line supplied values: + if name: + name = name.strip() + if email: + email = email.strip() + if name: + m = name_email_re.match(name) + if m: + name = m.group(1) + if not email: + email = m.group(2) + if email: + m = name_email_re.match(email) + if m: + if not name: + name = m.group(1) + email = m.group(2) + + if export and not name and not email and 'UBUMAIL' not in os.environ: + export = False + + for var, target in ( + ('UBUMAIL', 'name'), + ('UBUMAIL', 'email'), + ('DEBEMAIL', 'email'), + ('DEBFULLNAME', 'name'), + ('DEBEMAIL', 'name'), + ('DEBFULLNAME', 'email'), + ('NAME', 'name'), + ): + if name and email: + break + if var in os.environ and not locals()[target]: + m = name_email_re.match(os.environ[var]) + if m: + if target == 'name': + name = m.group(1) + elif target == 'email': + email = m.group(2) + elif var.endswith('MAIL') and target == 'email': + email = os.environ[var].strip() + elif var.endswith('NAME') and target == 'name': + name = os.environ[var].strip() + + if not name: + gecos_name = pwd.getpwuid(os.getuid())[4].split(',')[0].strip() + 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() + email = pwd.getpwuid(os.getuid())[0] + '@' + mailname + + if export: + os.environ['DEBFULLNAME'] = name + os.environ['DEBEMAIL'] = email + return name, email