rwx/rwx/fs/__init__.py

83 lines
2 KiB
Python
Raw Normal View History

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-13 15:53:20 +02:00
def create_image(file_path: str, size_bytes: int) -> None:
"""Create a virtual device image file."""
2023-10-10 22:11:57 +02:00
ps.run(
2024-06-10 09:51:59 +02:00
("qemu-img", "create"),
("-f", "qcow2"),
2023-10-10 22:11:57 +02:00
(file_path, size_bytes),
)
2024-09-13 15:53:20 +02:00
def empty_file(path: str) -> None:
"""Empty the file at provided 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-06-10 09:51:59 +02:00
("stat",),
("--format", "%m"),
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_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 17:07:27 +02:00
def make_directory(path: str) -> None:
"""Make a directory (and its parents) from a path."""
2024-09-13 17:24:37 +02:00
Path(path).mkdir(exist_ok=True, parents=True)
2023-10-10 22:11:57 +02:00
2024-09-13 18:55:31 +02:00
def read_file_bytes(file_path: str) -> bytes:
"""Read whole file bytes."""
with Path(file_path).open("br") as file_object:
2023-10-10 22:11:57 +02:00
return file_object.read()
2024-09-13 17:13:37 +02:00
def read_file_lines(file_path: str, charset: str = CHARSET):
2023-10-10 22:11:57 +02:00
return read_file_text(file_path).split(os.linesep)
2024-09-13 17:13:37 +02:00
def read_file_text(file_path: str, charset: str = CHARSET):
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 17:13:16 +02:00
def wipe(path: str) -> None:
"""Wipe provided path, whether directory or file."""
2023-10-10 22:11:57 +02:00
try:
shutil.rmtree(path)
except NotADirectoryError:
2024-09-13 17:19:33 +02:00
Path(path).unlink(missing_ok=True)
2023-10-10 22:11:57 +02:00
except FileNotFoundError:
pass
2024-09-13 17:13:37 +02:00
def write(file_path: str, text: str, charset: str = CHARSET) -> None:
2024-09-13 17:13:16 +02:00
"""Write text into a file."""
2024-09-13 17:24:37 +02:00
with Path(file_path).open(encoding=charset, mode="w") as file_object:
2024-09-13 17:22:36 +02:00
file_object.write(text)