rwx/rwx/fs/__init__.py
Marc Beninca 27329865ee
All checks were successful
/ job (push) Successful in 1m10s
fs/wipe
2024-10-26 10:57:20 +02:00

158 lines
3.6 KiB
Python

"""Operations involving FileSystems."""
import os
import shutil
import tomllib
from pathlib import Path
from rwx import ps
CHARSET = "UTF-8"
def create_image(file_path: Path, size_bytes: int) -> None:
"""Create a virtual device image file.
:param file_path: target image file
:type file_path: Path
:param size_bytes: virtual volume
:type size_bytes: int
"""
ps.run(
("qemu-img", "create"),
("-f", "qcow2"),
(str(file_path), str(size_bytes)),
)
def empty_file(path: Path) -> None:
"""Empty the file at provided path.
:param path: target file to empty
:type path: Path
"""
write(path, "")
def get_mount_uuid(path: Path) -> str:
"""Return the filesystem UUID of a mountpoint path.
:param path: mountpoint path
:type path: Path
:rtype: str
"""
return ps.run_line(
"findmnt",
"--noheadings",
("--output", "UUID"),
str(path),
)
def get_path_mount(path: Path) -> Path:
"""Return the mountpoint path of an arbitrary path.
:param path: arbitrary path
:type path: Path
:rtype: Path
"""
return Path(
ps.run_line(
"stat",
("--format", "%m"),
str(path),
)
)
def get_path_uuid(path: Path) -> str:
"""Return the filesystem UUID of an arbitrary path.
:param path: arbitrary path
:type path: Path
:rtype: str
"""
return get_mount_uuid(get_path_mount(path))
def make_directory(path: Path) -> None:
"""Make a directory (and its parents) from a path.
:param path: directory to create
:type path: Path
"""
path.mkdir(exist_ok=True, parents=True)
def read_file_bytes(file_path: Path) -> bytes:
"""Read whole file bytes.
:param file_path: source input file
:type file_path: Path
:rtype: bytes
"""
with file_path.open("br") as file_object:
return file_object.read()
def read_file_dict(file_path: Path, charset: str = CHARSET) -> dict:
"""Read whole file as toml object.
:param file_path: source input file
:type file_path: Path
:param charset: charset to use for decoding input
:type charset: str
:rtype: dict
"""
text = read_file_text(file_path, charset)
return tomllib.loads(text)
def read_file_lines(file_path: Path, charset: str = CHARSET) -> list[str]:
"""Read whole file lines.
:param file_path: source input file
:type file_path: Path
:param charset: charset to use for decoding input
:type charset: str
:rtype: list[str]
"""
return read_file_text(file_path, charset).split(os.linesep)
def read_file_text(file_path: Path, charset: str = CHARSET) -> str:
"""Read whole file text.
:param file_path: source input file
:type file_path: Path
:param charset: charset to use for decoding input
:type charset: str
:rtype: str
"""
return read_file_bytes(file_path).decode(charset)
def wipe(path: Path) -> None:
"""Wipe provided path, whether directory or file.
:param path: target path
:type path: Path
"""
try:
path.unlink(missing_ok=True)
except IsADirectoryError:
shutil.rmtree(path)
def write(file_path: Path, text: str, charset: str = CHARSET) -> None:
"""Write text into a file.
:param file_path: target file path
:type file_path: Path
:param text: content to write
:type text: str
:param charset: charset to use for encoding ouput
:type charset: str
"""
with file_path.open(encoding=charset, mode="w") as file_object:
file_object.write(text)