build.find_aport() -> helpers.pmaports.find()

Move find_aport() and find_aport_guess_main() from pmb/build/other.py
to the new file pmb/helpers/pmaports.py.

Finding aports is not only needed when building packages, hence it
makes sense to move it out of pmb.build. The pmb/helpers/pmaports.py
file will have more pmaports related functions in a follow up commit.
This commit is contained in:
Oliver Smith 2018-11-15 08:30:49 +01:00 committed by Martijn Braam
parent d7b888907f
commit a44b80b31d
16 changed files with 135 additions and 101 deletions

View File

@ -22,6 +22,6 @@ from pmb.build.checksum import checksum
from pmb.build.menuconfig import menuconfig from pmb.build.menuconfig import menuconfig
from pmb.build.newapkbuild import newapkbuild from pmb.build.newapkbuild import newapkbuild
from pmb.build.other import copy_to_buildpath, is_necessary, \ from pmb.build.other import copy_to_buildpath, is_necessary, \
find_aport, index_repo index_repo
from pmb.build._package import package from pmb.build._package import package
from pmb.build.qemu_workaround_aarch64 import qemu_workaround_aarch64 from pmb.build.qemu_workaround_aarch64 import qemu_workaround_aarch64

View File

@ -25,6 +25,7 @@ import pmb.build.autodetect
import pmb.chroot import pmb.chroot
import pmb.chroot.apk import pmb.chroot.apk
import pmb.chroot.distccd import pmb.chroot.distccd
import pmb.helpers.pmaports
import pmb.helpers.repo import pmb.helpers.repo
import pmb.parse import pmb.parse
import pmb.parse.arch import pmb.parse.arch
@ -59,7 +60,7 @@ def get_apkbuild(args, pkgname, arch):
pmb.helpers.repo.update(args, arch) pmb.helpers.repo.update(args, arch)
# Get aport, skip upstream only packages # Get aport, skip upstream only packages
aport = pmb.build.find_aport(args, pkgname, False) aport = pmb.helpers.pmaports.find(args, pkgname, False)
if aport: if aport:
return pmb.parse.apkbuild(args, aport + "/APKBUILD") return pmb.parse.apkbuild(args, aport + "/APKBUILD")
if pmb.parse.apkindex.providers(args, pkgname, arch, False): if pmb.parse.apkindex.providers(args, pkgname, arch, False):

View File

@ -22,6 +22,7 @@ import os
import pmb.config import pmb.config
import pmb.chroot.apk import pmb.chroot.apk
import pmb.helpers.pmaports
import pmb.parse.arch import pmb.parse.arch
@ -59,7 +60,7 @@ def arch(args, pkgname):
* device arch * device arch
* first arch in the APKBUILD * first arch in the APKBUILD
""" """
aport = pmb.build.find_aport(args, pkgname) aport = pmb.helpers.pmaports.find(args, pkgname)
ret = arch_from_deviceinfo(args, pkgname, aport) ret = arch_from_deviceinfo(args, pkgname, aport)
if ret: if ret:
return ret return ret

View File

@ -21,7 +21,7 @@ import logging
import pmb.chroot import pmb.chroot
import pmb.build import pmb.build
import pmb.helpers.run import pmb.helpers.run
import pmb.build.other import pmb.helpers.pmaports
def checksum(args, pkgname): def checksum(args, pkgname):
@ -33,5 +33,5 @@ def checksum(args, pkgname):
# Copy modified APKBUILD back # Copy modified APKBUILD back
source = args.work + "/chroot_native/home/pmos/build/APKBUILD" source = args.work + "/chroot_native/home/pmos/build/APKBUILD"
target = pmb.build.other.find_aport(args, pkgname) + "/" target = pmb.helpers.pmaports.find(args, pkgname) + "/"
pmb.helpers.run.user(args, ["cp", source, target]) pmb.helpers.run.user(args, ["cp", source, target])

View File

@ -25,6 +25,7 @@ import pmb.build.checksum
import pmb.chroot import pmb.chroot
import pmb.chroot.apk import pmb.chroot.apk
import pmb.chroot.other import pmb.chroot.other
import pmb.helpers.pmaports
import pmb.helpers.run import pmb.helpers.run
import pmb.parse import pmb.parse
@ -97,7 +98,7 @@ def menuconfig(args, pkgname):
pkgname = "linux-" + pkgname pkgname = "linux-" + pkgname
# Read apkbuild # Read apkbuild
aport = pmb.build.find_aport(args, pkgname) aport = pmb.helpers.pmaports.find(args, pkgname)
apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD") apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD")
arch = get_arch(args, apkbuild) arch = get_arch(args, apkbuild)
kopt = "menuconfig" kopt = "menuconfig"

View File

@ -21,95 +21,18 @@ import logging
import os import os
import shlex import shlex
import pmb.build.other
import pmb.chroot import pmb.chroot
import pmb.helpers.file import pmb.helpers.file
import pmb.helpers.git import pmb.helpers.git
import pmb.helpers.pmaports
import pmb.helpers.run import pmb.helpers.run
import pmb.parse.apkindex import pmb.parse.apkindex
import pmb.parse.version import pmb.parse.version
def find_aport_guess_main(args, subpkgname):
"""
Find the main package by assuming it is a prefix of the subpkgname.
We do that, because in some APKBUILDs the subpkgname="" variable gets
filled with a shell loop and the APKBUILD parser in pmbootstrap can't
parse this right. (Intentionally, we don't want to implement a full shell
parser.)
:param subpkgname: subpackage name (e.g. "u-boot-some-device")
:returns: * full path to the aport, e.g.:
"/home/user/code/pmbootstrap/aports/main/u-boot"
* None when we couldn't find a main package
"""
# Iterate until the cut up subpkgname is gone
words = subpkgname.split("-")
while len(words) > 1:
# Remove one dash-separated word at a time ("a-b-c" -> "a-b")
words.pop()
pkgname = "-".join(words)
# Look in pmaports
paths = glob.glob(args.aports + "/*/" + pkgname)
if paths:
logging.debug(subpkgname + ": guessed to be a subpackage of " +
pkgname)
return paths[0]
def find_aport(args, package, must_exist=True):
"""
Find the aport, that provides a certain subpackage.
:param must_exist: Raise an exception, when not found
:returns: the full path to the aport folder
"""
# Try to get a cached result first (we assume, that the aports don't change
# in one pmbootstrap call)
ret = None
if package in args.cache["find_aport"]:
ret = args.cache["find_aport"][package]
else:
# Sanity check
if "*" in package:
raise RuntimeError("Invalid pkgname: " + package)
# Search in packages
paths = glob.glob(args.aports + "/*/" + package)
if len(paths) > 1:
raise RuntimeError("Package " + package + " found in multiple"
" aports subfolders. Please put it only in one"
" folder.")
elif len(paths) == 1:
ret = paths[0]
# Search in subpackages
if not ret:
for path_current in glob.glob(args.aports + "/*/*/APKBUILD"):
apkbuild = pmb.parse.apkbuild(args, path_current)
if (package in apkbuild["subpackages"] or
package in apkbuild["provides"]):
ret = os.path.dirname(path_current)
break
# Guess a main package
if not ret:
ret = find_aport_guess_main(args, package)
# Crash when necessary
if ret is None and must_exist:
raise RuntimeError("Could not find aport for package: " +
package)
# Save result in cache
args.cache["find_aport"][package] = ret
return ret
def copy_to_buildpath(args, package, suffix="native"): def copy_to_buildpath(args, package, suffix="native"):
# Sanity check # Sanity check
aport = find_aport(args, package) aport = pmb.helpers.pmaports.find(args, package)
if not os.path.exists(aport + "/APKBUILD"): if not os.path.exists(aport + "/APKBUILD"):
raise ValueError("Path does not contain an APKBUILD file:" + raise ValueError("Path does not contain an APKBUILD file:" +
aport) aport)

View File

@ -22,6 +22,7 @@ import shlex
import pmb.chroot import pmb.chroot
import pmb.config import pmb.config
import pmb.helpers.pmaports
import pmb.parse.apkindex import pmb.parse.apkindex
import pmb.parse.arch import pmb.parse.arch
import pmb.parse.depends import pmb.parse.depends
@ -166,7 +167,7 @@ def replace_aports_packages_with_path(args, packages, suffix, arch):
""" """
ret = [] ret = []
for package in packages: for package in packages:
aport = pmb.build.find_aport(args, package, False) aport = pmb.helpers.pmaports.find(args, package, False)
if aport: if aport:
data_repo = pmb.parse.apkindex.package(args, package, arch, False) data_repo = pmb.parse.apkindex.package(args, package, arch, False)
apk_path = ("/mnt/pmbootstrap-packages/" + arch + "/" + apk_path = ("/mnt/pmbootstrap-packages/" + arch + "/" +

View File

@ -22,6 +22,7 @@ import math
import os import os
import pmb.chroot import pmb.chroot
import pmb.helpers.pmaports
import pmb.helpers.run import pmb.helpers.run
import pmb.parse.apkindex import pmb.parse.apkindex
@ -121,7 +122,7 @@ def zap_pkgs_local_mismatch(args, confirm=True, dry=False):
continue continue
# Aport path # Aport path
aport_path = pmb.build.other.find_aport(args, origin, False) aport_path = pmb.helpers.pmaports.find(args, origin, False)
if not aport_path: if not aport_path:
logging.info("% rm " + apk_path_short + " (" + origin + logging.info("% rm " + apk_path_short + " (" + origin +
" aport not found)") " aport not found)")

View File

@ -35,8 +35,8 @@ import pmb.chroot.other
import pmb.export import pmb.export
import pmb.flasher import pmb.flasher
import pmb.helpers.logging import pmb.helpers.logging
import pmb.helpers.other
import pmb.helpers.pkgrel_bump import pmb.helpers.pkgrel_bump
import pmb.helpers.pmaports
import pmb.helpers.repo import pmb.helpers.repo
import pmb.helpers.run import pmb.helpers.run
import pmb.install import pmb.install
@ -266,7 +266,7 @@ def apkbuild_parse(args):
packages.sort() packages.sort()
for package in packages: for package in packages:
print(package + ":") print(package + ":")
aport = pmb.build.other.find_aport(args, package) aport = pmb.helpers.pmaports.find(args, package)
path = aport + "/APKBUILD" path = aport + "/APKBUILD"
print(json.dumps(pmb.parse.apkbuild(args, path), indent=4, print(json.dumps(pmb.parse.apkbuild(args, path), indent=4,
sort_keys=True)) sort_keys=True))
@ -289,7 +289,7 @@ def pkgrel_bump(args):
else: else:
# Each package must exist # Each package must exist
for package in args.packages: for package in args.packages:
pmb.build.other.find_aport(args, package) pmb.helpers.pmaports.find(args, package)
# Increase pkgrel # Increase pkgrel
for package in args.packages: for package in args.packages:

View File

@ -21,6 +21,7 @@ import os
import re import re
import pmb.chroot import pmb.chroot
import pmb.config import pmb.config
import pmb.helpers.pmaports
import pmb.helpers.run import pmb.helpers.run

View File

@ -19,8 +19,8 @@ along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import os import os
import pmb.build.other
import pmb.helpers.file import pmb.helpers.file
import pmb.helpers.pmaports
import pmb.helpers.repo import pmb.helpers.repo
import pmb.parse import pmb.parse
@ -34,7 +34,7 @@ def package(args, pkgname, reason="", dry=False):
:param dry: don't modify the APKBUILD, just print the message :param dry: don't modify the APKBUILD, just print the message
""" """
# Current and new pkgrel # Current and new pkgrel
path = pmb.build.other.find_aport(args, pkgname) + "/APKBUILD" path = pmb.helpers.pmaports.find(args, pkgname) + "/APKBUILD"
apkbuild = pmb.parse.apkbuild(args, path) apkbuild = pmb.parse.apkbuild(args, path)
pkgrel = int(apkbuild["pkgrel"]) pkgrel = int(apkbuild["pkgrel"])
pkgrel_new = pkgrel + 1 pkgrel_new = pkgrel + 1
@ -128,7 +128,7 @@ def auto_apkindex_package(args, arch, aport, apk, dry=False):
# (which means dynamic libraries that the package was linked # (which means dynamic libraries that the package was linked
# against) and packages for which no aport exists. # against) and packages for which no aport exists.
if (depend.startswith("so:") or if (depend.startswith("so:") or
not pmb.build.other.find_aport(args, depend, False)): not pmb.helpers.pmaports.find(args, depend, False)):
missing.append(depend) missing.append(depend)
# Increase pkgrel # Increase pkgrel
@ -154,7 +154,7 @@ def auto(args, dry=False):
logging.verbose("{}: origin '{}' found again".format(pkgname, logging.verbose("{}: origin '{}' found again".format(pkgname,
origin)) origin))
continue continue
aport_path = pmb.build.other.find_aport(args, origin, False) aport_path = pmb.helpers.pmaports.find(args, origin, False)
if not aport_path: if not aport_path:
logging.warning("{}: origin '{}' aport not found".format( logging.warning("{}: origin '{}' aport not found".format(
pkgname, origin)) pkgname, origin))

103
pmb/helpers/pmaports.py Normal file
View File

@ -0,0 +1,103 @@
#!/usr/bin/env python3
"""
Copyright 2018 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 glob
import logging
import os
import pmb.parse
def guess_main(args, subpkgname):
"""
Find the main package by assuming it is a prefix of the subpkgname.
We do that, because in some APKBUILDs the subpkgname="" variable gets
filled with a shell loop and the APKBUILD parser in pmbootstrap can't
parse this right. (Intentionally, we don't want to implement a full shell
parser.)
:param subpkgname: subpackage name (e.g. "u-boot-some-device")
:returns: * full path to the aport, e.g.:
"/home/user/code/pmbootstrap/aports/main/u-boot"
* None when we couldn't find a main package
"""
# Iterate until the cut up subpkgname is gone
words = subpkgname.split("-")
while len(words) > 1:
# Remove one dash-separated word at a time ("a-b-c" -> "a-b")
words.pop()
pkgname = "-".join(words)
# Look in pmaports
paths = glob.glob(args.aports + "/*/" + pkgname)
if paths:
logging.debug(subpkgname + ": guessed to be a subpackage of " +
pkgname)
return paths[0]
def find(args, package, must_exist=True):
"""
Find the aport, that provides a certain subpackage.
:param must_exist: Raise an exception, when not found
:returns: the full path to the aport folder
"""
# Try to get a cached result first (we assume, that the aports don't change
# in one pmbootstrap call)
ret = None
if package in args.cache["find_aport"]:
ret = args.cache["find_aport"][package]
else:
# Sanity check
if "*" in package:
raise RuntimeError("Invalid pkgname: " + package)
# Search in packages
paths = glob.glob(args.aports + "/*/" + package)
if len(paths) > 1:
raise RuntimeError("Package " + package + " found in multiple"
" aports subfolders. Please put it only in one"
" folder.")
elif len(paths) == 1:
ret = paths[0]
# Search in subpackages
if not ret:
for path_current in glob.glob(args.aports + "/*/*/APKBUILD"):
apkbuild = pmb.parse.apkbuild(args, path_current)
if (package in apkbuild["subpackages"] or
package in apkbuild["provides"]):
ret = os.path.dirname(path_current)
break
# Guess a main package
if not ret:
ret = guess_main(args, package)
# Crash when necessary
if ret is None and must_exist:
raise RuntimeError("Could not find aport for package: " +
package)
# Save result in cache
args.cache["find_aport"][package] = ret
return ret

View File

@ -19,6 +19,7 @@ along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import pmb.chroot import pmb.chroot
import pmb.chroot.apk import pmb.chroot.apk
import pmb.helpers.pmaports
import pmb.parse.apkindex import pmb.parse.apkindex
import pmb.parse.arch import pmb.parse.arch
@ -29,7 +30,7 @@ def package_from_aports(args, pkgname_depend):
depends, version. The version is the combined pkgver and pkgrel. depends, version. The version is the combined pkgver and pkgrel.
""" """
# Get the aport # Get the aport
aport = pmb.build.find_aport(args, pkgname_depend, False) aport = pmb.helpers.pmaports.find(args, pkgname_depend, False)
if not aport: if not aport:
return None return None

View File

@ -24,6 +24,7 @@ import os
import pmb.build import pmb.build
import pmb.config import pmb.config
import pmb.parse import pmb.parse
import pmb.helpers.pmaports
def is_set(config, option): def is_set(config, option):
@ -50,7 +51,7 @@ def check(args, pkgname, details=False):
# Read all kernel configs in the aport # Read all kernel configs in the aport
ret = True ret = True
aport = pmb.build.find_aport(args, "linux-" + flavor) aport = pmb.helpers.pmaports.find(args, "linux-" + flavor)
for config_path in glob.glob(aport + "/config-*"): for config_path in glob.glob(aport + "/config-*"):
logging.debug("Check kconfig: " + config_path) logging.debug("Check kconfig: " + config_path)
with open(config_path) as handle: with open(config_path) as handle:

View File

@ -23,8 +23,8 @@ import pytest
# Import from parent directory # Import from parent directory
sys.path.insert(0, os.path.realpath( sys.path.insert(0, os.path.realpath(
os.path.join(os.path.dirname(__file__) + "/.."))) os.path.join(os.path.dirname(__file__) + "/..")))
import pmb.build.other
import pmb.helpers.logging import pmb.helpers.logging
import pmb.helpers.pmaports
@pytest.fixture @pytest.fixture
@ -59,7 +59,7 @@ def cache_apkindex(args, version):
def test_build_is_necessary(args): def test_build_is_necessary(args):
# Prepare APKBUILD and APKINDEX data # Prepare APKBUILD and APKINDEX data
aport = pmb.build.other.find_aport(args, "hello-world") aport = pmb.helpers.pmaports.find(args, "hello-world")
apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD") apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD")
apkbuild["pkgver"] = "1" apkbuild["pkgver"] = "1"
apkbuild["pkgrel"] = "2" apkbuild["pkgrel"] = "2"
@ -88,6 +88,6 @@ def test_build_is_necessary_no_binary_available(args):
hello-world package has not been built yet. hello-world package has not been built yet.
""" """
indexes = list(args.cache["apkindex"].keys()) indexes = list(args.cache["apkindex"].keys())
aport = pmb.build.other.find_aport(args, "hello-world") aport = pmb.helpers.pmaports.find(args, "hello-world")
apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD") apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD")
assert pmb.build.is_necessary(args, None, apkbuild, indexes) is True assert pmb.build.is_necessary(args, None, apkbuild, indexes) is True

View File

@ -38,14 +38,14 @@ def args(request):
return args return args
def test_find_aport_guess_main(args, tmpdir): def test_guess_main(args, tmpdir):
# Fake pmaports folder # Fake pmaports folder
tmpdir = str(tmpdir) tmpdir = str(tmpdir)
args.aports = tmpdir args.aports = tmpdir
for aport in ["temp/qemu", "main/some-pkg"]: for aport in ["temp/qemu", "main/some-pkg"]:
os.makedirs(tmpdir + "/" + aport) os.makedirs(tmpdir + "/" + aport)
func = pmb.build.other.find_aport_guess_main func = pmb.helpers.pmaports.guess_main
assert func(args, "qemu-x86_64") == tmpdir + "/temp/qemu" assert func(args, "qemu-x86_64") == tmpdir + "/temp/qemu"
assert func(args, "qemu-system-x86_64") == tmpdir + "/temp/qemu" assert func(args, "qemu-system-x86_64") == tmpdir + "/temp/qemu"
assert func(args, "some-pkg-sub-pkg") == tmpdir + "/main/some-pkg" assert func(args, "some-pkg-sub-pkg") == tmpdir + "/main/some-pkg"