[jhbuild/desrt/master: 6/26] utils: add 'sysid' submodule



commit f9ef5422279003b6ae6e1d9165027c3d8630d8b5
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Dec 22 17:50:37 2014 -0500

    utils: add 'sysid' submodule
    
    Expand and update the distro-checking functionality used to print the name of
    the distribution in 'jhbuild tinderbox' logs and move it to a new module.
    
    We will be using this for some other new things soon.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742267

 jhbuild/frontends/tinderbox.py |   40 +------------
 jhbuild/utils/Makefile.am      |    1 +
 jhbuild/utils/sysid.py         |  125 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+), 38 deletions(-)
---
diff --git a/jhbuild/frontends/tinderbox.py b/jhbuild/frontends/tinderbox.py
index c99eedc..6e5ae6f 100644
--- a/jhbuild/frontends/tinderbox.py
+++ b/jhbuild/frontends/tinderbox.py
@@ -27,6 +27,7 @@ import sys
 
 from jhbuild.main import _encoding
 from jhbuild.utils import cmds
+from jhbuild.utils import sysid
 from jhbuild.errors import CommandError, FatalError
 import buildscript
 import commands
@@ -133,39 +134,6 @@ buildlog_footer = '''
 </html>
 '''
 
-def get_distro():
-    # try using the lsb_release tool to get the distro info
-    try:
-        distro = cmds.get_output(['lsb_release', '--short', '--id']) \
-                     .decode(_encoding).strip()
-        release = cmds.get_output(['lsb_release', '--short', '--release']) \
-                      .decode(_encoding).strip()
-        codename = cmds.get_output(['lsb_release', '--short', '--codename']) \
-                       .decode(_encoding).strip()
-        if codename:
-            return '%s %s (%s)' % (distro, release, codename)
-        else:
-            return '%s %s' % (distro, release)
-    except (CommandError, IOError):
-        pass
-
-    # otherwise, look for a /etc/*-release file
-    release_files = ['/etc/redhat-release', '/etc/debian_version' ]
-    release_files.extend([ os.path.join('/etc', fname)
-                           for fname in os.listdir('/etc')
-                             if fname.endswith('release') \
-                                 and fname != 'lsb-release' ])
-    for filename in release_files:
-        if os.path.exists(filename):
-            return open(filename, 'r').readline().strip()
-
-    osx = commands.getoutput('sw_vers -productVersion')
-    if osx:
-        return 'Mac OS X ' + osx;
-
-    # else:
-    return None
-
 def escape(string):
     if type(string) is not unicode:
         string = unicode(string, _encoding, 'replace')
@@ -309,11 +277,7 @@ class TinderboxBuildScript(buildscript.BuildScript):
 
         info.append(('Build Host', socket.gethostname()))
         info.append(('Architecture', '%s %s (%s)' % (un[0], un[2], un[4])))
-
-        distro = get_distro()
-        if distro:
-            info.append(('Distribution', distro))
-
+        info.append(('System', sysid.get_pretty_name()))
         info.append(('Module Set', self.config.moduleset))
         info.append(('Start Time', self.timestamp()))
 
diff --git a/jhbuild/utils/Makefile.am b/jhbuild/utils/Makefile.am
index 81f2513..3234930 100644
--- a/jhbuild/utils/Makefile.am
+++ b/jhbuild/utils/Makefile.am
@@ -9,6 +9,7 @@ app_PYTHON = \
        notify.py \
        packagedb.py \
        sxml.py \
+       sysid.py \
        systeminstall.py \
        trigger.py \
        trayicon.py \
diff --git a/jhbuild/utils/sysid.py b/jhbuild/utils/sysid.py
new file mode 100644
index 0000000..2cf784f
--- /dev/null
+++ b/jhbuild/utils/sysid.py
@@ -0,0 +1,125 @@
+# jhbuild - a tool to ease building collections of source packages
+# Copyright (C) 2014 Canonical Limited
+#
+#  sysid.py: identify the system that we are running on
+#
+# 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 licence, 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
+
+import sys
+import subprocess
+import ast
+
+sys_id = None
+sys_name = None
+
+def read_os_release():
+    global sys_name
+    global sys_id
+
+    release_file = None
+
+    try:
+        release_file = open('/etc/os-release')
+    except:
+        try:
+            release_file = open('/usr/lib/os-release')
+        except:
+            return False
+
+    fields = {}
+    for line in release_file:
+        line = line.strip()
+
+        if not line or line.startswith('#'):
+            continue
+
+        if '=' not in line:
+            continue
+
+        field, _, value = line.partition('=')
+
+        if value.startswith("'") or value.startswith('"'):
+            try:
+                value = ast.literal_eval(value)
+            except:
+                continue
+
+        fields[field] = value
+
+    if 'ID' not in fields or 'VERSION_ID' not in fields:
+        return False
+
+    sys_id = fields['ID'] + '-' + fields['VERSION_ID']
+
+    if 'NAME' in fields and 'VERSION' in fields:
+        sys_name = fields['NAME'] + ' ' + fields['VERSION']
+    else:
+        # fall back
+        sys_name = fields['ID'] + ' ' + fields['VERSION_ID']
+
+    return True
+
+def get_macos_info():
+    global sys_name
+    global sys_id
+
+    try:
+        ver = subprocess.check_output('sw_vers -productVersion')
+
+        sys_name = 'Mac OS X ' + ver
+        sys_id = 'macos-' + ver
+
+        return True
+
+    except:
+        return False
+
+def ensure_loaded():
+    global sys_name
+    global sys_id
+
+    if sys_id is not None:
+        return
+
+    # our first choice is to use os-release info
+    if read_os_release():
+        return
+
+    # but failing that, fall back to using sys.platform
+    sys_id = sys.platform
+
+    if sys_id.startswith('linux'):
+        sys_name = "Unknown Linux Distribution (no 'os-release' file)"
+
+    elif sys_id.startswith('freebsd'):
+        sys_name = 'FreeBSD (%s)' % (sys_id)
+
+    elif sys_id.startswith('macos'):
+        if not get_macos_info():
+            sys_name = 'Mac OS X (unknown version)'
+
+    else:
+        sys_id = sys.platform
+        sys_name = sys.platform
+
+def get_id():
+    ensure_loaded()
+
+    return sys_id
+
+def get_pretty_name():
+    ensure_loaded()
+
+    return sys_name


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]