From 39ad89568ebd75afece8327d8bc5a40924e38015 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 18 Apr 2014 16:31:22 +0200 Subject: Split out and test Distro --- tests/test_distro.py | 41 ++++++++++++++++++++++++++++++ whatmaps/command.py | 62 +++------------------------------------------- whatmaps/distro.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 59 deletions(-) create mode 100644 tests/test_distro.py create mode 100644 whatmaps/distro.py diff --git a/tests/test_distro.py b/tests/test_distro.py new file mode 100644 index 0000000..3594f67 --- /dev/null +++ b/tests/test_distro.py @@ -0,0 +1,41 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +"""Test L{whatmaps.process} config""" + +import unittest + +from whatmaps.distro import Distro + +from . import context + +class Pkg(object): + name = 'doesnotmatter' + +class TestDistro(unittest.TestCase): + def test_abstract(self): + """Check abstract method signatures""" + # Variables + self.assertEqual(Distro.service_blacklist, set()) + self.assertIsNone(Distro.id) + # Pure virtual methods + self.assertRaises(Distro.pkg, None, None, NotImplementedError) + self.assertRaises(Distro.pkg_by_file, None, NotImplementedError) + self.assertRaises(Distro.restart_service_cmd, None, NotImplementedError) + self.assertRaises(Distro.restart_service, None, NotImplementedError) + # Lookup methods + self.assertEqual(Distro.pkg_services(Pkg), []) + self.assertEqual(Distro.pkg_service_blacklist(Pkg), []) + self.assertFalse(Distro.has_apt()) diff --git a/whatmaps/command.py b/whatmaps/command.py index 502f7d3..556baf4 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -34,71 +34,15 @@ try: except ImportError: lsb_release = None -from whatmaps.process import Process + +from . process import Process +from . distro import Distro class PkgError(Exception): pass -class Distro(object): - """ - A distribution - @cvar id: distro id as returned by lsb-release - """ - id = None - service_blacklist = set() - _pkg_services = {} - _pkg_blacklist = {} - - @classmethod - def pkg(klass, name): - """Return package object named name""" - raise NotImplementedError - - @classmethod - def pkg_by_file(klass, path): - """Return package object that contains path""" - raise NotImplementedError - - @classmethod - def restart_service_cmd(klass, service): - """Command to restart service""" - raise NotImplementedError - - @classmethod - def restart_service(klass, service): - """Restart a service""" - subprocess.call(klass.restart_service_cmd(service)) - - @classmethod - def pkg_services(klass, pkg): - """ - List of services that package pkg needs restarted that aren't part - of pkg itself - """ - try: - return klass._pkg_services[pkg.name] - except KeyError: - return [] - - @classmethod - def pkg_service_blacklist(klass, pkg): - """ - List of services in pkg that we don't want to be restarted even when - a binary from this package maps a shared lib that changed. - """ - try: - return klass._pkg_service_blacklist[pkg.name] - except KeyError: - return [] - - @classmethod - def has_apt(klass): - """Does the distribution use apt""" - return False - - class Pkg(object): """ A package in a distribution diff --git a/whatmaps/distro.py b/whatmaps/distro.py new file mode 100644 index 0000000..e4f417f --- /dev/null +++ b/whatmaps/distro.py @@ -0,0 +1,70 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2010,2014 Guido Günther +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import subprocess + +class Distro(object): + """ + A distribution + @cvar id: distro id as returned by lsb-release + """ + id = None + service_blacklist = set() + _pkg_services = {} + _pkg_blacklist = {} + _pkg_service_blacklist = {} + + @classmethod + def pkg(klass, name): + """Return package object named name""" + raise NotImplementedError + + @classmethod + def pkg_by_file(klass, path): + """Return package object that contains path""" + raise NotImplementedError + + @classmethod + def restart_service_cmd(klass, service): + """Command to restart service""" + raise NotImplementedError + + @classmethod + def restart_service(klass, service): + """Restart a service""" + subprocess.call(klass.restart_service_cmd(service)) + + @classmethod + def pkg_services(klass, pkg): + """ + List of services that package pkg needs restarted that aren't part + of pkg itself + """ + return klass._pkg_services.get(pkg.name, []) + + @classmethod + def pkg_service_blacklist(klass, pkg): + """ + List of services in pkg that we don't want to be restarted even when + a binary from this package maps a shared lib that changed. + """ + return klass._pkg_service_blacklist.get(pkg.name, []) + + @classmethod + def has_apt(klass): + """Does the distribution use apt""" + return False -- cgit v1.2.3