2024-09-13 15:53:20 +02:00
|
|
|
"""Operations involving FileSystems."""
|
|
|
|
|
2023-10-10 22:11:57 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2024-09-13 17:19:33 +02:00
|
|
|
from pathlib import Path
|
2023-10-10 22:11:57 +02:00
|
|
|
|
2024-03-03 21:38:04 +01:00
|
|
|
from rwx import ps
|
2023-10-10 22:11:57 +02:00
|
|
|
|
2024-06-10 09:51:59 +02:00
|
|
|
CHARSET = "UTF-8"
|
2023-10-10 22:11:57 +02:00
|
|
|
|
|
|
|
|
2024-09-17 23:17:36 +02:00
|
|
|
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
|
|
|
|
"""
|
2023-10-10 22:11:57 +02:00
|
|
|
ps.run(
|
2024-06-10 09:51:59 +02:00
|
|
|
("qemu-img", "create"),
|
|
|
|
("-f", "qcow2"),
|
2024-09-17 23:17:36 +02:00
|
|
|
(str(file_path), str(size_bytes)),
|
2023-10-10 22:11:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-13 23:22:12 +02:00
|
|
|
def empty_file(path: Path) -> None:
|
2024-09-17 23:07:41 +02:00
|
|
|
"""Empty the file at provided path.
|
|
|
|
|
|
|
|
:param path: target file to empty
|
|
|
|
:type path: Path
|
|
|
|
"""
|
2024-06-10 10:01:24 +02:00
|
|
|
write(path, "")
|
2023-10-10 22:11:57 +02:00
|
|
|
|
|
|
|
|
2024-09-13 15:53:20 +02:00
|
|
|
def get_mount_uuid(path: str) -> str:
|
2024-09-13 17:07:27 +02:00
|
|
|
"""Return the filesystem UUID of a mountpoint path."""
|
2023-10-10 22:11:57 +02:00
|
|
|
return ps.run_line(
|
2024-06-10 09:51:59 +02:00
|
|
|
("findmnt",),
|
|
|
|
("--noheadings",),
|
|
|
|
("--output", "UUID"),
|
2024-02-16 21:55:09 +01:00
|
|
|
(path,),
|
2023-10-10 22:11:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-13 17:07:27 +02:00
|
|
|
def get_path_mount(path: str) -> str:
|
|
|
|
"""Return the mountpoint path of an arbitrary path."""
|
2023-10-10 22:11:57 +02:00
|
|
|
return ps.run_line(
|
2024-09-13 23:55:02 +02:00
|
|
|
"stat",
|
2024-06-10 09:51:59 +02:00
|
|
|
("--format", "%m"),
|
2024-09-13 23:55:02 +02:00
|
|
|
path,
|
2023-10-10 22:11:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-13 17:07:27 +02:00
|
|
|
def get_path_uuid(path: str) -> str:
|
|
|
|
"""Return the filesystem UUID of an arbitrary path."""
|
2023-10-10 22:11:57 +02:00
|
|
|
return get_mount_uuid(get_path_mount(path))
|
|
|
|
|
|
|
|
|
2024-09-13 23:22:12 +02:00
|
|
|
def make_directory(path: Path) -> None:
|
2024-09-17 23:19:29 +02:00
|
|
|
"""Make a directory (and its parents) from a path.
|
|
|
|
|
|
|
|
:param path: directory to create
|
|
|
|
:type path: Path
|
|
|
|
"""
|
2024-09-13 23:22:12 +02:00
|
|
|
path.mkdir(exist_ok=True, parents=True)
|
2023-10-10 22:11:57 +02:00
|
|
|
|
|
|
|
|
2024-09-14 02:47:52 +02:00
|
|
|
def read_file_bytes(file_path: Path) -> bytes:
|
2024-09-13 18:55:31 +02:00
|
|
|
"""Read whole file bytes."""
|
2024-09-14 02:47:52 +02:00
|
|
|
with file_path.open("br") as file_object:
|
2023-10-10 22:11:57 +02:00
|
|
|
return file_object.read()
|
|
|
|
|
|
|
|
|
2024-09-14 02:47:52 +02:00
|
|
|
def read_file_lines(file_path: Path, charset: str = CHARSET) -> list[str]:
|
2024-09-13 18:58:57 +02:00
|
|
|
"""Read whole file lines."""
|
|
|
|
return read_file_text(file_path, charset).split(os.linesep)
|
2023-10-10 22:11:57 +02:00
|
|
|
|
|
|
|
|
2024-09-14 02:47:52 +02:00
|
|
|
def read_file_text(file_path: Path, charset: str = CHARSET) -> str:
|
2024-09-13 18:57:49 +02:00
|
|
|
"""Read whole file text."""
|
2024-09-13 18:55:31 +02:00
|
|
|
return read_file_bytes(file_path).decode(charset)
|
2023-10-10 22:11:57 +02:00
|
|
|
|
|
|
|
|
2024-09-13 23:17:09 +02:00
|
|
|
def wipe(path: Path) -> None:
|
2024-09-17 22:42:19 +02:00
|
|
|
"""Wipe provided path, whether directory or file.
|
|
|
|
|
|
|
|
:param path: target path
|
|
|
|
:type path: Path
|
|
|
|
"""
|
2023-10-10 22:11:57 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
except NotADirectoryError:
|
2024-09-13 23:17:09 +02:00
|
|
|
path.unlink(missing_ok=True)
|
2023-10-10 22:11:57 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-09-13 23:22:12 +02:00
|
|
|
def write(file_path: Path, text: str, charset: str = CHARSET) -> None:
|
2024-09-17 23:05:00 +02:00
|
|
|
"""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
|
|
|
|
"""
|
2024-09-13 23:22:12 +02:00
|
|
|
with file_path.open(encoding=charset, mode="w") as file_object:
|
2024-09-13 17:22:36 +02:00
|
|
|
file_object.write(text)
|