diff options
Diffstat (limited to 'whatmaps/distro.py')
-rw-r--r-- | whatmaps/distro.py | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/whatmaps/distro.py b/whatmaps/distro.py index e4f417f..f0f08eb 100644 --- a/whatmaps/distro.py +++ b/whatmaps/distro.py @@ -15,12 +15,28 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import logging +import os import subprocess + +try: + import lsb_release +except ImportError: + lsb_release = None + class Distro(object): """ A distribution + @cvar id: distro id as returned by lsb-release + + @cvar service_blacklist: services that should never be restarted + @cvar _pkg_services: A C{dict} that maps packages to services. In + case we find binaries that match a key in this hash restart + the services listed in values. + @cvar _pkg_service_blacklist: if we find binaries in the package + listed as key don't restart services listed in values """ id = None service_blacklist = set() @@ -49,12 +65,18 @@ class Distro(object): subprocess.call(klass.restart_service_cmd(service)) @classmethod + def is_service_installed(klass, service): + """Check wether a service exists on the system""" + return True + + @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, []) + return [ s for s in klass._pkg_services.get(pkg.name, []) + if klass.is_service_installed(s) ] @classmethod def pkg_service_blacklist(klass, pkg): @@ -68,3 +90,45 @@ class Distro(object): def has_apt(klass): """Does the distribution use apt""" return False + + @staticmethod + def detect(): + return detect() + +import whatmaps.debiandistro +import whatmaps.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 == whatmaps.debiandistro.DebianDistro.id: + return whatmaps.debiandistro.DebianDistro + elif id == whatmaps.redhatdistro.FedoraDistro.id: + return whatmaps.redhatdistro.FedoraDistro + else: + if os.path.exists('/usr/bin/dpkg'): + logging.warning("Unknown distro but dpkg found, assuming Debian") + return whatmaps.debiandistro.DebianDistro + elif os.path.exists('/bin/rpm'): + logging.warning("Unknown distro but rpm found, assuming Fedora") + return whatmaps.debiandistro.FedoraDistro + else: + return None |