From fe1c3af968020b906a8ca417fee1dd80673d9b9f Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 21 Apr 2014 21:18:52 +0200 Subject: Move detect_distro to Distro --- whatmaps/distro.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'whatmaps/distro.py') diff --git a/whatmaps/distro.py b/whatmaps/distro.py index 5625955..b2e2c82 100644 --- a/whatmaps/distro.py +++ b/whatmaps/distro.py @@ -15,8 +15,16 @@ # along with this program. If not, see . # +import logging +import os import subprocess + +try: + import lsb_release +except ImportError: + lsb_release = None + class Distro(object): """ A distribution @@ -74,3 +82,45 @@ class Distro(object): def has_apt(klass): """Does the distribution use apt""" return False + + @staticmethod + def detect(): + return detect() + +import debiandistro +import redhatdistro + +def detect(): + """ + Detect the distribution we run on. Returns C{None} if the + distribution is unknown. + """ + id = None + + if lsb_release: + id = lsb_release.get_distro_information()['ID'] + else: + try: + lsb_cmd = subprocess.Popen(['lsb_release', '--id', '-s'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + output = lsb_cmd.communicate()[0] + if not lsb_cmd.returncode: + id = output.strip() + except OSError: + # id is None in this case + pass + + if id == debiandistro.DebianDistro.id: + return debiandistro.DebianDistro + elif id == redhatdistro.FedoraDistro.id: + return redhatdistro.FedoraDistro + else: + if os.path.exists('/usr/bin/dpkg'): + logging.warning("Unknown distro but dpkg found, assuming Debian") + return debiandistro.DebianDistro + elif os.path.exists('/bin/rpm'): + logging.warning("Unknown distro but rpm found, assuming Fedora") + return debiandistro.FedoraDistro + else: + return None -- cgit v1.2.3