Skip to content

Commit bb35d41

Browse files
Cache /etc/debian_version content (#351)
1 parent 2808f6b commit bb35d41

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

distro.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,7 @@ def version(self, pretty=False, best=False):
868868
]
869869
if self.id() == "debian" or "debian" in self.like().split():
870870
# On Debian-like, add debian_version file content to candidates list.
871-
try:
872-
with open(os.path.join(self.etc_dir, "debian_version")) as fp:
873-
versions.append(fp.readline().rstrip())
874-
except IOError:
875-
pass
871+
versions.append(self._debian_version)
876872
version = ""
877873
if best:
878874
# This algorithm uses the last version in priority order that has
@@ -1199,6 +1195,15 @@ def _uname_info(self):
11991195
content = self._to_str(stdout).splitlines()
12001196
return self._parse_uname_content(content)
12011197

1198+
@cached_property
1199+
def _debian_version(self):
1200+
# type: () -> str
1201+
try:
1202+
with open(os.path.join(self.etc_dir, "debian_version")) as fp:
1203+
return fp.readline().rstrip()
1204+
except (OSError, IOError):
1205+
return ""
1206+
12021207
@staticmethod
12031208
def _parse_uname_content(lines):
12041209
# type: (Sequence[str]) -> Dict[str, str]

tests/test_distro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,7 @@ def test_repr(self):
22582258
repr_str = repr(distro._distro)
22592259
assert "LinuxDistribution" in repr_str
22602260
for attr in MODULE_DISTRO.__dict__.keys():
2261-
if attr in ("root_dir", "etc_dir", "usr_lib_dir"):
2261+
if attr in ("root_dir", "etc_dir", "usr_lib_dir", "_debian_version"):
22622262
continue
22632263
assert attr + "=" in repr_str
22642264

0 commit comments

Comments
 (0)