helpers: apk: move update_repository_list() from chroot.apk (MR 2463)

This function better belongs here, especially as it will be used outside
of the context of a chroot() soon.

Additionally, it's adjusted to take the rootfs path as its first
argument rather than a chroot, since it could operate on any rootfs.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-10-27 23:10:59 +01:00
parent af1bf53867
commit 12846f3b8e
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
6 changed files with 62 additions and 57 deletions

View file

@ -9,7 +9,6 @@ import traceback
import pmb.chroot.apk_static import pmb.chroot.apk_static
from pmb.core.arch import Arch from pmb.core.arch import Arch
from pmb.helpers import logging from pmb.helpers import logging
import shlex
from collections.abc import Sequence from collections.abc import Sequence
import pmb.build import pmb.build
@ -30,57 +29,6 @@ from pmb.types import PathString
from pmb.helpers.exceptions import NonBugError from pmb.helpers.exceptions import NonBugError
@Cache("chroot", "user_repository", mirrors_exclude=[])
def update_repository_list(
chroot: Chroot,
user_repository: bool = False,
mirrors_exclude: list[str] = [],
check: bool = False,
) -> None:
"""
Update /etc/apk/repositories, if it is outdated (when the user changed the
--mirror-alpine or --mirror-pmOS parameters).
:param mirrors_exclude: mirrors to exclude from the repository list
:param check: This function calls it self after updating the
/etc/apk/repositories file, to check if it was successful.
Only for this purpose, the "check" parameter should be set to
True.
"""
# Read old entries or create folder structure
path = chroot / "etc/apk/repositories"
lines_old: list[str] = []
if path.exists():
# Read all old lines
lines_old = []
with path.open() as handle:
for line in handle:
lines_old.append(line[:-1])
else:
pmb.helpers.run.root(["mkdir", "-p", path.parent])
# Up to date: Save cache, return
lines_new = pmb.helpers.repo.urls(
user_repository=user_repository, mirrors_exclude=mirrors_exclude
)
if lines_old == lines_new:
return
# Check phase: raise error when still outdated
if check:
raise RuntimeError(f"Failed to update: {path}")
# Update the file
logging.debug(f"({chroot}) update /etc/apk/repositories")
if path.exists():
pmb.helpers.run.root(["rm", path])
for line in lines_new:
pmb.helpers.run.root(["sh", "-c", "echo " f"{shlex.quote(line)} >> {path}"])
update_repository_list(
chroot, user_repository=user_repository, mirrors_exclude=mirrors_exclude, check=True
)
@Cache("chroot") @Cache("chroot")
def check_min_version(chroot: Chroot = Chroot.native()) -> None: def check_min_version(chroot: Chroot = Chroot.native()) -> None:
""" """

View file

@ -136,7 +136,7 @@ def init(chroot: Chroot, usr_merge: UsrMerge = UsrMerge.AUTO) -> None:
mark_in_chroot(chroot) mark_in_chroot(chroot)
if chroot.exists(): if chroot.exists():
copy_resolv_conf(chroot) copy_resolv_conf(chroot)
pmb.chroot.apk.update_repository_list(chroot) pmb.helpers.apk.update_repository_list(chroot.path)
warn_if_chroots_outdated() warn_if_chroots_outdated()
return return
@ -152,7 +152,7 @@ def init(chroot: Chroot, usr_merge: UsrMerge = UsrMerge.AUTO) -> None:
# Initialize /etc/apk/keys/, resolv.conf, repositories # Initialize /etc/apk/keys/, resolv.conf, repositories
init_keys() init_keys()
copy_resolv_conf(chroot) copy_resolv_conf(chroot)
pmb.chroot.apk.update_repository_list(chroot) pmb.helpers.apk.update_repository_list(chroot.path)
pmb.config.workdir.chroot_save_init(chroot) pmb.config.workdir.chroot_save_init(chroot)

View file

@ -11,6 +11,7 @@ import pmb.config.workdir
import pmb.chroot import pmb.chroot
import pmb.config.pmaports import pmb.config.pmaports
import pmb.config.workdir import pmb.config.workdir
import pmb.helpers.apk
import pmb.helpers.cli import pmb.helpers.cli
import pmb.helpers.pmaports import pmb.helpers.pmaports
import pmb.helpers.run import pmb.helpers.run
@ -107,7 +108,7 @@ def zap(
pmb.config.workdir.clean() pmb.config.workdir.clean()
# Chroots were zapped, so no repo lists exist anymore # Chroots were zapped, so no repo lists exist anymore
pmb.chroot.apk.update_repository_list.cache_clear() pmb.helpers.apk.update_repository_list.cache_clear()
# Let chroot.init be called again # Let chroot.init be called again
pmb.chroot.init.cache_clear() pmb.chroot.init.cache_clear()

View file

@ -109,7 +109,7 @@ class RepoBootstrap(commands.Command):
self.log_progress(f"initializing {chroot} chroot (merge /usr: {usr_merge.name})") self.log_progress(f"initializing {chroot} chroot (merge /usr: {usr_merge.name})")
# Initialize without pmOS binary package repo # Initialize without pmOS binary package repo
pmb.chroot.apk.update_repository_list(chroot, mirrors_exclude=[self.repo]) pmb.helpers.apk.update_repository_list(chroot.path, mirrors_exclude=[self.repo])
pmb.chroot.init(chroot, usr_merge) pmb.chroot.init(chroot, usr_merge)
bootstrap_stage = int(step.split("bootstrap_", 1)[1]) bootstrap_stage = int(step.split("bootstrap_", 1)[1])

View file

@ -1,6 +1,7 @@
# Copyright 2023 Johannes Marbach, Oliver Smith # Copyright 2023 Johannes Marbach, Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import os import os
import shlex
from collections.abc import Sequence from collections.abc import Sequence
from pathlib import Path from pathlib import Path
@ -10,10 +11,65 @@ from pmb.core.arch import Arch
from pmb.core.chroot import Chroot from pmb.core.chroot import Chroot
from pmb.types import PathString from pmb.types import PathString
import pmb.helpers.cli import pmb.helpers.cli
import pmb.helpers.repo
import pmb.helpers.run import pmb.helpers.run
import pmb.helpers.run_core import pmb.helpers.run_core
import pmb.parse.version import pmb.parse.version
from pmb.core.context import get_context from pmb.core.context import get_context
from pmb.helpers import logging
from pmb.meta import Cache
@Cache("root", "user_repository", mirrors_exclude=[])
def update_repository_list(
root: Path,
user_repository: bool = False,
mirrors_exclude: list[str] = [],
check: bool = False,
) -> None:
"""
Update /etc/apk/repositories, if it is outdated (when the user changed the
--mirror-alpine or --mirror-pmOS parameters).
:param root: the root directory to operate on
:param mirrors_exclude: mirrors to exclude from the repository list
:param check: This function calls it self after updating the
/etc/apk/repositories file, to check if it was successful.
Only for this purpose, the "check" parameter should be set to
True.
"""
# Read old entries or create folder structure
path = root / "etc/apk/repositories"
lines_old: list[str] = []
if path.exists():
# Read all old lines
lines_old = []
with path.open() as handle:
for line in handle:
lines_old.append(line[:-1])
else:
pmb.helpers.run.root(["mkdir", "-p", path.parent])
# Up to date: Save cache, return
lines_new = pmb.helpers.repo.urls(
user_repository=user_repository, mirrors_exclude=mirrors_exclude
)
if lines_old == lines_new:
return
# Check phase: raise error when still outdated
if check:
raise RuntimeError(f"Failed to update: {path}")
# Update the file
logging.debug(f"({root.name}) update /etc/apk/repositories")
if path.exists():
pmb.helpers.run.root(["rm", path])
for line in lines_new:
pmb.helpers.run.root(["sh", "-c", "echo " f"{shlex.quote(line)} >> {path}"])
update_repository_list(
root, user_repository=user_repository, mirrors_exclude=mirrors_exclude, check=True
)
def _prepare_fifo() -> Path: def _prepare_fifo() -> Path:

View file

@ -211,7 +211,7 @@ def chroot(args: PmbArgs) -> None:
size_reserve = 2048 # 2 GiB size_reserve = 2048 # 2 GiB
pmb.install.blockdevice.create_and_mount_image(args, size_boot, size_root, size_reserve) pmb.install.blockdevice.create_and_mount_image(args, size_boot, size_root, size_reserve)
pmb.chroot.apk.update_repository_list(chroot, user_repository=True) pmb.helpers.apk.update_repository_list(chroot.path, user_repository=True)
# TODO: Maybe this could be done better. # TODO: Maybe this could be done better.
output_type = cast(RunOutputType, args.output) output_type = cast(RunOutputType, args.output)