From: Michael Tremer Date: Sat, 6 Dec 2025 18:32:35 +0000 (+0000) Subject: dnsbl: Add command to export all lists X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86be401e6f824b0100c52c2b664c19083aaa919b;p=dnsbl.git dnsbl: Add command to export all lists Signed-off-by: Michael Tremer --- diff --git a/src/scripts/dnsbl.in b/src/scripts/dnsbl.in index 68027b3..0e127df 100644 --- a/src/scripts/dnsbl.in +++ b/src/scripts/dnsbl.in @@ -26,6 +26,7 @@ import os import rich.console import rich.table import sys +import tempfile # i18n from dnsbl.i18n import _ @@ -100,6 +101,11 @@ class CLI(object): help=_("Output Format")) export.set_defaults(func=self.__export) + # export-all + export = subparsers.add_parser("export-all", help=_("Export all lists")) + export.add_argument("directory", help=_("Output Directory")) + export.set_defaults(func=self.__export_all) + # add-source add_source = subparsers.add_parser("add-source", help=_("Creates a new source to a list")) @@ -259,6 +265,43 @@ class CLI(object): # Export! list.export(sys.stdout, format=args.format) + def __export_all(self, backend, args): + """ + Exports all lists + """ + formats = { + "domains" : "%s.txt", + "rpz" : "%s.rpz", + } + + # Ensure the output directory exists + try: + os.makedirs(args.directory) + except FileExistsError: + pass + + # Export all lists + for list in backend.lists: + for format, filename in formats.items(): + # Compose the output filename + name = os.path.join(args.directory, filename % list.slug) + + # Create a new temporary file + with tempfile.NamedTemporaryFile(mode="w", dir=args.directory) as f: + list.export(f, format=format) + + # Remove the previous file (if it exists) + try: + os.unlink(name) + except FileNotFoundError: + pass + + # Once the output has been written in full, we will rename the file + os.link(f.name, name) + + # Fix permissions + os.chmod(name, 0o644) + def __add_source(self, backend, args): """ Adds a new source to a list