Compare commits

..

No commits in common. "2327d5388dc93f1e5d6255dcfde567ef523c0ba2" and "012b10f7d56d12b7125798cbed00e7f28b8ef61f" have entirely different histories.

10 changed files with 50 additions and 196 deletions

View file

@ -5,11 +5,7 @@ packages: list[str] = []
def need(command: str) -> None:
"""Assert package dependency for a command.
:param command: name of the requested command
:type command: str
"""
"""Assert package dependency for a command."""
package: str | None
match command:
case "debootstrap":

View file

@ -1,24 +1,16 @@
"""Wrap SquashFS commands."""
from pathlib import Path
from rwx import cmd, ps
cmd.need("mksquashfs")
def mksquashfs(input_root: Path, output_file: Path) -> None:
"""Make a SquashFS bootable image file.
:param input_root: ?
:type input_root: Path
:param output_file: ?
:type output_file: Path
"""
def mksquashfs(input_root: str, output_file: str) -> None:
"""Make a SquashFS bootable image file."""
ps.run(
"mksquashfs",
str(input_root),
str(output_file),
input_root,
output_file,
"-comp",
"zstd",
"-Xcompression-level",

View file

@ -1,7 +1,5 @@
"""Wrap Debian commands."""
from pathlib import Path
from rwx import cmd, ps
cmd.need("debootstrap")
@ -10,22 +8,14 @@ BOOTSTRAP_ARCHITECTURE = "amd64"
BOOTSTRAP_VARIANT = "minbase"
def bootstrap(root_path: Path, suite: str, mirror_location: str) -> None:
"""Boostrap a base operating filesystem.
:param root_path: target output path
:type root_path: Path
:param suite: target distribution name
:type suite: str
:param mirror_location: source input repository
:type mirror_location: str
"""
command = (
"debootstrap",
def bootstrap(root_path: str, suite: str, mirror_location: str) -> None:
"""Boostrap a base operating filesystem."""
command = [
("debootstrap",),
("--arch", BOOTSTRAP_ARCHITECTURE),
("--variant", BOOTSTRAP_VARIANT),
suite,
str(root_path),
mirror_location,
)
(suite,),
(root_path,),
(mirror_location,),
]
ps.run(*command)

View file

@ -9,121 +9,67 @@ 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
"""
def create_image(file_path: str, size_bytes: int) -> None:
"""Create a virtual device image file."""
ps.run(
("qemu-img", "create"),
("-f", "qcow2"),
(str(file_path), str(size_bytes)),
(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
"""
"""Empty the file at provided 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
"""
def get_mount_uuid(path: str) -> str:
"""Return the filesystem UUID of a mountpoint path."""
return ps.run_line(
"findmnt",
"--noheadings",
("findmnt",),
("--noheadings",),
("--output", "UUID"),
str(path),
(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_mount(path: str) -> str:
"""Return the mountpoint path of an arbitrary path."""
return ps.run_line(
"stat",
("--format", "%m"),
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
"""
def get_path_uuid(path: str) -> str:
"""Return the filesystem UUID of an arbitrary path."""
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
"""
"""Make a directory (and its parents) from a 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
"""
"""Read whole file bytes."""
with file_path.open("br") as file_object:
return file_object.read()
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]
"""
"""Read whole file lines."""
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
"""
"""Read whole file text."""
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
"""
"""Wipe provided path, whether directory or file."""
try:
shutil.rmtree(path)
except NotADirectoryError:
@ -133,14 +79,6 @@ def wipe(path: Path) -> None:
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
"""
"""Write text into a file."""
with file_path.open(encoding=charset, mode="w") as file_object:
file_object.write(text)

View file

@ -5,12 +5,7 @@ import sys
def get_file_logger(name: str) -> logging.Logger:
"""Return a file logger.
:param name: arbitrary name
:type name: str
:rtype: logging.Logger
"""
"""Return a file logger."""
# formatter
items = [
"%(name)s: %(asctime)s",
@ -32,12 +27,7 @@ def get_file_logger(name: str) -> logging.Logger:
def get_stream_logger(level: int) -> logging.Logger:
"""Return a stream logger.
:param level: filtering level
:type level: int
:rtype: logging.Logger
"""
"""Return a stream logger."""
# handler
out_handler = logging.StreamHandler(stream=sys.stdout)
out_handler.setLevel(level)

View file

@ -8,12 +8,7 @@ from .debian import Debian
def from_path(path: Path) -> OS:
"""Initialize from an already existing path.
:param path: source root directory
:type path: Path
:rtype: OS
"""
"""Initialize from an already existing path."""
return Debian(path)

View file

@ -10,17 +10,10 @@ class OS(Class, ABC):
"""Operating System."""
def __init__(self, path: Path) -> None:
"""Set root.
:param path: root directory
:type path: Path
"""
"""Set root."""
self.root = path
self.name = self.get_name()
@abstractmethod
def get_name(self) -> str:
"""Return mandatory name.
:rtype: str
"""
"""Return mandatory name."""

View file

@ -16,14 +16,8 @@ class PM(Class, ABC):
@abstractmethod
def get_clean_command(self) -> Command:
"""Command to clean packages cache.
:rtype: Command
"""
"""Command to clean packages cache."""
@abstractmethod
def get_install_command(self) -> Command:
"""Command to install package(s).
:rtype: Command
"""
"""Command to install package(s)."""

View file

@ -8,15 +8,9 @@ class APT(PM):
"""Advanced Package Tool."""
def get_clean_command(self) -> Command:
"""Return clean command.
:rtype: Command
"""
"""Return clean command."""
return Command()
def get_install_command(self) -> Command:
"""Return install command.
:rtype: Command
"""
"""Return install command."""
return Command()

View file

@ -9,22 +9,13 @@ class Command(Class):
"""Command to run."""
def __init__(self, *arguments: str | tuple[str, ...]) -> None:
"""Set raw & flat arguments.
:param *arguments: single argument or grouped ones
:type *arguments: str | tuple[str, ...]
"""
"""Set raw & flat arguments."""
self.raw = arguments
self.flat: list[str] = []
def get_tuples_args(*items: str | tuple[str, ...]) -> list[str]:
"""Turn arguments tuples into an arguments list.
:param *items: single item or grouped ones
:type *items: str | tuple[str, ...]
:rtype: list[str]
"""
"""Turn arguments tuples into an arguments list."""
args: list[str] = []
for item in items:
match item:
@ -36,26 +27,14 @@ def get_tuples_args(*items: str | tuple[str, ...]) -> list[str]:
def run(*items: str | tuple[str, ...]) -> subprocess.CompletedProcess:
"""Run from a list of arguments tuples.
:param *items: single item or grouped ones
:type *items: str | tuple[str, ...]
:rtype: 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:
"""Run and return output line.
:param *items: single item or grouped ones
:type *items: str | tuple[str, ...]
:param charset: charset to use for decoding binary output
:type charset: str
:rtype: str
"""
"""Run and return output line."""
line, *_ = run_lines(*items, charset=charset)
return line
@ -63,14 +42,7 @@ def run_line(*items: str | tuple[str, ...], charset: str = txt.CHARSET) -> str:
def run_lines(
*items: str | tuple[str, ...], charset: str = txt.CHARSET
) -> list[str]:
"""Run and return output lines.
:param *items: single item or grouped ones
:type *items: str | tuple[str, ...]
:param charset: charset to use for decoding binary output
:type charset: str
:rtype: list[str]
"""
"""Run and return output lines."""
process = subprocess.run(
get_tuples_args(*items), capture_output=True, check=True
)