treewide: adopt pathlib.Path and type hinting (MR 2252)

With the new chroot type, we can now write fancy paths in the pythonic
way. Convert most of the codebase over, as well as adding various other
type hints.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-04-04 06:14:14 +02:00 committed by Oliver Smith
parent 00383bf354
commit 31cc898dd5
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
64 changed files with 513 additions and 385 deletions

View file

@ -7,16 +7,17 @@ import time
from pmb.core.types import PmbArgs
import pmb.helpers.run
import pmb.helpers.pmaports
def replace(path, old, new):
def replace(path: Path, old: str, new: str):
text = ""
with open(path, "r", encoding="utf-8") as handle:
with path.open("r", encoding="utf-8") as handle:
text = handle.read()
text = text.replace(old, new)
with open(path, "w", encoding="utf-8") as handle:
with path.open("w", encoding="utf-8") as handle:
handle.write(text)
@ -29,7 +30,7 @@ def replace_apkbuild(args: PmbArgs, pkgname, key, new, in_quotes=False):
:param in_quotes: expect the value to be in quotation marks ("")
"""
# Read old value
path = pmb.helpers.pmaports.find(args, pkgname) + "/APKBUILD"
path = pmb.helpers.pmaports.find(args, pkgname) / "APKBUILD"
apkbuild = pmb.parse.apkbuild(path)
old = apkbuild[key]
@ -95,7 +96,7 @@ def symlink(args: PmbArgs, file: Path, link: Path):
if (os.path.islink(link) and
os.path.realpath(os.readlink(link)) == os.path.realpath(file)):
return
raise RuntimeError("File exists: " + link)
raise RuntimeError(f"File exists: {link}")
elif link.is_symlink():
link.unlink()