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

@ -4,7 +4,7 @@
from __future__ import annotations
import enum
from typing import Generator, Optional
from pathlib import Path, PosixPath
from pathlib import Path, PosixPath, PurePosixPath
import pmb.config
class ChrootType(enum.Enum):
@ -86,7 +86,7 @@ class Chroot:
def __truediv__(self, other: object) -> Path:
if isinstance(other, PosixPath):
if isinstance(other, PosixPath) or isinstance(other, PurePosixPath):
# Convert the other path to a relative path
# FIXME: we should avoid creating absolute paths that we actually want
# to make relative to the chroot...
@ -99,8 +99,8 @@ class Chroot:
def __rtruediv__(self, other: object) -> Path:
if isinstance(other, PosixPath):
return other / self.path
if isinstance(other, PosixPath) or isinstance(other, PurePosixPath):
return Path(other) / self.path
if isinstance(other, str):
return other / self.path
@ -120,6 +120,11 @@ class Chroot:
return Chroot(ChrootType.NATIVE)
@staticmethod
def buildroot(arch: str) -> Chroot:
return Chroot(ChrootType.BUILDROOT, arch)
@staticmethod
def rootfs(device: str) -> Chroot:
return Chroot(ChrootType.ROOTFS, device)