]> git.ipfire.org Git - location/libloc.git/blobdiff - src/python/location-exporter.in
Rename location-query(8) to location(8)
[location/libloc.git] / src / python / location-exporter.in
index 421ed028456ce676f116b38e5934edf0d0563a16..d82f1d3df04aece8b77085d0da4bcc12050a587b 100644 (file)
 ###############################################################################
 
 import argparse
-import gettext
 import io
 import ipaddress
 import logging
-import logging.handlers
 import os.path
+import re
 import socket
 import sys
 
 # Load our location module
 import location
-
-def setup_logging(level=logging.INFO):
-       l = logging.getLogger("location-downloader")
-       l.setLevel(level)
-
-       # Log to console
-       h = logging.StreamHandler()
-       h.setLevel(logging.DEBUG)
-       l.addHandler(h)
-
-       # Log to syslog
-       h = logging.handlers.SysLogHandler(address="/dev/log",
-               facility=logging.handlers.SysLogHandler.LOG_DAEMON)
-       h.setLevel(logging.INFO)
-       l.addHandler(h)
-
-       # Format syslog messages
-       formatter = logging.Formatter("location-exporter[%(process)d]: %(message)s")
-       h.setFormatter(formatter)
-
-       return l
+from location.i18n import _
 
 # Initialise logging
-log = setup_logging()
-
-# i18n
-def _(singular, plural=None, n=None):
-       if plural:
-               return gettext.dngettext("libloc", singular, plural, n)
-
-       return gettext.dgettext("libloc", singular)
+log = logging.getLogger("location.exporter")
+log.propagate = 1
 
 class OutputWriter(object):
        suffix = "networks"
@@ -177,8 +150,8 @@ class Exporter(object):
                self.db = db
                self.writer = writer
 
-       def export(self, directory, countries, asns):
-               for family in (socket.AF_INET6, socket.AF_INET):
+       def export(self, directory, families, countries, asns):
+               for family in families:
                        log.debug("Exporting family %s" % family)
 
                        writers = {}
@@ -225,10 +198,12 @@ class CLI(object):
                # Global configuration flags
                parser.add_argument("--debug", action="store_true",
                        help=_("Enable debug output"))
+               parser.add_argument("--quiet", action="store_true",
+                       help=_("Enable quiet mode"))
 
                # version
                parser.add_argument("--version", action="version",
-                       version="%%(prog)s %s" % location.__version__)
+                       version="%(prog)s @VERSION@")
 
                # database
                parser.add_argument("--database", "-d",
@@ -242,14 +217,19 @@ class CLI(object):
                # directory
                parser.add_argument("--directory", help=_("Output directory"), required=True)
 
+               # family
+               parser.add_argument("--family", help=_("Specify address family"), choices=("ipv6", "ipv4"))
+
                # Countries and Autonomous Systems
                parser.add_argument("objects", nargs="+")
 
                args = parser.parse_args()
 
-               # Enable debug logging
+               # Configure logging
                if args.debug:
-                       log.setLevel(logging.DEBUG)
+                       location.logger.set_level(logging.DEBUG)
+               elif args.quiet:
+                       location.logger.set_level(logging.WARNING)
 
                return args
 
@@ -270,22 +250,32 @@ class CLI(object):
        def handle_export(self, ns):
                countries, asns = [], []
 
+               # Translate family
+               if ns.family == "ipv6":
+                       families = [ socket.AF_INET6 ]
+               elif ns.family == "ipv4":
+                       families = [ socket.AF_INET ]
+               else:
+                       families = [ socket.AF_INET6, socket.AF_INET ]
+
                for object in ns.objects:
-                       if object.startswith("AS"):
-                               try:
-                                       object = int(object[2:])
-                               except ValueError:
-                                       log.error("Invalid argument: %s" % object)
-                                       return 2
+                       m = re.match("^AS(\d+)$", object)
+                       if m:
+                               object = int(m.group(1))
 
                                asns.append(object)
 
-                       elif location.country_code_is_valid(object):
+                       elif location.country_code_is_valid(object) \
+                                       or object in ("A1", "A2", "A3"):
                                countries.append(object)
 
                        else:
-                               log.error("Invalid argument: %s" % object)
-                               return 2
+                               log.warning("Invalid argument: %s" % object)
+                               continue
+
+               if not countries and not asns:
+                       log.error("Nothing to export")
+                       return 2
 
                # Open the database
                try:
@@ -299,7 +289,7 @@ class CLI(object):
                assert writer
 
                e = Exporter(db, writer)
-               e.export(ns.directory, countries=countries, asns=asns)
+               e.export(ns.directory, countries=countries, asns=asns, families=families)
 
 
 def main():