Skip to content

Commit

Permalink
rpms: Rename source_dir variable to package_dir
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
brunoapimentel committed Apr 23, 2024
1 parent 3c6b69b commit 574f5b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
32 changes: 16 additions & 16 deletions cachi2/core/package_managers/rpm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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=(
Expand All @@ -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:
Expand All @@ -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)


Expand Down Expand Up @@ -233,16 +233,16 @@ 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)


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():
Expand Down Expand Up @@ -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}'")
Expand All @@ -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"
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/package_managers/test_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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")

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 574f5b2

Please sign in to comment.