Skip to content

Commit 5d22880

Browse files
Drastically lowers LinuxDistribution._distro_release_info complexity (#327)
1 parent 697b2db commit 5d22880

1 file changed

Lines changed: 47 additions & 37 deletions

File tree

src/distro/distro.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ class InfoDict(TypedDict):
122122
# Pattern for base file name of distro release file
123123
_DISTRO_RELEASE_BASENAME_PATTERN = re.compile(r"(\w+)[-_](release|version)$")
124124

125+
# Base file names to be looked up for if _UNIXCONFDIR is not readable.
126+
_DISTRO_RELEASE_BASENAMES = [
127+
"SuSE-release",
128+
"arch-release",
129+
"base-release",
130+
"centos-release",
131+
"fedora-release",
132+
"gentoo-release",
133+
"mageia-release",
134+
"mandrake-release",
135+
"mandriva-release",
136+
"mandrivalinux-release",
137+
"manjaro-release",
138+
"oracle-release",
139+
"redhat-release",
140+
"rocky-release",
141+
"sl-release",
142+
"slackware-version",
143+
]
144+
125145
# Base file names to be ignored when searching for distro release file
126146
_DISTRO_RELEASE_IGNORE_BASENAMES = (
127147
"debian_version",
@@ -1228,14 +1248,14 @@ def _distro_release_info(self) -> Dict[str, str]:
12281248
# file), because we want to use what was specified as best as
12291249
# possible.
12301250
match = _DISTRO_RELEASE_BASENAME_PATTERN.match(basename)
1231-
if "name" in distro_info and "cloudlinux" in distro_info["name"].lower():
1232-
distro_info["id"] = "cloudlinux"
1233-
elif match:
1234-
distro_info["id"] = match.group(1)
1235-
return distro_info
12361251
else:
12371252
try:
1238-
basenames = os.listdir(self.etc_dir)
1253+
basenames = [
1254+
basename
1255+
for basename in os.listdir(self.etc_dir)
1256+
if basename not in _DISTRO_RELEASE_IGNORE_BASENAMES
1257+
and os.path.isfile(os.path.join(self.etc_dir, basename))
1258+
]
12391259
# We sort for repeatability in cases where there are multiple
12401260
# distro specific files; e.g. CentOS, Oracle, Enterprise all
12411261
# containing `redhat-release` on top of their own.
@@ -1245,39 +1265,29 @@ def _distro_release_info(self) -> Dict[str, str]:
12451265
# sure about the *-release files. Check common entries of
12461266
# /etc for information. If they turn out to not be there the
12471267
# error is handled in `_parse_distro_release_file()`.
1248-
basenames = [
1249-
"SuSE-release",
1250-
"arch-release",
1251-
"base-release",
1252-
"centos-release",
1253-
"fedora-release",
1254-
"gentoo-release",
1255-
"mageia-release",
1256-
"mandrake-release",
1257-
"mandriva-release",
1258-
"mandrivalinux-release",
1259-
"manjaro-release",
1260-
"oracle-release",
1261-
"redhat-release",
1262-
"rocky-release",
1263-
"sl-release",
1264-
"slackware-version",
1265-
]
1268+
basenames = _DISTRO_RELEASE_BASENAMES
12661269
for basename in basenames:
1267-
if basename in _DISTRO_RELEASE_IGNORE_BASENAMES:
1268-
continue
12691270
match = _DISTRO_RELEASE_BASENAME_PATTERN.match(basename)
1270-
if match:
1271-
filepath = os.path.join(self.etc_dir, basename)
1272-
distro_info = self._parse_distro_release_file(filepath)
1273-
if "name" in distro_info:
1274-
# The name is always present if the pattern matches
1275-
self.distro_release_file = filepath
1276-
distro_info["id"] = match.group(1)
1277-
if "cloudlinux" in distro_info["name"].lower():
1278-
distro_info["id"] = "cloudlinux"
1279-
return distro_info
1280-
return {}
1271+
if match is None:
1272+
continue
1273+
filepath = os.path.join(self.etc_dir, basename)
1274+
distro_info = self._parse_distro_release_file(filepath)
1275+
# The name is always present if the pattern matches.
1276+
if "name" not in distro_info:
1277+
continue
1278+
self.distro_release_file = filepath
1279+
break
1280+
else: # the loop didn't "break": no candidate.
1281+
return {}
1282+
1283+
if match is not None:
1284+
distro_info["id"] = match.group(1)
1285+
1286+
# CloudLinux < 7: manually enrich info with proper id.
1287+
if "cloudlinux" in distro_info.get("name", "").lower():
1288+
distro_info["id"] = "cloudlinux"
1289+
1290+
return distro_info
12811291

12821292
def _parse_distro_release_file(self, filepath: str) -> Dict[str, str]:
12831293
"""

0 commit comments

Comments
 (0)