From: Joerg Behrmann Date: Wed, 25 Jan 2023 16:08:51 +0000 (+0100) Subject: tighten types on functions that have no or very few str callers X-Git-Tag: v15~351 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb0f950a6a3f1ee2b2a8a8758e381593b4d2802a;p=thirdparty%2Fmkosi.git tighten types on functions that have no or very few str callers --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 33eb2e994..bd0a72a29 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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", diff --git a/mkosi/backend.py b/mkosi/backend.py index 74e73a8c1..71174a0cb 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -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 diff --git a/mkosi/install.py b/mkosi/install.py index 05fdef058..ed0977891 100644 --- a/mkosi/install.py +++ b/mkosi/install.py @@ -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]) diff --git a/mkosi/remove.py b/mkosi/remove.py index bb7453459..29ed078d5 100644 --- a/mkosi/remove.py +++ b/mkosi/remove.py @@ -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