From 78161a96be7fa2380e46b0aa530fd9441b39df44 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 6 Jan 2026 16:01:31 +0900 Subject: [PATCH] Try to read config from `/etc/dibbler/dibbler.conf` --- dibbler/conf.py | 19 +++++++++++++++++++ dibbler/main.py | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/dibbler/conf.py b/dibbler/conf.py index b862ade..8fef746 100644 --- a/dibbler/conf.py +++ b/dibbler/conf.py @@ -1,6 +1,25 @@ # This module is supposed to act as a singleton and be filled # with config variables by cli.py +from pathlib import Path +import os import configparser config = configparser.ConfigParser() + +DEFAULT_CONFIG_PATH = Path('/etc/dibbler/dibbler.conf') + +def default_config_path_submissive_and_readable() -> bool: + return DEFAULT_CONFIG_PATH.is_file() and any( + [ + ( + DEFAULT_CONFIG_PATH.stat().st_mode & 0o400 + and DEFAULT_CONFIG_PATH.stat().st_uid == os.getuid() + ), + ( + DEFAULT_CONFIG_PATH.stat().st_mode & 0o040 + and DEFAULT_CONFIG_PATH.stat().st_gid == os.getgid() + ), + (DEFAULT_CONFIG_PATH.stat().st_mode & 0o004), + ] + ) diff --git a/dibbler/main.py b/dibbler/main.py index f7c459f..b9313a8 100644 --- a/dibbler/main.py +++ b/dibbler/main.py @@ -1,6 +1,7 @@ import argparse +import sys -from dibbler.conf import config +from dibbler.conf import DEFAULT_CONFIG_PATH, config, default_config_path_submissive_and_readable parser = argparse.ArgumentParser() @@ -25,7 +26,13 @@ subparsers.add_parser("seed-data", help="Fill with mock data") def main(): args = parser.parse_args() - config.read(args.config) + + if args.config is not None: + config.read(args.config) + elif default_config_path_submissive_and_readable(): + config.read(DEFAULT_CONFIG_PATH) + else: + print("Could not read config file, it was neither provided nor readable in default location", file=sys.stderr) if args.subcommand == "loop": import dibbler.subcommands.loop as loop