###############################################################################
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"
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 = {}
# 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",
# 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
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:
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():