pmbootstrap/pmb/chroot/apk.py

100 lines
3.1 KiB
Python

"""
Copyright 2017 Oliver Smith
This file is part of pmbootstrap.
pmbootstrap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pmbootstrap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
import pmb.chroot
import pmb.parse.apkindex
def install(args, packages, suffix="native", build=True):
"""
:param build: automatically build the package, when it does not exist yet
and it is inside the pm-aports folder. Checking this is expensive - if
you know, that all packages are provides by upstream repos, set this to
False!
"""
# Initialize chroot
pmb.chroot.init(args, suffix)
# Filter already installed packages
packages_installed = installed(args, suffix)
packages_todo = []
for package in packages:
if package not in packages_installed:
packages_todo.append(package)
if not len(packages_todo):
return
# Build packages if necessary
arch = pmb.parse.arch.from_chroot_suffix(args, suffix)
if build:
for package in packages_todo:
pmb.build.package(args, package, arch)
# Sanitize packages: don't allow '--allow-untrusted' and other options
# to be passed to apk!
for package in packages_todo:
if package.startswith("-"):
raise ValueError("Invalid package name: " + package)
# Install everything
logging.info("(" + suffix + ") install " + " ".join(packages_todo))
pmb.chroot.root(args, ["apk", "--no-progress", "add"] + packages_todo,
suffix)
def update(args, suffix="native"):
"""
Update all packages installed in a chroot
"""
pmb.chroot.init(args, suffix)
pmb.chroot.root(args, ["apk", "update"], suffix)
def package_split(package):
"""
FIXME: move to pmb.parse
"""
split = package.split("-")
pkgrel = split[-1][1:]
pkgver = split[-2]
version = "-" + pkgver + "-r" + pkgrel
pkgname = package[:-1 * len(version)]
return {"pkgname": pkgname,
"pkgrel": pkgrel,
"pkgver": pkgver,
"package": package}
def installed(args, suffix="native"):
"""
Get all installed packages and their versions.
:returns: { "hello-world": {"package": "hello-world-1-r2", "pkgrel": "2",
"pkgver": "1", "pkgname": "hello-world"}, ...}
"""
ret = {}
list = pmb.chroot.user(args, ["apk", "info", "-vv"], suffix,
return_stdout=True)
for line in list.split("\n"):
if not line.rstrip():
continue
package = line.split(" - ")[0]
split = package_split(package)
ret[split["pkgname"]] = split
return ret