]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
tighten types on functions that have no or very few str callers
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Wed, 25 Jan 2023 16:08:51 +0000 (17:08 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 25 Jan 2023 20:32:06 +0000 (21:32 +0100)
mkosi/__init__.py
mkosi/backend.py
mkosi/install.py
mkosi/remove.py

index 33eb2e994c38b96a2c9f67e638885712fc9d791b..bd0a72a29f9dc0a59265b8a566e29d67510e70e7 100644 (file)
@@ -715,7 +715,7 @@ def install_extra_trees(state: MkosiState) -> None:
 
 
 @contextlib.contextmanager
-def chdir(directory: PathString) -> Iterator[Path]:
+def chdir(directory: Path) -> Iterator[Path]:
     c = os.getcwd()
     os.chdir(directory)
     try:
@@ -2461,7 +2461,7 @@ def parse_bytes(num_bytes: Optional[str], *, sector_size: int = 512) -> int:
     return result
 
 
-def remove_glob(*patterns: PathString) -> None:
+def remove_glob(*patterns: Path) -> None:
     pathgen = (glob.glob(str(pattern)) for pattern in patterns)
     paths: set[str] = set(sum(pathgen, []))  # uniquify
     for path in paths:
@@ -2483,8 +2483,8 @@ def unlink_output(config: MkosiConfig) -> None:
                 for p in config.output.parent.iterdir():
                     if p.name.startswith(config.output.name) and "cache" not in p.name:
                         unlink_try_hard(p)
-            unlink_try_hard(f"{config.output}.manifest")
-            unlink_try_hard(f"{config.output}.changelog")
+            unlink_try_hard(Path(f"{config.output}.manifest"))
+            unlink_try_hard(Path(f"{config.output}.changelog"))
 
             if config.checksum:
                 unlink_try_hard(config.output_checksum)
@@ -2625,7 +2625,7 @@ def find_cache(args: argparse.Namespace) -> None:
         return
 
 
-def require_private_file(name: PathString, description: str) -> None:
+def require_private_file(name: Path, description: str) -> None:
     mode = os.stat(name).st_mode & 0o777
     if mode & 0o007:
         warn(dedent(f"""\
@@ -2652,10 +2652,10 @@ def find_password(args: argparse.Namespace) -> None:
         return
 
     try:
-        require_private_file("mkosi.rootpw", "root password")
+        pwfile = Path("mkosi.rootpw")
+        require_private_file(pwfile, "root password")
 
-        with open("mkosi.rootpw") as f:
-            args.password = f.read().strip()
+        args.password = pwfile.read_text().strip()
 
     except FileNotFoundError:
         pass
@@ -3307,7 +3307,7 @@ def configure_ssh(state: MkosiState, cached: bool) -> None:
 
     authorized_keys = state.root / "root/.ssh/authorized_keys"
     if state.config.ssh_key:
-        copy_path(f"{state.config.ssh_key}.pub", authorized_keys)
+        copy_path(Path(f"{state.config.ssh_key}.pub"), authorized_keys)
     elif state.config.ssh_agent is not None:
         env = {"SSH_AUTH_SOCK": state.config.ssh_agent}
         result = run(["ssh-add", "-L"], env=env, text=True, stdout=subprocess.PIPE)
@@ -3648,16 +3648,17 @@ def build_stuff(config: MkosiConfig) -> None:
     make_output_dir(config)
     make_cache_dir(config)
     workspace = setup_workspace(config)
-    cache = setup_package_cache(config, Path(workspace.name))
+    workspace_dir = Path(workspace.name)
+    cache = setup_package_cache(config, workspace_dir)
 
     manifest = Manifest(config)
 
     # Make sure tmpfiles' aging doesn't interfere with our workspace
     # while we are working on it.
-    with flock(workspace.name):
+    with flock(workspace_dir):
         state = MkosiState(
             config=config,
-            workspace=Path(workspace.name),
+            workspace=workspace_dir,
             cache=cache,
             do_run_build_script=False,
             machine_id=config.machine_id or uuid.uuid4().hex,
@@ -4045,7 +4046,7 @@ def run_qemu(config: MkosiConfig) -> None:
     with contextlib.ExitStack() as stack:
         if fw_supports_sb:
             ovmf_vars = stack.enter_context(tempfile.NamedTemporaryFile(prefix=".mkosi-", dir=tmp_dir()))
-            copy_path(find_ovmf_vars(config), ovmf_vars.name)
+            copy_path(find_ovmf_vars(config), Path(ovmf_vars.name))
             cmdline += [
                 "-global",
                 "ICH9-LPC.disable_s3=1",
index 74e73a8c15e2bbfaa2b3ecdd9b7812aeb39f4ef5..71174a0cb5f3c9a7933f66c8ccae609e7de16162 100644 (file)
@@ -709,10 +709,8 @@ def patch_file(filepath: Path, line_rewriter: Callable[[str], str]) -> None:
     shutil.move(temp_new_filepath, filepath)
 
 
-def path_relative_to_cwd(path: PathString) -> Path:
+def path_relative_to_cwd(path: Path) -> Path:
     "Return path as relative to $PWD if underneath, absolute path otherwise"
-    path = Path(path)
-
     try:
         return path.relative_to(os.getcwd())
     except ValueError:
@@ -785,7 +783,7 @@ class MkosiPrinter:
             cls.print_step(text2.format(*args))
 
 
-def chown_to_running_user(path: PathString) -> None:
+def chown_to_running_user(path: Path) -> None:
     uid = int(os.getenv("SUDO_UID") or os.getenv("PKEXEC_UID") or str(os.getuid()))
     user = pwd.getpwuid(uid).pw_name
     gid = pwd.getpwuid(uid).pw_gid
index 05fdef05831cc0478dc0083ff4271e55b5aa6844..ed0977891e7790f4e21a99b9932fe130317e9eb5 100644 (file)
@@ -11,7 +11,7 @@ from pathlib import Path
 from textwrap import dedent
 from typing import Optional
 
-from mkosi.backend import MkosiState, PathString, complete_step, run
+from mkosi.backend import MkosiState, complete_step, run
 
 
 def make_executable(path: Path) -> None:
@@ -47,7 +47,7 @@ def add_dropin_config_from_resource(
 
 
 @contextlib.contextmanager
-def flock(path: PathString) -> Iterator[Path]:
+def flock(path: Path) -> Iterator[Path]:
     fd = os.open(path, os.O_CLOEXEC|os.O_DIRECTORY|os.O_RDONLY)
     try:
         fcntl.fcntl(fd, fcntl.FD_CLOEXEC)
@@ -57,7 +57,7 @@ def flock(path: PathString) -> Iterator[Path]:
         os.close(fd)
 
 
-def copy_path(src: PathString, dst: PathString, parents: bool = False) -> None:
+def copy_path(src: Path, dst: Path, parents: bool = False) -> None:
     run(["cp", "--archive", "--no-target-directory", "--reflink=auto", src, dst])
 
 
index bb745345989618e56ce71661098c74d86eb9f38c..29ed078d5ef837f6e72f4d35e3191fe5991d94a6 100644 (file)
@@ -6,7 +6,7 @@ import subprocess
 from pathlib import Path
 from typing import Optional
 
-from mkosi.backend import PathString, run
+from mkosi.backend import run
 
 
 def btrfs_subvol_delete(path: Path) -> None:
@@ -30,7 +30,7 @@ def btrfs_subvol_delete(path: Path) -> None:
         stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
 
 
-def unlink_try_hard(path: Optional[PathString]) -> None:
+def unlink_try_hard(path: Optional[Path]) -> None:
     if path is None:
         return