From 574f5b2973240efa8e85457dcdf5e17a1a82651f Mon Sep 17 00:00:00 2001 From: Bruno Pimentel Date: Tue, 23 Apr 2024 17:45:52 -0300 Subject: [PATCH] rpms: Rename source_dir variable to package_dir Calling this variable source_dir might mislead the reader into believing it refers to the actual repository root directory. In reality, it refers to the relative path of the Cachi2 input package. This also led to the need of renaming what was previously called as package_dir to deps_dir, since it refers to the path where the downloaded RPMs will be placed in the output dir. Signed-off-by: Bruno Pimentel --- cachi2/core/package_managers/rpm/main.py | 32 ++++++++++++------------ tests/unit/package_managers/test_rpm.py | 24 +++++++++--------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cachi2/core/package_managers/rpm/main.py b/cachi2/core/package_managers/rpm/main.py index ceae610d3..3e8b3c469 100644 --- a/cachi2/core/package_managers/rpm/main.py +++ b/cachi2/core/package_managers/rpm/main.py @@ -24,14 +24,14 @@ DEFAULT_LOCKFILE_NAME = "rpms.lock.yaml" -DEFAULT_PACKAGE_DIR = "deps/rpm" +DEFAULT_DEPS_DIR = "deps/rpm" # during the computing of file checksum read chunk of size 1 MB READ_CHUNK = 1048576 def fetch_rpm_source(request: Request) -> RequestOutput: - """Process all the rpm source directories in a request.""" + """Resolve and fetch all the RPM dependencies for a request.""" components: list[Component] = [] for package in request.rpm_packages: path = request.source_dir.join_within_root(package.path) @@ -44,14 +44,14 @@ def fetch_rpm_source(request: Request) -> RequestOutput: ) -def _resolve_rpm_project(source_dir: RootedPath, output_dir: RootedPath) -> list[Component]: +def _resolve_rpm_project(package_dir: RootedPath, output_dir: RootedPath) -> list[Component]: """ - Process a request for a single RPM source directory. + Resolve the RPMs for a single Cachi2 input package. - Process the input lockfile, fetch packages and generate SBOM. + Process the input lockfile, fetch packages and generate SBOM components. """ # Check the availability of the input lockfile. - if not source_dir.join_within_root(DEFAULT_LOCKFILE_NAME).path.exists(): + if not package_dir.join_within_root(DEFAULT_LOCKFILE_NAME).path.exists(): raise PackageRejected( f"RPM lockfile '{DEFAULT_LOCKFILE_NAME}' missing, refusing to continue.", solution=( @@ -60,7 +60,7 @@ def _resolve_rpm_project(source_dir: RootedPath, output_dir: RootedPath) -> list ), ) - lockfile_name = source_dir.join_within_root(DEFAULT_LOCKFILE_NAME) + lockfile_name = package_dir.join_within_root(DEFAULT_LOCKFILE_NAME) log.info(f"Reading RPM lockfile: {lockfile_name}") with open(lockfile_name) as f: try: @@ -85,11 +85,11 @@ def _resolve_rpm_project(source_dir: RootedPath, output_dir: RootedPath) -> list ), ) - package_dir = output_dir.join_within_root(DEFAULT_PACKAGE_DIR) - metadata = _download(redhat_rpms_lock, package_dir.path) + deps_dir = output_dir.join_within_root(DEFAULT_DEPS_DIR) + metadata = _download(redhat_rpms_lock, deps_dir.path) _verify_downloaded(metadata) - lockfile_relative_path = (source_dir.subpath_from_root / DEFAULT_LOCKFILE_NAME).name + lockfile_relative_path = (package_dir.subpath_from_root / DEFAULT_LOCKFILE_NAME).name return _generate_sbom_components(metadata, lockfile_relative_path) @@ -233,7 +233,7 @@ def inject_files_post(*args: Any, **kwargs: Any) -> None: from_output_dir = kwargs["from_output_dir"] for_output_dir = kwargs["for_output_dir"] - if Path.exists(from_output_dir.joinpath(DEFAULT_PACKAGE_DIR)): + if Path.exists(from_output_dir.joinpath(DEFAULT_DEPS_DIR)): _generate_repos(from_output_dir) _generate_repofiles(from_output_dir, for_output_dir) @@ -241,8 +241,8 @@ def inject_files_post(*args: Any, **kwargs: Any) -> None: def _generate_repos(from_output_dir: Path) -> None: """Search structure for all repoid dirs and create repository metadata \ out of its RPMs (and SRPMs).""" - package_dir = from_output_dir.joinpath(DEFAULT_PACKAGE_DIR) - for arch in package_dir.iterdir(): + deps_dir = from_output_dir.joinpath(DEFAULT_DEPS_DIR) + for arch in deps_dir.iterdir(): if not arch.is_dir(): continue for entry in arch.iterdir(): @@ -271,8 +271,8 @@ def _generate_repofiles(from_output_dir: Path, for_output_dir: Path) -> None: Repofiles are not directly created from the templates in this method - templates are stored in the project file. """ - package_dir = from_output_dir.joinpath(DEFAULT_PACKAGE_DIR) - for arch in package_dir.iterdir(): + deps_dir = from_output_dir.joinpath(DEFAULT_DEPS_DIR) + for arch in deps_dir.iterdir(): if not arch.is_dir(): continue log.debug(f"Preparing repofile content for arch '{arch.name}'") @@ -281,7 +281,7 @@ def _generate_repofiles(from_output_dir: Path, for_output_dir: Path) -> None: if not entry.is_dir() or entry.name == "repos.d": continue repoid = entry.name - localpath = for_output_dir.joinpath(DEFAULT_PACKAGE_DIR, arch.name, repoid) + localpath = for_output_dir.joinpath(DEFAULT_DEPS_DIR, arch.name, repoid) content += f"[{repoid}]\n" content += f"baseurl=file://{localpath}\n" content += "gpgcheck=1\n" diff --git a/tests/unit/package_managers/test_rpm.py b/tests/unit/package_managers/test_rpm.py index cba26b5ae..a2ef0cc83 100644 --- a/tests/unit/package_managers/test_rpm.py +++ b/tests/unit/package_managers/test_rpm.py @@ -10,7 +10,7 @@ from cachi2.core.package_managers.rpm import fetch_rpm_source, inject_files_post from cachi2.core.package_managers.rpm.main import ( DEFAULT_LOCKFILE_NAME, - DEFAULT_PACKAGE_DIR, + DEFAULT_DEPS_DIR, _createrepo, _download, _generate_repofiles, @@ -59,9 +59,9 @@ def test_fetch_rpm_source( def test_resolve_rpm_project_no_lockfile(rooted_tmp_path: RootedPath) -> None: with pytest.raises(PackageRejected) as exc_info: - mock_source_dir = mock.Mock() - mock_source_dir.join_within_root.return_value.path.exists.return_value = False - _resolve_rpm_project(mock_source_dir, mock.Mock()) + mock_package_dir = mock.Mock() + mock_package_dir.join_within_root.return_value.path.exists.return_value = False + _resolve_rpm_project(mock_package_dir, mock.Mock()) assert f"RPM lockfile '{DEFAULT_LOCKFILE_NAME}' missing, refusing to continue" in str( exc_info.value ) @@ -279,15 +279,15 @@ def test_resolve_rpm_project( mock_open: mock.Mock, ) -> None: output_dir = mock.Mock() - mock_package_dir_path = mock.Mock() - output_dir.join_within_root.return_value.path = mock_package_dir_path + mock_deps_dir_path = mock.Mock() + output_dir.join_within_root.return_value.path = mock_deps_dir_path mock_download.return_value = {} - source_dir = mock.Mock() - source_dir.subpath_from_root = Path() + package_dir = mock.Mock() + package_dir.subpath_from_root = Path() - _resolve_rpm_project(source_dir, output_dir) - mock_download.assert_called_once_with(mock_model_validate.return_value, mock_package_dir_path) + _resolve_rpm_project(package_dir, output_dir) + mock_download.assert_called_once_with(mock_model_validate.return_value, mock_deps_dir_path) mock_verify_downloaded.assert_called_once_with({}) mock_generate_sbom_components.assert_called_once_with({}, "rpms.lock.yaml") @@ -302,7 +302,7 @@ def test_createrepo(mock_run_cmd: mock.Mock, rooted_tmp_path: RootedPath) -> Non @mock.patch("cachi2.core.package_managers.rpm.main._createrepo") def test_generate_repos(mock_createrepo: mock.Mock, rooted_tmp_path: RootedPath) -> None: - package_dir = rooted_tmp_path.join_within_root(DEFAULT_PACKAGE_DIR) + package_dir = rooted_tmp_path.join_within_root(DEFAULT_DEPS_DIR) arch_dir = package_dir.path.joinpath("x86_64") arch_dir.joinpath("repo1").mkdir(parents=True) arch_dir.joinpath("repos.d").mkdir(parents=True) @@ -311,7 +311,7 @@ def test_generate_repos(mock_createrepo: mock.Mock, rooted_tmp_path: RootedPath) def test_generate_repofiles(rooted_tmp_path: RootedPath) -> None: - package_dir = rooted_tmp_path.join_within_root(DEFAULT_PACKAGE_DIR) + package_dir = rooted_tmp_path.join_within_root(DEFAULT_DEPS_DIR) arch_dir = package_dir.path.joinpath("x86_64") arch_dir.joinpath("repo1").mkdir(parents=True) arch_dir.joinpath("cachi2-repo2").mkdir(parents=True)