diff --git a/rwx/__main__.py b/rwx/__main__.py index f6276e4..e19dea1 100755 --- a/rwx/__main__.py +++ b/rwx/__main__.py @@ -4,12 +4,12 @@ from pathlib import Path -import fs +from rwx import fs if __name__ == "__main__": - file_path = Path(__file__).resolve() - root_path = file_path.parent - directory_path = root_path / "tmp" + file_path: Path = Path(__file__).resolve() + root_path: Path = file_path.parent + directory_path: Path = root_path / "tmp" file_path = directory_path / "file" fs.wipe(directory_path) diff --git a/rwx/cmd/__init__.py b/rwx/cmd/__init__.py index e3b75bc..ca4dced 100644 --- a/rwx/cmd/__init__.py +++ b/rwx/cmd/__init__.py @@ -6,6 +6,7 @@ packages: list[str] = [] def need(command: str) -> None: """Assert package dependency for a command.""" + package: str | None match command: case "debootstrap": package = "debootstrap" diff --git a/rwx/cmd/squashfs/__init__.py b/rwx/cmd/squashfs/__init__.py index 85e20b2..db603da 100644 --- a/rwx/cmd/squashfs/__init__.py +++ b/rwx/cmd/squashfs/__init__.py @@ -1,22 +1,18 @@ """Wrap SquashFS commands.""" -import ps +from rwx import cmd, ps -import rwx.cmd - -rwx.cmd.need("mksquashfs") +cmd.need("mksquashfs") def mksquashfs(input_root: str, output_file: str) -> None: """Make a SquashFS bootable image file.""" ps.run( - [ - "mksquashfs", - input_root, - output_file, - "-comp", - "zstd", - "-Xcompression-level", - str(18), - ] + "mksquashfs", + input_root, + output_file, + "-comp", + "zstd", + "-Xcompression-level", + str(18), ) diff --git a/rwx/deb/__init__.py b/rwx/deb/__init__.py index f022ca5..fcd4198 100644 --- a/rwx/deb/__init__.py +++ b/rwx/deb/__init__.py @@ -1,8 +1,6 @@ """Wrap Debian commands.""" -import cmd - -import ps +from rwx import cmd, ps cmd.need("debootstrap") @@ -20,4 +18,4 @@ def bootstrap(root_path: str, suite: str, mirror_location: str) -> None: (root_path,), (mirror_location,), ] - return ps.run(command) + ps.run(*command) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index a8865e7..0685c44 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -14,11 +14,11 @@ def create_image(file_path: str, size_bytes: int) -> None: ps.run( ("qemu-img", "create"), ("-f", "qcow2"), - (file_path, size_bytes), + (file_path, str(size_bytes)), ) -def empty_file(path: str) -> None: +def empty_file(path: Path) -> None: """Empty the file at provided path.""" write(path, "") @@ -36,9 +36,9 @@ def get_mount_uuid(path: str) -> str: def get_path_mount(path: str) -> str: """Return the mountpoint path of an arbitrary path.""" return ps.run_line( - ("stat",), + "stat", ("--format", "%m"), - (path,), + path, ) @@ -47,9 +47,9 @@ def get_path_uuid(path: str) -> str: return get_mount_uuid(get_path_mount(path)) -def make_directory(path: str) -> None: +def make_directory(path: Path) -> None: """Make a directory (and its parents) from a path.""" - Path(path).mkdir(exist_ok=True, parents=True) + path.mkdir(exist_ok=True, parents=True) def read_file_bytes(file_path: str) -> bytes: @@ -68,17 +68,17 @@ def read_file_text(file_path: str, charset: str = CHARSET) -> str: return read_file_bytes(file_path).decode(charset) -def wipe(path: str) -> None: +def wipe(path: Path) -> None: """Wipe provided path, whether directory or file.""" try: shutil.rmtree(path) except NotADirectoryError: - Path(path).unlink(missing_ok=True) + path.unlink(missing_ok=True) except FileNotFoundError: pass -def write(file_path: str, text: str, charset: str = CHARSET) -> None: +def write(file_path: Path, text: str, charset: str = CHARSET) -> None: """Write text into a file.""" - with Path(file_path).open(encoding=charset, mode="w") as file_object: + with file_path.open(encoding=charset, mode="w") as file_object: file_object.write(text) diff --git a/rwx/grub/__init__.py b/rwx/grub/__init__.py index dde847a..accec9f 100644 --- a/rwx/grub/__init__.py +++ b/rwx/grub/__init__.py @@ -1,8 +1,6 @@ """Wrap GRUB commands.""" -import cmd - -import ps +from rwx import cmd, ps cmd.need("grub-mkimage") diff --git a/rwx/os/__init__.py b/rwx/os/__init__.py new file mode 100644 index 0000000..70ca05c --- /dev/null +++ b/rwx/os/__init__.py @@ -0,0 +1,11 @@ +"""Control Operating Systems.""" + +from pathlib import Path + + +class OS: + """Operating System.""" + + def __init__(self, path: str) -> None: + """Set root.""" + self.root = Path(path) diff --git a/rwx/prj/__init__.py b/rwx/prj/__init__.py index 8b70a4a..36dab99 100644 --- a/rwx/prj/__init__.py +++ b/rwx/prj/__init__.py @@ -1,6 +1,5 @@ """Handle projects.""" -from os.path import realpath from pathlib import Path @@ -9,6 +8,6 @@ class Project: def __init__(self, file_path: str) -> None: """Set file, root & name.""" - self.file: str = realpath(file_path) - self.root: str = Path(self.file).parent + self.file: Path = Path(file_path).resolve() + self.root: Path = self.file.parent self.name: str = self.root.name diff --git a/rwx/prj/sphinx.py b/rwx/prj/sphinx.py index b85cfa5..be207ef 100644 --- a/rwx/prj/sphinx.py +++ b/rwx/prj/sphinx.py @@ -1,5 +1,10 @@ """Project consisting only of a Sphinx documentation.""" +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from pathlib import Path + from sphinx.cmd.build import build_main from rwx.fs import wipe @@ -15,7 +20,7 @@ class SphinxProject(Project): def build(self) -> None: """Build the project.""" - output_root: str = self.root / "out" + output_root: Path = self.root / "out" wipe(output_root) arguments: list[str] = [ "-E", @@ -30,9 +35,9 @@ class SphinxProject(Project): "-D", "html_theme={}".format("sphinx_rtd_theme"), "-c", - self.root, + str(self.root), # "-C", - self.root / self.name, - output_root / "web", + str(self.root / self.name), + str(output_root / "web"), ] build_main(arguments) diff --git a/rwx/ps/__init__.py b/rwx/ps/__init__.py index e45672e..e18e250 100644 --- a/rwx/ps/__init__.py +++ b/rwx/ps/__init__.py @@ -5,7 +5,7 @@ import subprocess from rwx import txt -def get_tuples_args(*items: str | tuple[str]) -> list[str]: +def get_tuples_args(*items: str | tuple[str, ...]) -> list[str]: """Turn arguments tuples into an arguments list.""" args: list[str] = [] for item in items: @@ -16,21 +16,21 @@ def get_tuples_args(*items: str | tuple[str]) -> list[str]: return args -def run(*items: str | tuple[str]) -> subprocess.CompletedProcess: +def run(*items: str | tuple[str, ...]) -> subprocess.CompletedProcess: """Run from a list of arguments tuples.""" return subprocess.run( get_tuples_args(*items), capture_output=False, check=True ) -def run_line(*items: str | tuple[str], charset: str = txt.CHARSET) -> str: +def run_line(*items: str | tuple[str, ...], charset: str = txt.CHARSET) -> str: """Run and return output line.""" line, *_ = run_lines(*items, charset=charset) return line def run_lines( - *items: str | tuple[str], charset: str = txt.CHARSET + *items: str | tuple[str, ...], charset: str = txt.CHARSET ) -> list[str]: """Run and return output lines.""" process = subprocess.run(