use nametuple

This commit is contained in:
Wes Barnett 2021-03-27 19:05:00 -04:00
parent 7734706157
commit d81f6f1f6f
No known key found for this signature in database
GPG key ID: 1070BCC98C18BD66
2 changed files with 27 additions and 18 deletions

View file

@ -17,6 +17,7 @@
"""Script for taking pre/post snapshots; run from pacman hooks.""" """Script for taking pre/post snapshots; run from pacman hooks."""
from argparse import ArgumentParser from argparse import ArgumentParser
from collections import namedtuple
from configparser import ConfigParser from configparser import ConfigParser
import json import json
import logging import logging
@ -59,6 +60,12 @@ class SnapperCmd:
return " ".join(self.cmd) return " ".join(self.cmd)
ProcessedConfig = namedtuple(
"ProcessedConfig",
["description", "cleanup_algorithm", "userdata", "snapshot"]
)
class ConfigProcessor: class ConfigProcessor:
def __init__(self, ini_file, snapshot_type, parent_cmd=None, packages=None): def __init__(self, ini_file, snapshot_type, parent_cmd=None, packages=None):
@ -119,12 +126,12 @@ class ConfigProcessor:
def __call__(self, section): def __call__(self, section):
if section not in self.config: if section not in self.config:
self.config.add_section(section) self.config.add_section(section)
return { return ProcessedConfig(
"description": self.get_description(section), self.get_description(section),
"cleanup_algorithm": self.get_cleanup_algorithm(section), self.get_cleanup_algorithm(section),
"userdata": self.get_userdata(section), self.get_userdata(section),
"snapshot": self.config.getboolean(section, "snapshot") self.config.getboolean(section, "snapshot")
} )
def get_snapper_configs(conf_file): def get_snapper_configs(conf_file):
@ -181,12 +188,12 @@ if __name__ == "__main__":
for snapper_config in snapper_configs: for snapper_config in snapper_configs:
data = config_processor(snapper_config) processed_config = config_processor(snapper_config)
if data["snapshot"]: if processed_config["snapshot"]:
prefile = tmpdir / f"snap-pac-pre_{snapper_config}" prefile = tmpdir / f"snap-pac-pre_{snapper_config}"
pre_number = get_pre_number(snapshot_type, prefile) pre_number = get_pre_number(snapshot_type, prefile)
snapper_cmd = SnapperCmd(snapper_config, snapshot_type, data["cleanup_algorithm"], snapper_cmd = SnapperCmd(snapper_config, snapshot_type, processed_config["cleanup_algorithm"],
data["description"], chroot, pre_number, data["userdata"]) processed_config["description"], chroot, pre_number, processed_config["userdata"])
num = snapper_cmd() num = snapper_cmd()
logging.info(f"==> {snapper_config}: {num}") logging.info(f"==> {snapper_config}: {num}")

View file

@ -4,7 +4,10 @@ import os
import pytest import pytest
from scripts.snap_pac import SnapperCmd, ConfigProcessor, check_skip, get_pre_number, get_snapper_configs from scripts.snap_pac import (
SnapperCmd, ConfigProcessor, check_skip, get_pre_number, get_snapper_configs,
ProcessedConfig
)
@pytest.fixture @pytest.fixture
@ -65,28 +68,27 @@ def test_skip_snap_pac():
@pytest.mark.parametrize("section, command, packages, snapshot_type, result", [ @pytest.mark.parametrize("section, command, packages, snapshot_type, result", [
( (
"root", "foo", ["bar"], "pre", "root", "foo", ["bar"], "pre",
{"description": "foo", "cleanup_algorithm": "number", "userdata": "", "snapshot": True} ProcessedConfig("foo", "number", "", True)
), ),
( (
"root", "pacman -Syu", [], "pre", "root", "pacman -Syu", [], "pre",
{"description": "pacman -Syu", "cleanup_algorithm": "number", "userdata": "important=yes", "snapshot": True} ProcessedConfig("pacman -Syu", "number", "important=yes", True)
), ),
( (
"mail", "pacman -Syu", [], "pre", "mail", "pacman -Syu", [], "pre",
{"description": "pacman -Syu", "cleanup_algorithm": "number", "userdata": "", "snapshot": False} ProcessedConfig("pacman -Syu", "number", "", False)
), ),
( (
"home", "pacman -Syu", [], "pre", "home", "pacman -Syu", [], "pre",
{"description": "pac", "cleanup_algorithm": "number", "userdata": "foo=bar,requestid=42", "snapshot": True} ProcessedConfig("pac", "number", "foo=bar,requestid=42", True)
), ),
( (
"home", "pacman -Syu", [], "post", "home", "pacman -Syu", [], "post",
{"description": "a r", "cleanup_algorithm": "number", "userdata": "foo=bar,requestid=42", "snapshot": True} ProcessedConfig("a r", "number", "foo=bar,requestid=42", True)
), ),
( (
"myconfig", "pacman -S linux", ["linux"], "post", "myconfig", "pacman -S linux", ["linux"], "post",
{"description": "linux", "cleanup_algorithm": "timeline", ProcessedConfig("linux", "timeline", "foo=bar,important=yes,requestid=42", True)
"userdata": "foo=bar,important=yes,requestid=42", "snapshot": True}
), ),
]) ])
def test_config_processor(section, command, packages, snapshot_type, result): def test_config_processor(section, command, packages, snapshot_type, result):