swc-installation-test-2.py: Prefer distro to platform.linux_distribution master
authorW. Trevor King <wking@tremily.us>
Sun, 10 Jul 2016 22:36:30 +0000 (15:36 -0700)
committerW. Trevor King <wking@tremily.us>
Sun, 10 Jul 2016 22:36:30 +0000 (15:36 -0700)
platform.linux_distribution was deprecated in 2015 to be removed in
Python 3.7 [1,2].  And it may not exist at all on some Python builds
(Manjaro's [3]?).  Prefer distro (the replacement PyPI package [4,5])
on Linux if the user already has it installed.

If we can't find a distro/version, use '*' for generic help-URL
matching.  And silently skip distro/version information in
print_system_info.

[1]: http://bugs.python.org/issue1322#msg243063
[2]: https://docs.python.org/3/library/platform.html#platform.linux_distribution
[3]: https://github.com/prerit2010/swc-setup-installation-test/issues/7#issuecomment-231573710
[4]: http://bugs.python.org/issue1322#msg263896
[5]: https://pypi.python.org/pypi/distro

swc-installation-test-2.py

index bd7960d58c96444e915c02d7582a20040d6a1323..254d4ece92f2b702a622595bc09ddd7e5764ddf6 100755 (executable)
@@ -71,6 +71,11 @@ except ImportError:  # Python 2.x
     import urllib as _urllib_parse  # for quote()
 import xml.etree.ElementTree as _element_tree
 
+try:
+    import distro as _distro
+except ImportError:
+    _distro = None
+
 
 if not hasattr(_shlex, 'quote'):  # Python versions older than 3.3
     # Use the undocumented pipes.quote()
@@ -215,15 +220,20 @@ class DependencyError (Exception):
     def get_url(self):
         system = _platform.system()
         version = None
-        for pversion in (
-            'linux_distribution',
-            'mac_ver',
-            'win32_ver',
-            ):
-            value = getattr(_platform, pversion)()
-            if value[0]:
-                version = value[0]
+        if system == 'Linux' and _distro:
+            version = _distro.id()
+        else:
+            for pversion in (
+                    'linux_distribution',
+                    'mac_ver',
+                    'win32_ver',
+                    ):
+                value = getattr(_platform, pversion)()
+                if value[0]:
+                    version = value[0]
                 break
+        if not version:
+            version = '*'
         package = self.checker.name
         for (s,v,p),url in self._setup_urls.items():
             if (_fnmatch.fnmatch(system, s) and
@@ -980,14 +990,20 @@ def print_system_info():
     _print_info('os.uname', _platform.uname())
     _print_info('platform', _sys.platform)
     _print_info('platform+', _platform.platform())
-    for pversion in (
+    system = _platform.system()
+    if system == 'Linux' and _distro:
+        _print_info(
             'linux_distribution',
-            'mac_ver',
-            'win32_ver',
-            ):
-        value = getattr(_platform, pversion)()
-        if value[0]:
-            _print_info(pversion, value)
+            _distro.name(pretty=True) or _distro.linux_distribution())
+    else:
+        for pversion in (
+                'linux_distribution',
+                'mac_ver',
+                'win32_ver',
+                ):
+            value = getattr(_platform, pversion)()
+            if value[0]:
+                _print_info(pversion, value)
     _print_info('prefix', _sys.prefix)
     _print_info('exec_prefix', _sys.exec_prefix)
     _print_info('executable', _sys.executable)