forked from Mirror/pmbootstrap
pmb.helpers.pmaports.get/find: deduplicate code (MR 2299) (MR 2252)
find() gets the pmaports directory of a package, get() parses the APKBUILD inside that directory. Instead of having the extra code path in get() for not searching subpackages, which duplicated part of find(), add a subpackages parameter to get(). Besides the deduplication done now, this is in preparation for code that will extend get() to handle the extra-repos/systemd directory - without this patch, the changes would need to be duplicated in both functions.
This commit is contained in:
parent
f422b5c7ca
commit
34ee1cff69
1 changed files with 17 additions and 22 deletions
|
@ -137,13 +137,18 @@ def _find_package_in_apkbuild(package: str, path: Path) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def find(package: str, must_exist=True) -> Path:
|
def find(package, must_exist=True, subpackages=True):
|
||||||
"""Find the aport path that provides a certain subpackage.
|
"""Find the directory in pmaports that provides a package or subpackage.
|
||||||
|
|
||||||
If you want the parsed APKBUILD instead, use pmb.helpers.pmaports.get().
|
If you want the parsed APKBUILD instead, use pmb.helpers.pmaports.get().
|
||||||
|
|
||||||
:param must_exist: Raise an exception, when not found
|
:param must_exist: Raise an exception, when not found
|
||||||
:returns: the full path to the aport folder
|
:param subpackages: set to False as speed optimization, if you know that
|
||||||
|
the package is not a subpackage of another package
|
||||||
|
(i.e. looking for UI packages for "pmbootstrap init").
|
||||||
|
If a previous search with subpackages=True has found
|
||||||
|
the package already, it will still be returned as
|
||||||
|
cached result.
|
||||||
|
:returns: the full path to the package's dir in pmaports
|
||||||
"""
|
"""
|
||||||
# Try to get a cached result first (we assume that the aports don't change
|
# Try to get a cached result first (we assume that the aports don't change
|
||||||
# in one pmbootstrap call)
|
# in one pmbootstrap call)
|
||||||
|
@ -159,7 +164,7 @@ def find(package: str, must_exist=True) -> Path:
|
||||||
path = _find_apkbuilds().get(package)
|
path = _find_apkbuilds().get(package)
|
||||||
if path:
|
if path:
|
||||||
ret = path.parent
|
ret = path.parent
|
||||||
else:
|
elif subpackages:
|
||||||
# No luck, take a guess what APKBUILD could have the package we are
|
# No luck, take a guess what APKBUILD could have the package we are
|
||||||
# looking for as subpackage
|
# looking for as subpackage
|
||||||
guess = guess_main(package)
|
guess = guess_main(package)
|
||||||
|
@ -187,8 +192,10 @@ def find(package: str, must_exist=True) -> Path:
|
||||||
raise RuntimeError("Could not find aport for package: " +
|
raise RuntimeError("Could not find aport for package: " +
|
||||||
package)
|
package)
|
||||||
|
|
||||||
# Save result in cache
|
# Save result in cache (only if subpackage search was enabled)
|
||||||
pmb.helpers.other.cache["find_aport"][package] = ret
|
if subpackages:
|
||||||
|
pmb.helpers.other.cache["find_aport"][package] = ret
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
@ -219,21 +226,9 @@ def get(pkgname, must_exist=True, subpackages=True):
|
||||||
... }
|
... }
|
||||||
"""
|
"""
|
||||||
pkgname = pmb.helpers.package.remove_operators(pkgname)
|
pkgname = pmb.helpers.package.remove_operators(pkgname)
|
||||||
if subpackages:
|
pmaport = find(pkgname, must_exist, subpackages)
|
||||||
aport = find_optional(pkgname)
|
if pmaport:
|
||||||
if aport:
|
return pmb.parse.apkbuild(pmaport / "APKBUILD")
|
||||||
return pmb.parse.apkbuild(aport / "APKBUILD")
|
|
||||||
elif must_exist:
|
|
||||||
raise RuntimeError("Could not find APKBUILD for package:"
|
|
||||||
f" {pkgname}")
|
|
||||||
else:
|
|
||||||
path = _find_apkbuilds().get(pkgname)
|
|
||||||
if path:
|
|
||||||
return pmb.parse.apkbuild(path)
|
|
||||||
if must_exist:
|
|
||||||
raise RuntimeError("Could not find APKBUILD for package:"
|
|
||||||
f" {pkgname}")
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue