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 --- tests/test_distro.py | 12 ++++++++++-- whatmaps/command.py | 43 ++--------------------------------------- whatmaps/debiandistro.py | 4 ++-- whatmaps/distro.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 45 deletions(-) diff --git a/tests/test_distro.py b/tests/test_distro.py index e22da68..c67b0f6 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -16,9 +16,9 @@ import unittest -from whatmaps.distro import Distro +from mock import patch -from . import context +from whatmaps.distro import Distro, detect class Pkg(object): name = 'doesnotmatter' @@ -38,3 +38,11 @@ class TestDistro(unittest.TestCase): self.assertEqual(Distro.pkg_services(Pkg), []) self.assertEqual(Distro.pkg_service_blacklist(Pkg), []) self.assertFalse(Distro.has_apt()) + + def test_detect_via_lsb_release_module(self): + "Detect distro via lsb_release" + with patch('lsb_release.get_distro_information', return_value={'ID': 'Debian'}): + # Make sure we don't use the fallback + with patch('os.path.exists', return_value=False): + d = detect() + self.assertEqual(d.id, 'Debian') diff --git a/whatmaps/command.py b/whatmaps/command.py index 29996b1..b942a9a 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -22,20 +22,13 @@ import errno import glob import os import logging -import subprocess import sys from optparse import OptionParser -try: - import lsb_release -except ImportError: - lsb_release = None from . process import Process -from . debiandistro import DebianDistro -from . redhatdistro import FedoraDistro +from . distro import Distro from . pkg import PkgError - def check_maps(procs, shared_objects): restart_procs = {} for proc in procs: @@ -60,38 +53,6 @@ def get_all_pids(): return processes -def detect_distro(): - 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.id: - return DebianDistro - elif id == FedoraDistro.id: - return FedoraDistro - else: - if os.path.exists('/usr/bin/dpkg'): - logging.warning("Unknown distro but dpkg found, assuming Debian") - return DebianDistro - elif os.path.exists('/bin/rpm'): - logging.warning("Unknown distro but rpm found, assuming Fedora") - return FedoraDistro - else: - return None - - def write_cmd_file(services, cmd_file, distro): "Write out commands needed to restart the services to a file" out = open(cmd_file, 'w') @@ -130,7 +91,7 @@ def main(argv): logging.basicConfig(level=level, format='%(levelname)s: %(message)s') - distro = detect_distro() + distro = Distro.detect() if not distro: logging.error("Unsupported Distribution") return 1 diff --git a/whatmaps/debiandistro.py b/whatmaps/debiandistro.py index af95bc2..d608d8f 100644 --- a/whatmaps/debiandistro.py +++ b/whatmaps/debiandistro.py @@ -31,11 +31,11 @@ import subprocess import sys import string -from . distro import Distro +import distro from . debianpkg import DebianPkg from . pkg import PkgError -class DebianDistro(Distro): +class DebianDistro(distro.Distro): "Debian (dpkg) based distribution" id = 'Debian' 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