import rich.console
import rich.table
import sys
+import tempfile
# i18n
from dnsbl.i18n import _
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"))
# 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