From 2398d09ba42de7d684f37d6ea93d99a5c9d16e6f Mon Sep 17 00:00:00 2001 From: Evan Broder Date: Sat, 11 Jun 2011 05:05:35 -0700 Subject: [PATCH] ubuntutools.misc: Add a new "system_distribution_chain", which returns a list starting with the current distribution and working its way up each distribution's parent. --- debian/changelog | 7 +++++- ubuntutools/misc.py | 61 +++++++++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/debian/changelog b/debian/changelog index 515eb64..db65216 100644 --- a/debian/changelog +++ b/debian/changelog @@ -22,7 +22,12 @@ ubuntu-dev-tools (0.125) UNRELEASED; urgency=low * lp-project-upload: - fix a bug when new milestone wasn't specified - -- Benjamin Drung Sat, 28 May 2011 19:43:10 +0200 + [ Evan Broder ] + * ubuntutools.misc: Add a new "system_distribution_chain", which returns + a list starting with the current distribution and working its way up + each distribution's parent. + + -- Evan Broder Sat, 11 Jun 2011 05:05:21 -0700 ubuntu-dev-tools (0.124) unstable; urgency=low diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index 55c6551..df7fb3f 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -4,6 +4,7 @@ # Copyright (C) 2008, Jonathan Davies , # 2008-2009, Siegfried-Angel Gevatter Pujals , # 2010, Stefano Rivera +# 2011, Evan Broder # # ################################################################## # @@ -30,7 +31,46 @@ import sys from ubuntutools.lp.udtexceptions import PocketDoesNotExistError -_system_distribution = None +_system_distribution_chain = [] +def system_distribution_chain(): + """ system_distribution_chain() -> [string] + + Detect the system's distribution as well as all of its parent + distributions and return them as a list of strings, with the + system distribution first (and the greatest grandparent last). If + the distribution chain can't be determined, print an error message + and return an empty list. + """ + global _system_distribution_chain + if len(_system_distribution_chain) == 0: + try: + p = Popen(('dpkg-vendor', '--query', 'Vendor'), + stdout=PIPE) + _system_distribution_chain.append(p.communicate()[0].strip()) + except OSError: + print ('Error: Could not determine what distribution you are ' + 'running.') + return [] + + while True: + try: + p = Popen(('dpkg-vendor', + '--vendor', _system_distribution_chain[-1], + '--query', 'Parent'), + stdout=PIPE) + parent = p.communicate()[0].strip() + # Don't check return code, because if a vendor has no + # parent, dpkg-vendor returns 1 + if not parent: + break + _system_distribution_chain.append(parent) + except Exception: + print ('Error: Could not determine the parent of the ' + 'distribution %s' % _system_distribution_chain[-1]) + return [] + + return _system_distribution_chain + def system_distribution(): """ system_distro() -> string @@ -38,24 +78,7 @@ def system_distribution(): name of the distribution can't be determined, print an error message and return None. """ - global _system_distribution - if _system_distribution is None: - try: - if os.path.isfile('/usr/bin/dpkg-vendor'): - process = Popen(('dpkg-vendor', '--query', 'vendor'), - stdout=PIPE) - else: - process = Popen(('lsb_release', '-cs'), stdout=PIPE) - output = process.communicate()[0] - except OSError: - print ('Error: Could not determine what distribution you are ' - 'running.') - return None - if process.returncode != 0: - print 'Error determininng system distribution' - return None - _system_distribution = output.strip() - return _system_distribution + return system_distribution_chain()[0] def host_architecture(): """ host_architecture -> string