Skip to content

Commit 7ce285c

Browse files
Fix Armbian release info (#366)
Co-authored-by: Samuel FORESTIER <samuel+dev@forestier.app>
1 parent 8d88a2d commit 7ce285c

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

src/distro/distro.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class InfoDict(TypedDict):
127127
"SuSE-release",
128128
"altlinux-release",
129129
"arch-release",
130+
"armbian-release",
130131
"base-release",
131132
"centos-release",
132133
"fedora-release",
@@ -905,6 +906,9 @@ def version(self, pretty: bool = False, best: bool = False) -> str:
905906
elif self.id() == "debian" or "debian" in self.like().split():
906907
# On Debian-like, add debian_version file content to candidates list.
907908
versions.append(self._debian_version)
909+
if self._distro_release_info.get("id") == "armbian":
910+
# On Armbian, add version from armbian-release file to candidates list.
911+
versions.append(self._armbian_version)
908912
version = ""
909913
if best:
910914
# This algorithm uses the last version in priority order that has
@@ -1225,6 +1229,16 @@ def _debian_version(self) -> str:
12251229
except FileNotFoundError:
12261230
return ""
12271231

1232+
@cached_property
1233+
def _armbian_version(self) -> str:
1234+
try:
1235+
with open(
1236+
os.path.join(self.etc_dir, "armbian-release"), encoding="ascii"
1237+
) as fp:
1238+
return self._parse_os_release_content(fp).get("version", "")
1239+
except FileNotFoundError:
1240+
return ""
1241+
12281242
@staticmethod
12291243
def _parse_uname_content(lines: Sequence[str]) -> Dict[str, str]:
12301244
if not lines:
@@ -1302,6 +1316,14 @@ def _distro_release_info(self) -> Dict[str, str]:
13021316
if match is not None:
13031317
distro_info["id"] = match.group(1)
13041318

1319+
# Armbian release files are not standard as they start with a
1320+
# comment. Manually set name if it has not been inferred.
1321+
if distro_info["id"] == "armbian" and distro_info.get(
1322+
"name", ""
1323+
).startswith("#"):
1324+
distro_info["name"] = "Armbian"
1325+
distro_info["version_id"] = self._armbian_version
1326+
13051327
# CloudLinux < 7: manually enrich info with proper id.
13061328
if "cloudlinux" in distro_info.get("name", "").lower():
13071329
distro_info["id"] = "cloudlinux"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# PLEASE DO NOT EDIT THIS FILE
2+
BOARD=nanopim4v2
3+
BOARD_NAME="NanoPi M4V2"
4+
BOARDFAMILY=rk3399
5+
BUILD_REPOSITORY_URL=https://github.com/armbian/build
6+
BUILD_REPOSITORY_COMMIT=1a8daf0
7+
VERSION=23.02.2
8+
LINUXFAMILY=rockchip64
9+
ARCH=arm64
10+
IMAGE_TYPE=nightly
11+
BOARD_TYPE=conf
12+
INITRD_ARCH=arm64
13+
KERNEL_IMAGE_TYPE=Image
14+
BRANCH=current
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../usr/lib/os-release
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
2+
NAME="Debian GNU/Linux"
3+
VERSION_ID="10"
4+
VERSION="10 (buster)"
5+
VERSION_CODENAME=buster
6+
ID=debian
7+
HOME_URL="https://www.debian.org/"
8+
SUPPORT_URL="https://www.debian.org/support"
9+
BUG_REPORT_URL="https://bugs.debian.org/"

tests/test_distro.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,28 @@ def test_altlinux10_release(self) -> None:
19191919
}
19201920
self._test_release_file_info("altlinux-release", desired_info)
19211921

1922+
def test_armbian_release(self) -> None:
1923+
desired_outcome = {
1924+
"id": "debian",
1925+
"codename": "buster",
1926+
"name": "Debian GNU/Linux",
1927+
"pretty_name": "Debian GNU/Linux 10 (buster)",
1928+
"like": "",
1929+
"version": "10",
1930+
"pretty_version": "10 (buster)",
1931+
"best_version": "23.02.2",
1932+
"major_version": "10",
1933+
"minor_version": "",
1934+
}
1935+
self._test_outcome(desired_outcome)
1936+
1937+
desired_info = {
1938+
"id": "armbian",
1939+
"name": "Armbian",
1940+
"version_id": "23.02.2",
1941+
}
1942+
self._test_release_file_info("armbian-release", desired_info)
1943+
19221944

19231945
def _bad_os_listdir(path: str = ".") -> NoReturn:
19241946
"""This function is used by TestOverallWithEtcNotReadable to simulate
@@ -2362,6 +2384,12 @@ def test_repr(self) -> None:
23622384
repr_str = repr(distro._distro)
23632385
assert "LinuxDistribution" in repr_str
23642386
for attr in MODULE_DISTRO.__dict__.keys():
2365-
if attr in ("root_dir", "etc_dir", "usr_lib_dir", "_debian_version"):
2387+
if attr in (
2388+
"root_dir",
2389+
"etc_dir",
2390+
"usr_lib_dir",
2391+
"_debian_version",
2392+
"_armbian_version",
2393+
):
23662394
continue
23672395
assert f"{attr}=" in repr_str

0 commit comments

Comments
 (0)