]> git.ipfire.org Git - dnsbl.git/commitdiff
dnsbl: Add command to export all lists
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2025 18:32:35 +0000 (18:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2025 18:32:35 +0000 (18:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/scripts/dnsbl.in

index 68027b3ed5af1475732fc323dff8e3feb9bb5ae9..0e127dfe2d121fcc6f4c9ad0fc51b4005cec6fc7 100644 (file)
@@ -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