style!: adopt the core lint bar and migrate to it

The package carried no ruff configuration at all, so it ran on the defaults (E4+E7+E9+F) while infinito-nexus-core, its only consumer, holds itself to a far wider selection. Measured against that selection this tree had 224 findings. It now has none.

The selector list is core's verbatim so both repositories answer to one bar. target-version stays py39 rather than core's py311, because requires-python still declares >=3.9 and pyupgrade would otherwise propose syntax the declared minimum cannot run. Every ignore carries its reason: S603/S607 in particular, since running docker and dump binaries from PATH in list form is this tool's whole job and is already injection-safe.

Two conversions are judgement rather than mechanics. os.path.join(dir, '') was the rsync idiom for a trailing separator, which Path drops, so it becomes an explicit os.sep. os.path.abspath stays where Path.resolve() would follow symlinks and let a symlinked volume test as inside the snapshot subject.

BREAKING CHANGE: VersionMismatch is renamed VersionMismatchError.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-18 01:51:18 +02:00
parent 94637c32aa
commit efcfe88e7f
18 changed files with 157 additions and 91 deletions

View File

@@ -82,7 +82,7 @@ def handle_docker_compose_services(
continue
dir_path = entry.path
name = os.path.basename(dir_path)
name = Path(dir_path).name
print(f"Checking directory: {dir_path}", flush=True)

View File

@@ -14,6 +14,7 @@ from __future__ import annotations
import csv
import re
from pathlib import Path
from typing import NamedTuple
COLUMNS = ("instance", "database", "username", "password")
@@ -87,7 +88,7 @@ def read_rows(csv_path: str) -> list[Row]:
DatabasesCsvError: a row holds fewer columns than :data:`COLUMNS`.
"""
rows: list[Row] = []
with open(csv_path, newline="", encoding="utf-8") as handle:
with Path(csv_path).open(newline="", encoding="utf-8") as handle:
reader = csv.reader(handle, delimiter=DELIMITER)
next(reader, None)
for raw in reader:

View File

@@ -165,7 +165,7 @@ def main(argv: list[str] | None = None) -> int:
return 0
parser.error("Unhandled command")
return 2
return 2 # noqa: TRY300 - the try wraps the whole dispatch on purpose
except Exception as e: # noqa: BLE001 - CLI boundary: any failure becomes exit 1
print(f"ERROR: {e}", file=sys.stderr)

View File

@@ -19,16 +19,20 @@ the implementation:
from __future__ import annotations
import os
import re
import tempfile
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import TYPE_CHECKING
from baudolo.restore.run import docker_exec
from ..run import docker_exec
from .version import guard
if TYPE_CHECKING:
from collections.abc import Iterable, Iterator
CONTROL_DB = "postgres"
_CLUSTER_PRECLEAN_SQL = os.path.join(os.path.dirname(__file__), "cluster_preclean.sql")
_CLUSTER_PRECLEAN_SQL = Path(__file__).parent / "cluster_preclean.sql"
_CREATE_ROLE = re.compile(rb'^CREATE ROLE "?([^";]+)"?;\s*$')
_CREATE_DATABASE = re.compile(rb"^CREATE DATABASE\s+(.*)$")
_CREATE_ROLE_LINE = re.compile(rb"^CREATE ROLE\s+(.*)$")
@@ -92,7 +96,7 @@ def dump_inventory(sql_path: str) -> tuple[list[str], list[str]]:
"""
databases: list[str] = []
roles: list[str] = []
with open(sql_path, "rb") as handle:
with Path(sql_path).open("rb") as handle:
for raw in handle:
line = raw.decode("utf-8", "replace")
for pattern, sink, read in (
@@ -111,7 +115,7 @@ def dump_inventory(sql_path: str) -> tuple[list[str], list[str]]:
def preclean_sql() -> str:
"""The catalog-wide pre-clean, safe only behind the instance check."""
with open(_CLUSTER_PRECLEAN_SQL, encoding="utf-8") as preclean:
with _CLUSTER_PRECLEAN_SQL.open(encoding="utf-8") as preclean:
return preclean.read()
@@ -158,7 +162,7 @@ def assert_instance_matches_dump(
if foreign:
raise RuntimeError(
f"{container} also holds {', '.join(foreign)}, which "
f"{os.path.basename(sql_path)} does not carry. --empty wipes the "
f"{Path(sql_path).name} does not carry. --empty wipes the "
"instance, so those would be destroyed with nothing to restore "
"them from. Move them off this instance, or drop them yourself if "
"they are disposable."
@@ -216,7 +220,7 @@ def restore_cluster_sql(
check_version: refuse a dump from a newer major version than the
running engine before anything is dropped.
"""
if not os.path.isfile(sql_path):
if not Path(sql_path).is_file():
raise FileNotFoundError(sql_path)
if check_version:
@@ -239,10 +243,10 @@ def restore_cluster_sql(
docker_env=docker_env,
)
with open(sql_path, "rb") as src, tempfile.TemporaryFile() as filtered:
with Path(sql_path).open("rb") as src, tempfile.TemporaryFile() as filtered:
for line in filter_own_role_creation(src, user):
filtered.write(line)
filtered.seek(0)
docker_exec(container, _psql(user), stdin=filtered, docker_env=docker_env)
print(f"PostgreSQL cluster restore complete from '{os.path.basename(sql_path)}'.")
print(f"PostgreSQL cluster restore complete from '{Path(sql_path).name}'.")

View File

@@ -1,11 +1,14 @@
from __future__ import annotations
import os
import sys
from pathlib import Path
from baudolo.restore.run import docker_exec, docker_exec_sh
from ..run import docker_exec, docker_exec_sh
from .version import guard
_NO_CLIENT = "ERROR: neither 'mariadb' nor 'mysql' found in container."
def _pick_client(container: str) -> str:
"""
@@ -20,14 +23,13 @@ exit 42
"""
try:
out = docker_exec_sh(container, script, capture=True).stdout.decode().strip()
if not out:
raise RuntimeError("empty client detection output")
return out
except Exception:
print(
"ERROR: neither 'mariadb' nor 'mysql' found in container.", file=sys.stderr
)
print(_NO_CLIENT, file=sys.stderr)
raise
if not out:
print(_NO_CLIENT, file=sys.stderr)
raise RuntimeError("empty client detection output")
return out
def restore_mariadb_sql(
@@ -42,7 +44,7 @@ def restore_mariadb_sql(
) -> None:
client = _pick_client(container)
if not os.path.isfile(sql_path):
if not Path(sql_path).is_file():
raise FileNotFoundError(sql_path)
if check_version:
@@ -66,7 +68,7 @@ def restore_mariadb_sql(
f"--password={password}",
"-N",
"-e",
f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{db_name}';",
f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{db_name}';", # noqa: S608 - validate_database() constrains the name to ^[a-zA-Z0-9_][a-zA-Z0-9_-]*$
],
capture=True,
)
@@ -94,7 +96,7 @@ def restore_mariadb_sql(
],
)
with open(sql_path, "rb") as f:
with Path(sql_path).open("rb") as f:
docker_exec(
container, [client, "-u", user, f"--password={password}", db_name], stdin=f
)

View File

@@ -1,14 +1,18 @@
from __future__ import annotations
import os
import tempfile
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import TYPE_CHECKING
from baudolo.restore.run import docker_exec
from ..run import docker_exec
from .version import guard
if TYPE_CHECKING:
from collections.abc import Iterable, Iterator
_SUPERUSER_ONLY_PREFIXES = (b"COMMENT ON EXTENSION", b"ALTER DEFAULT PRIVILEGES")
_EMPTY_PRECLEAN_SQL = os.path.join(os.path.dirname(__file__), "empty_preclean.sql")
_EMPTY_PRECLEAN_SQL = Path(__file__).parent / "empty_preclean.sql"
def filter_superuser_only_lines(lines: Iterable[bytes]) -> Iterator[bytes]:
@@ -49,7 +53,7 @@ def restore_postgres_sql(
empty: bool,
check_version: bool = True,
) -> None:
if not os.path.isfile(sql_path):
if not Path(sql_path).is_file():
raise FileNotFoundError(sql_path)
if check_version:
@@ -64,7 +68,7 @@ def restore_postgres_sql(
docker_env = {"PGPASSWORD": password}
if empty:
with open(_EMPTY_PRECLEAN_SQL, encoding="utf-8") as preclean:
with _EMPTY_PRECLEAN_SQL.open(encoding="utf-8") as preclean:
drop_sql = preclean.read()
docker_exec(
container,
@@ -76,7 +80,7 @@ def restore_postgres_sql(
# Filter into a spooled temp file instead of building the whole dump in
# memory: production dumps reach many GB and the previous read/splitlines/
# join needed roughly three times the dump size in RSS.
with open(sql_path, "rb") as src, tempfile.TemporaryFile() as filtered:
with Path(sql_path).open("rb") as src, tempfile.TemporaryFile() as filtered:
for line in filter_superuser_only_lines(src):
filtered.write(line)
filtered.seek(0)

View File

@@ -24,8 +24,9 @@ with the cluster banner and the roles section, and the first
from __future__ import annotations
import re
from pathlib import Path
from ..run import docker_exec, stdout_of
from baudolo.restore.run import docker_exec, stdout_of
SCAN_LINES = 2000
DUMP_VERSION = {
@@ -34,7 +35,7 @@ DUMP_VERSION = {
}
class VersionMismatch(Exception):
class VersionMismatchError(Exception):
"""The dump cannot be replayed into this engine."""
@@ -46,11 +47,11 @@ def major_of(version: str) -> int:
``11.8.8-MariaDB-ubu2404``.
Raises:
VersionMismatch: the string does not start with a number.
VersionMismatchError: the string does not start with a number.
"""
leading = re.match(r"(\d+)", version)
if not leading:
raise VersionMismatch(f"cannot read a major version from '{version}'")
raise VersionMismatchError(f"cannot read a major version from '{version}'")
return int(leading.group(1))
@@ -65,10 +66,10 @@ def dump_version(sql_path: str, engine: str) -> str:
The version string as the dump spells it.
Raises:
VersionMismatch: no version line within the first ``SCAN_LINES``.
VersionMismatchError: no version line within the first ``SCAN_LINES``.
"""
pattern = DUMP_VERSION[engine]
with open(sql_path, encoding="utf-8", errors="replace") as handle:
with Path(sql_path).open(encoding="utf-8", errors="replace") as handle:
for _ in range(SCAN_LINES):
line = handle.readline()
if not line:
@@ -76,7 +77,7 @@ def dump_version(sql_path: str, engine: str) -> str:
found = pattern.search(line)
if found:
return found.group(1)
raise VersionMismatch(
raise VersionMismatchError(
f"{sql_path} carries no {engine} version header in its first {SCAN_LINES} lines"
)
@@ -120,10 +121,10 @@ def assert_replayable(sql_path: str, engine: str, dumped: str, serving: str) ->
server rejects and the pre-clean would already have dropped the schema.
Raises:
VersionMismatch: the dump is newer than the engine.
VersionMismatchError: the dump is newer than the engine.
"""
if major_of(dumped) > major_of(serving):
raise VersionMismatch(
raise VersionMismatchError(
f"{sql_path} came from {engine} {dumped} but {serving} is running; "
"a newer dump does not replay into an older engine, and --empty "
"would drop the schema before finding out"

View File

@@ -12,6 +12,7 @@ from __future__ import annotations
import os
import sys
from pathlib import Path
from .run import docker_volume_exists, run, stdout_of
@@ -21,7 +22,7 @@ INSPECT_FORMAT = (
def restore_volume_files(volume_name: str, backup_files_dir: str) -> int:
if not os.path.isdir(backup_files_dir):
if not Path(backup_files_dir).is_dir():
print(f"ERROR: backup files dir not found: {backup_files_dir}", file=sys.stderr)
return 2
@@ -44,7 +45,7 @@ def restore_volume_files(volume_name: str, backup_files_dir: str) -> int:
)
return 2
driver, options = (fields + ["local", "plain"])[1:3]
driver, options = ([*fields, "local", "plain"])[1:3]
if (driver != "local" or options == "opts") and not os.path.ismount(mountpoint):
print(
f"ERROR: volume {volume_name} has a backing store of its own "
@@ -55,8 +56,9 @@ def restore_volume_files(volume_name: str, backup_files_dir: str) -> int:
)
return 2
src = os.path.join(backup_files_dir, "")
dest = os.path.join(mountpoint, "")
# rsync reads "dir/" as its contents and "dir" as the directory itself.
src = f"{Path(backup_files_dir)}{os.sep}"
dest = f"{Path(mountpoint)}{os.sep}"
run(["rsync", "-avv", "--delete", src, dest])
print("File restore complete.")
return 0

View File

@@ -1,8 +1,8 @@
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
import pandas as pd
from pandas.errors import EmptyDataError
@@ -30,7 +30,7 @@ def check_and_add_entry(
"""
database = validate_database(database, instance=instance)
if os.path.exists(file_path):
if Path(file_path).exists():
try:
df = pd.read_csv(
file_path,