From 3564016ae13acb372b841a55f936dadf05789fdf Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Thu, 18 May 2023 20:22:47 -0400 Subject: [PATCH] util: replace os_release.py with ditsro package Signed-off-by: Alex Lowe --- craft_archives/os_release.py | 95 --------------------- craft_archives/repo/apt_sources_manager.py | 8 +- pyproject.toml | 1 + tests/integration/repo/test_installer.py | 5 +- tests/unit/repo/test_apt_sources_manager.py | 11 +-- 5 files changed, 15 insertions(+), 105 deletions(-) delete mode 100644 craft_archives/os_release.py diff --git a/craft_archives/os_release.py b/craft_archives/os_release.py deleted file mode 100644 index 4db4703..0000000 --- a/craft_archives/os_release.py +++ /dev/null @@ -1,95 +0,0 @@ -# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- -# -# Copyright 2017-2023 Canonical Ltd. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3 as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -"""OS release information helpers.""" - -import contextlib -from pathlib import Path -from typing import Dict - -from craft_archives import errors - -_ID_TO_UBUNTU_CODENAME = { - "17.10": "artful", - "17.04": "zesty", - "16.04": "xenial", - "14.04": "trusty", -} - - -class OsRelease: - """A class to intelligently determine the OS on which we're running.""" - - def __init__( - self, *, os_release_file: Path = Path("/etc/os-release") # noqa: B008 - ) -> None: - """Create a new OsRelease instance. - - :param str os_release_file: Path to os-release file to be parsed. - """ - with contextlib.suppress(FileNotFoundError): - self._os_release: Dict[str, str] = {} - with os_release_file.open(encoding="utf-8") as release_file: - for line in release_file: - entry = line.rstrip().split("=") - if len(entry) == 2: # noqa: PLR2004 - self._os_release[entry[0]] = entry[1].strip('"') - - def id(self) -> str: - """Return the OS ID. - - :raises SnapcraftError: If no ID can be determined. - """ - with contextlib.suppress(KeyError): - return self._os_release["ID"] - - raise errors.ArchivesError("Unable to determine host OS ID") - - def name(self) -> str: - """Return the OS name. - - :raises SnapcraftError: If no name can be determined. - """ - with contextlib.suppress(KeyError): - return self._os_release["NAME"] - - raise errors.ArchivesError("Unable to determine host OS name") - - def version_id(self) -> str: - """Return the OS version ID. - - :raises SnapcraftError: If no version ID can be determined. - """ - with contextlib.suppress(KeyError): - return self._os_release["VERSION_ID"] - - raise errors.ArchivesError("Unable to determine host OS version ID") - - def version_codename(self) -> str: - """Return the OS version codename. - - This first tries to use the VERSION_CODENAME. If that's missing, it - tries to use the VERSION_ID to figure out the codename on its own. - - :raises SnapcraftError: If no version codename can be determined. - """ - with contextlib.suppress(KeyError): - return self._os_release["VERSION_CODENAME"] - - with contextlib.suppress(KeyError): - return _ID_TO_UBUNTU_CODENAME[self._os_release["VERSION_ID"]] - - raise errors.ArchivesError("Unable to determine host OS version codename") diff --git a/craft_archives/repo/apt_sources_manager.py b/craft_archives/repo/apt_sources_manager.py index 4a70bf9..4f45687 100644 --- a/craft_archives/repo/apt_sources_manager.py +++ b/craft_archives/repo/apt_sources_manager.py @@ -23,7 +23,9 @@ from pathlib import Path from typing import List, Optional, cast -from craft_archives import os_release, utils +import distro + +from craft_archives import utils from . import apt_key_manager, apt_ppa, apt_uca, errors, package_repository @@ -194,7 +196,7 @@ def _install_sources_ppa( :returns: True if source configuration was changed. """ owner, name = apt_ppa.split_ppa_parts(ppa=package_repo.ppa) - codename = os_release.OsRelease().version_codename() + codename = distro.codename() key_id = apt_ppa.get_launchpad_ppa_key_id(ppa=package_repo.ppa) keyring_path = apt_key_manager.get_keyring_path( @@ -226,7 +228,7 @@ def _install_sources_uca( cloud = package_repo.cloud pocket = package_repo.pocket - codename = os_release.OsRelease().version_codename() + codename = distro.codename() apt_uca.check_release_compatibility(codename, cloud, pocket) key_id = package_repository.UCA_KEY_ID diff --git a/pyproject.toml b/pyproject.toml index 5999f01..c627d3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,7 @@ name = "craft-archives" version = "0.0.4" readme = "README.rst" dependencies = [ + "distro>=1.3.0", "gnupg", "launchpadlib", "lazr.restfulclient", diff --git a/tests/integration/repo/test_installer.py b/tests/integration/repo/test_installer.py index 1db2408..27678b1 100644 --- a/tests/integration/repo/test_installer.py +++ b/tests/integration/repo/test_installer.py @@ -21,8 +21,9 @@ from textwrap import dedent from typing import Any, Dict, List +import distro import pytest -from craft_archives import os_release, repo +from craft_archives import repo APT_SOURCES = dedent( """ @@ -35,7 +36,7 @@ """ ).lstrip() -VERSION_CODENAME = os_release.OsRelease().version_codename() +VERSION_CODENAME = distro.codename() PPA_SOURCES = dedent( """ diff --git a/tests/unit/repo/test_apt_sources_manager.py b/tests/unit/repo/test_apt_sources_manager.py index 0c300aa..6ac7cdd 100644 --- a/tests/unit/repo/test_apt_sources_manager.py +++ b/tests/unit/repo/test_apt_sources_manager.py @@ -18,8 +18,10 @@ import http import urllib.error from textwrap import dedent +from unittest import mock from unittest.mock import call, patch +import distro import pytest from craft_archives.repo import apt_ppa, apt_sources_manager, errors from craft_archives.repo.package_repository import ( @@ -59,11 +61,10 @@ def mock_run(mocker): @pytest.fixture(autouse=True) -def mock_version_codename(mocker): - yield mocker.patch( - "craft_archives.os_release.OsRelease.version_codename", - return_value="FAKE-CODENAME", - ) +def mock_version_codename(monkeypatch): + mock_codename = mock.Mock(return_value="FAKE-CODENAME") + monkeypatch.setattr(distro, "codename", mock_codename) + yield mock_codename @pytest.fixture