aboutsummaryrefslogtreecommitdiff
path: root/whatmaps/distro.py
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-04-21 21:18:52 +0200
committerGuido Günther <agx@sigxcpu.org>2014-07-01 10:02:39 +0200
commitfe1c3af968020b906a8ca417fee1dd80673d9b9f (patch)
treecd86ffd5d7eee75b9e1aae8d32b9fba168c1da2d /whatmaps/distro.py
parentc56870bfd90ed2d6730c7d74dee01c2ed4a85d34 (diff)
Move detect_distro to Distro
Diffstat (limited to 'whatmaps/distro.py')
-rw-r--r--whatmaps/distro.py50
1 files changed, 50 insertions, 0 deletions
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 <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
@@ -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