4949
5050_log = fancylogger .getLogger ('systemtools' , fname = False )
5151
52+
53+ try :
54+ import distro
55+ HAVE_DISTRO = True
56+ except ImportError as err :
57+ _log .debug ("Failed to import 'distro' Python module: %s" , err )
58+ HAVE_DISTRO = False
59+
60+
5261# Architecture constants
5362AARCH32 = 'AArch32'
5463AARCH64 = 'AArch64'
@@ -531,9 +540,21 @@ def get_os_name():
531540 Determine system name, e.g., 'redhat' (generic), 'centos', 'debian', 'fedora', 'suse', 'ubuntu',
532541 'red hat enterprise linux server', 'SL' (Scientific Linux), 'opensuse', ...
533542 """
534- # platform.linux_distribution is more useful, but only available since Python 2.6
535- # this allows to differentiate between Fedora, CentOS, RHEL and Scientific Linux (Rocks is just CentOS)
536- os_name = platform .linux_distribution ()[0 ].strip ().lower ()
543+ os_name = None
544+
545+ # platform.linux_distribution was removed in Python 3.8,
546+ # see https://docs.python.org/2/library/platform.html#platform.linux_distribution
547+ if hasattr (platform , 'linux_distribution' ):
548+ # platform.linux_distribution is more useful, but only available since Python 2.6
549+ # this allows to differentiate between Fedora, CentOS, RHEL and Scientific Linux (Rocks is just CentOS)
550+ os_name = platform .linux_distribution ()[0 ].strip ().lower ()
551+ elif HAVE_DISTRO :
552+ # distro package is the recommended alternative to platform.linux_distribution,
553+ # see https://pypi.org/project/distro
554+ os_name = distro .name ()
555+ else :
556+ # no easy way to determine name of Linux distribution
557+ os_name = None
537558
538559 os_name_map = {
539560 'red hat enterprise linux server' : 'RHEL' ,
@@ -550,7 +571,15 @@ def get_os_name():
550571
551572def get_os_version ():
552573 """Determine system version."""
553- os_version = platform .dist ()[1 ]
574+
575+ # platform.dist was removed in Python 3.8
576+ if hasattr (platform , 'dist' ):
577+ os_version = platform .dist ()[1 ]
578+ elif HAVE_DISTRO :
579+ os_version = distro .version ()
580+ else :
581+ os_version = None
582+
554583 if os_version :
555584 if get_os_name () in ["suse" , "SLES" ]:
556585
0 commit comments