core: pkgrepo: support arbitrarily named pmaports directories (MR 2470)

There was an oversight when this API was originally created and it
implicitly assumed that the pmaports repository was always named
"pmaports". This unfortunately broke some peoples workflows.

Introduce a new "pkgrepo_name()" function and adjust the codebase to use
it, as well as adjusting pkgrepo internally to special case the
"pmaports" repo so that it's always named pmaports no matter what the
directory itself is named.

This is probably more complexity than we should be dealing with here, we
should probably create a new type to encode this behaviour.

Fixes: #2412
Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-11-02 16:01:46 +01:00 committed by Oliver Smith
parent 70dc855281
commit a72a60f546
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
5 changed files with 49 additions and 15 deletions

View file

@ -1,6 +1,6 @@
import enum
from pathlib import Path
from pmb.core.pkgrepo import pkgrepo_paths
from pmb.core.pkgrepo import pkgrepo_name, pkgrepo_paths
import pmb.helpers.run
import pmb.chroot
@ -126,10 +126,10 @@ def mount_pmaports(chroot: Chroot = Chroot.native()) -> dict[str, Path]:
"""
dest_paths = {}
for repo in pkgrepo_paths(skip_extras=True):
destination = Path("/mnt") / repo.name
destination = Path("/mnt") / pkgrepo_name(repo)
outside_destination = chroot / destination
pmb.helpers.mount.bind(repo, outside_destination, umount=True)
dest_paths[repo.name] = destination
dest_paths[pkgrepo_name(repo)] = destination
return dest_paths

View file

@ -2,7 +2,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import configparser
from pathlib import Path
from pmb.core.pkgrepo import pkgrepo_default_path, pkgrepo_paths, pkgrepo_relative_path
from pmb.core.pkgrepo import (
pkgrepo_default_path,
pkgrepo_name,
pkgrepo_paths,
pkgrepo_relative_path,
)
from pmb.helpers import logging
import os
import sys
@ -97,7 +102,7 @@ def read_config(aports: Path | None = None) -> dict[str, Any]:
if aports is None:
aports = pkgrepo_paths()[0]
systemd = aports.name == "systemd"
systemd = pkgrepo_name(aports) == "systemd"
# extra-repos don't have a pmaports.cfg
# so jump up the main aports dir
if "extra-repos" in aports.parts:

View file

@ -28,21 +28,42 @@ def pkgrepo_paths(skip_extras: bool = False) -> list[Path]:
return out_paths
@Cache()
def pkgrepo_default_path() -> Path:
return pkgrepo_paths(skip_extras=True)[0]
def pkgrepo_names(skip_exras: bool = False) -> list[str]:
"""
Return a list of all the package repository names.
Return a list of all the package repository names. We REQUIRE
that the last repository is "pmaports", though the directory
may be named differently. So we hardcode the name here.
"""
return [aports.name for aports in pkgrepo_paths(skip_exras)]
names = [aports.name for aports in pkgrepo_paths(skip_exras)]
names[-1] = "pmaports"
return names
def pkgrepo_name(path: Path) -> str:
"""
Return the name of the package repository with the given path. This
MUST be used instead of "path.name" as we need special handling
for the pmaports repository.
"""
if path == get_context().config.aports[-1]:
return "pmaports"
return path.name
def pkgrepo_path(name: str) -> Path:
"""
Return the absolute path to the package repository with the given name.
"""
# The pmaports repo is always last, and we hardcode the name.
if name == "pmaports":
return get_context().config.aports[-1]
for aports in pkgrepo_paths():
if aports.name == name:
return aports
@ -56,7 +77,7 @@ def pkgrepo_name_from_subdir(subdir: Path) -> str:
"""
for aports in pkgrepo_paths():
if subdir.is_relative_to(aports):
return aports.name
return pkgrepo_name(aports)
raise RuntimeError(f"aports subdir '{subdir}' not found")
@ -105,14 +126,14 @@ def pkgrepo_iter_package_dirs(skip_extra_repos: bool = False) -> Generator[Path,
if "extra-repos" not in repo.parts and "extra-repos" in pdir.parts:
continue
pkg = os.path.basename(pdir)
if pkg in seen[repo.name]:
if pkg in seen[pkgrepo_name(repo)]:
raise RuntimeError(
f"Package {pkg} found in multiple aports "
"subfolders. Please put it only in one folder."
)
if pkg in [x for li in seen.values() for x in li]:
continue
seen[repo.name].append(pkg)
seen[pkgrepo_name(repo)].append(pkg)
yield pdir

View file

@ -5,7 +5,7 @@ from enum import Enum
from pathlib import Path
from typing import Final
from pmb.core.context import get_context
from pmb.core.pkgrepo import pkgrepo_default_path, pkgrepo_path
from pmb.core.pkgrepo import pkgrepo_default_path, pkgrepo_path, pkgrepo_name
from pmb.helpers import logging
import os
import re
@ -107,7 +107,10 @@ def get_upstream_remote(aports: Path) -> str:
Usually "origin", but the user may have set up their git repository differently.
"""
name_repo = aports.parts[-1]
name_repo = pkgrepo_name(aports)
if name_repo not in pmb.config.git_repos:
logging.warning(f"WARNING: can't determine remote for {name_repo}, using 'origin'")
return "origin"
urls = pmb.config.git_repos[name_repo]
lines = list_remotes(aports)
for line in lines:

View file

@ -2,7 +2,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from collections.abc import Sequence
from pmb.core.chroot import Chroot
from pmb.core.pkgrepo import pkgrepo_iter_package_dirs, pkgrepo_names, pkgrepo_relative_path
from pmb.core.pkgrepo import (
pkgrepo_iter_package_dirs,
pkgrepo_name,
pkgrepo_names,
pkgrepo_relative_path,
)
from pmb.helpers import logging
from pmb.helpers.exceptions import NonBugError
from pmb.helpers.toml import load_toml_file
@ -69,7 +74,7 @@ def check(pkgnames: Sequence[str]) -> None:
continue
repo, relpath = pkgrepo_relative_path(pkgdir)
apkbuilds[repo.name].append(os.fspath(relpath / "APKBUILD"))
apkbuilds[pkgrepo_name(repo)].append(os.fspath(relpath / "APKBUILD"))
found_pkgnames.add(pkgdir.name)
# Check we found all the packages in pkgnames
@ -94,7 +99,7 @@ def check(pkgnames: Sequence[str]) -> None:
["apkbuild-lint"] + apkbuild_paths,
check=False,
output="stdout",
working_dir=dest_paths[repo.name],
working_dir=dest_paths[pkgrepo_name(repo)],
env={"CUSTOM_VALID_OPTIONS": " ".join(get_custom_valid_options())},
):
has_failed = True