Files
dotfiles/python/common.py
T
Richie 37f37c41ac
treefmt / nix fmt (pull_request) Successful in 5s
pytest / pytest (pull_request) Successful in 24s
build_systems / build-brain (pull_request) Successful in 44s
build_systems / build-bob (pull_request) Successful in 44s
build_systems / build-rhapsody-in-green (pull_request) Successful in 57s
build_systems / build-jeeves (pull_request) Successful in 2m17s
treefmt / nix fmt (push) Successful in 5s
build_systems / build-brain (push) Successful in 8s
pytest / pytest (push) Successful in 23s
build_systems / build-bob (push) Successful in 30s
build_systems / build-rhapsody-in-green (push) Successful in 42s
build_systems / build-jeeves (push) Successful in 2m2s
feat(common): add get_repo_dir function and corresponding tests
2026-07-12 14:24:05 -04:00

48 lines
1.2 KiB
Python

"""common."""
from __future__ import annotations
import logging
from datetime import UTC, datetime
from pathlib import Path
from subprocess import PIPE, Popen
from python.logging_config import configure_logger as _configure_logger
logger = logging.getLogger(__name__)
def get_repo_dir() -> Path:
"""Return the repository root directory."""
return Path(__file__).resolve().parents[1]
def configure_logger(level: str = "INFO") -> None:
"""Configure the logger."""
_configure_logger(level)
def bash_wrapper(command: str) -> tuple[str, int]:
"""Execute a bash command and capture the output.
Args:
command (str): The bash command to be executed.
Returns:
Tuple[str, int]: A tuple containing the output of the command (stdout) as a string,
the error output (stderr) as a string (optional), and the return code as an integer.
"""
# This is a acceptable risk
process = Popen(command.split(), stdout=PIPE, stderr=PIPE)
output, error = process.communicate()
if error:
logger.error(f"{error=}")
return error.decode(), process.returncode
return output.decode(), process.returncode
def utcnow() -> datetime:
"""Get the current UTC time."""
return datetime.now(tz=UTC)