From f347770b46f3859b1b9d607222eea0cfc7be9caa Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Thu, 2 Dec 2010 09:34:09 +0200 Subject: [PATCH] Use dpkg-vendor in ubuntutools.misc.system_distribution(), cache result. --- debian/changelog | 1 + ubuntutools/misc.py | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/debian/changelog b/debian/changelog index e1bbac5..d7d7470 100644 --- a/debian/changelog +++ b/debian/changelog @@ -17,6 +17,7 @@ ubuntu-dev-tools (0.107) UNRELEASED; urgency=low - Refactor to use subprocess.popen instead of os.system (LP: #398974) - Catch OSErrors when creating directories (LP: #671067) - Set HOME so pbuilder reads .pbuilderrc + * Use dpkg-vendor in ubuntutools.misc.system_distribution(), cache result. [ Benjamin Drung ] * wrap-and-sort: Remove duplicate items from sorted lists. diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index c59a10d..d61c85e 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -26,6 +26,7 @@ from subprocess import Popen, PIPE from ubuntutools.lp.udtexceptions import PocketDoesNotExistError +_system_distribution = None def system_distribution(): """ system_distro() -> string @@ -33,21 +34,22 @@ def system_distribution(): name of the distribution can't be determined, print an error message and return None. """ - # We try to avoid calling the "lsb_release" as looking up the value - # directly is faster. However, Debian doesn't have /etc/lsb-release - # so we need to fallback to the former there. - if os.path.isfile('/etc/lsb-release'): - for line in open('/etc/lsb-release'): - line = line.strip() - if line.startswith('DISTRIB_CODENAME'): - return line[17:] - else: - import commands - output = commands.getoutput('lsb_release -cs') - if output: - return output - print 'Error: Could not determine what distribution you are running.' - return None + global _system_distribution + if _system_distribution is None: + try: + if os.path.isfile('/usr/bin/dpkg-vendor'): + p = Popen(('dpkg-vendor', '--query', 'vendor'), stdout=PIPE) + else: + p = Popen(('lsb_release', '-cs'), stdout=PIPE) + output = p.communicate()[0] + except OSError: + print 'Error: Could not determine what distribution you are running.' + return None + if p.returncode != 0: + print 'Error determininng system distribution' + return None + _system_distribution = output.strip() + return _system_distribution def host_architecture(): """ host_architecture -> string