diff --git a/pmb/chroot/apk.py b/pmb/chroot/apk.py index 21367589..cca7aadc 100644 --- a/pmb/chroot/apk.py +++ b/pmb/chroot/apk.py @@ -18,6 +18,8 @@ along with pmbootstrap. If not, see . """ import os import logging +import shlex + import pmb.chroot import pmb.config import pmb.parse.apkindex @@ -25,6 +27,53 @@ import pmb.parse.arch import pmb.parse.depends +def update_repository_list(args, suffix="native", check=False): + """ + Update /etc/apk/repositories, if it is outdated (when the user changed the + --mirror-alpine or --mirror-pmOS parameters). + + :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. + """ + # Skip if we already did this + if suffix in args.cache["apk_repository_list_updated"]: + return + + # Read old entries or create folder structure + path = args.work + "/chroot_" + suffix + "/etc/apk/repositories" + lines_old = [] + if os.path.exists(path): + # Read all old lines + lines_old = [] + with open(path) as handle: + for line in handle: + lines_old.append(line[:-1]) + else: + pmb.helpers.run.root(args, ["mkdir", "-p", os.path.dirname(path)], + suffix) + + # Up to date: Save cache, return + lines_new = pmb.helpers.repo.urls(args) + if lines_old == lines_new: + args.cache["apk_repository_list_updated"].append(suffix) + return + + # Check phase: raise error when still outdated + if check: + raise RuntimeError("Failed to update: " + path) + + # Update the file + logging.debug("(" + suffix + ") update /etc/apk/repositories") + if os.path.exists(path): + pmb.helpers.run.root(args, ["rm", path]) + for line in lines_new: + pmb.helpers.run.root(args, ["sh", "-c", "echo " + + shlex.quote(line) + " >> " + path]) + update_repository_list(args, suffix, True) + + def check_min_version(args, suffix="native"): """ Check the minimum apk version, before running it the first time in the diff --git a/pmb/chroot/init.py b/pmb/chroot/init.py index 48a0be5f..20b2f135 100644 --- a/pmb/chroot/init.py +++ b/pmb/chroot/init.py @@ -18,7 +18,6 @@ along with pmbootstrap. If not, see . """ import logging import os -import shlex import glob import filecmp @@ -51,6 +50,7 @@ def init(args, suffix="native"): if suffix != "native": pmb.chroot.binfmt.register(args, arch) copy_resolv_conf(args, suffix) + pmb.chroot.apk.update_repository_list(args, suffix) return # Require apk-tools-static @@ -69,19 +69,13 @@ def init(args, suffix="native"): pmb.helpers.run.root(args, ["ln", "-s", "/var/cache/apk", chroot + "/etc/apk/cache"]) - # Copy /etc/apk/keys/ and resolv.conf + # Initialize /etc/apk/keys/, resolv.conf, repositories logging.debug(pmb.config.apk_keys_path) for key in glob.glob(pmb.config.apk_keys_path + "/*.pub"): pmb.helpers.run.root(args, ["cp", key, args.work + "/config_apk_keys/"]) copy_resolv_conf(args, suffix) - - # Write /etc/apk/repositories - repos_path = chroot + "/etc/apk/repositories" - if not os.path.exists(repos_path): - for line in pmb.helpers.repo.urls(args): - pmb.helpers.run.root(args, ["sh", "-c", - "echo " + shlex.quote(line) + " >> " + repos_path]) + pmb.chroot.apk.update_repository_list(args, suffix) # Install alpine-base (no clean exit for non-native chroot!) pmb.chroot.apk_static.run(args, ["-U", "--root", chroot, diff --git a/pmb/parse/arguments.py b/pmb/parse/arguments.py index 56fc05ae..0a6f5acd 100644 --- a/pmb/parse/arguments.py +++ b/pmb/parse/arguments.py @@ -225,6 +225,7 @@ def arguments(): "apkindex_files": {}, "apkbuild": {}, "apk_min_version_checked": [], + "apk_repository_list_updated": [], "aports_files_out_of_sync_with_git": None, "find_aport": {}})