116116
117117UNKNOWN = 'UNKNOWN'
118118
119+ ETC_OS_RELEASE = '/etc/os-release'
119120MAX_FREQ_FP = '/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq'
120121PROC_CPUINFO_FP = '/proc/cpuinfo'
121122PROC_MEMINFO_FP = '/proc/meminfo'
@@ -681,11 +682,21 @@ def get_os_name():
681682 if hasattr (platform , 'linux_distribution' ):
682683 # platform.linux_distribution is more useful, but only available since Python 2.6
683684 # this allows to differentiate between Fedora, CentOS, RHEL and Scientific Linux (Rocks is just CentOS)
684- os_name = platform .linux_distribution ()[0 ].strip ().lower ()
685- elif HAVE_DISTRO :
685+ os_name = platform .linux_distribution ()[0 ].strip ()
686+
687+ # take into account that on some OSs, platform.distribution returns an empty string as OS name,
688+ # for example on OpenSUSE Leap 15.2
689+ if not os_name and HAVE_DISTRO :
686690 # distro package is the recommended alternative to platform.linux_distribution,
687691 # see https://pypi.org/project/distro
688692 os_name = distro .name ()
693+
694+ if not os_name and os .path .exists (ETC_OS_RELEASE ):
695+ os_release_txt = read_file (ETC_OS_RELEASE )
696+ name_regex = re .compile ('^NAME="?(?P<name>[^"\n ]+)"?$' , re .M )
697+ res = name_regex .search (os_release_txt )
698+ if res :
699+ os_name = res .group ('name' )
689700 else :
690701 # no easy way to determine name of Linux distribution
691702 os_name = None
@@ -699,19 +710,38 @@ def get_os_name():
699710 }
700711
701712 if os_name :
702- return os_name_map .get (os_name , os_name )
713+ return os_name_map .get (os_name . lower () , os_name )
703714 else :
704715 return UNKNOWN
705716
706717
707718def get_os_version ():
708719 """Determine system version."""
709720
721+ os_version = None
722+
710723 # platform.dist was removed in Python 3.8
711724 if hasattr (platform , 'dist' ):
712725 os_version = platform .dist ()[1 ]
713- elif HAVE_DISTRO :
726+
727+ # take into account that on some OSs, platform.dist returns an empty string as OS version,
728+ # for example on OpenSUSE Leap 15.2
729+ if not os_version and HAVE_DISTRO :
714730 os_version = distro .version ()
731+
732+ if not os_version and os .path .exists (ETC_OS_RELEASE ):
733+ os_release_txt = read_file (ETC_OS_RELEASE )
734+ version_regex = re .compile ('^VERSION="?(?P<version>[^"\n ]+)"?$' , re .M )
735+ res = version_regex .search (os_release_txt )
736+ if res :
737+ os_version = res .group ('version' )
738+ else :
739+ # VERSION may not always be defined (for example on Gentoo),
740+ # fall back to VERSION_ID in that case
741+ version_regex = re .compile ('^VERSION_ID="?(?P<version>[^"\n ]+)"?$' , re .M )
742+ res = version_regex .search (os_release_txt )
743+ if res :
744+ os_version = res .group ('version' )
715745 else :
716746 os_version = None
717747
0 commit comments