]> git.ipfire.org Git - location/libloc.git/blobdiff - src/python/location-query.in
location-query: Add command to dump the whole database
[location/libloc.git] / src / python / location-query.in
index 4534279e72d6e7da34d565a2c025c824bb5386ec..fa3a6cb7a297e02a9c8e57a051dd53dff893e3d0 100644 (file)
@@ -22,6 +22,7 @@ import ipaddress
 import os
 import socket
 import sys
+import time
 
 # Load our location module
 import location
@@ -142,6 +143,13 @@ class CLI(object):
                lookup.add_argument("address", nargs="+")
                lookup.set_defaults(func=self.handle_lookup)
 
+               # Dump the whole database
+               dump = subparsers.add_parser("dump",
+                       help=_("Dump the entire database"),
+               )
+               dump.add_argument("output", nargs="?", type=argparse.FileType("w"))
+               dump.set_defaults(func=self.handle_dump)
+
                # Get AS
                get_as = subparsers.add_parser("get-as",
                        help=_("Get information about one or multiple Autonomous Systems"),
@@ -271,6 +279,48 @@ class CLI(object):
 
                return ret
 
+       def handle_dump(self, db, ns):
+               # Use output file or write to stdout
+               f = ns.output or sys.stdout
+
+               # Write metadata
+               f.write("#\n# Location Database Export\n#\n")
+
+               f.write("# Generated: %s\n" % time.strftime(
+                       "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
+               ))
+
+               if db.vendor:
+                       f.write("# Vendor:    %s\n" % db.vendor)
+
+               if db.license:
+                       f.write("# License:   %s\n" % db.license)
+
+               f.write("#\n")
+
+               if db.description:
+                       for line in db.description.splitlines():
+                               f.write("# %s\n" % line)
+
+                       f.write("#\n")
+
+               # Iterate over all ASes
+               for a in db.ases:
+                       f.write("\n")
+                       f.write("aut-num:        AS%s\n" % a.number)
+                       f.write("name:           %s\n" % a.name)
+
+               # Iterate over all networks
+               for n in db.networks:
+                       f.write("\n")
+                       f.write("net:            %s\n" % n)
+
+                       if n.country_code:
+                               f.write("country:        %s\n" % n.country_code)
+
+                       if n.asn:
+                               f.write("autnum:         %s\n" % n.asn)
+
        def handle_get_as(self, db, ns):
                """
                        Gets information about Autonomous Systems