]> git.ipfire.org Git - people/ms/libloc.git/blob - tools/copy.py
tools: Import the copy script
[people/ms/libloc.git] / tools / copy.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2024 IPFire Development Team <info@ipfire.org> #
7 # #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
12 # #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
17 # #
18 ###############################################################################
19
20 import argparse
21
22 import location
23 from location.i18n import _
24
25 flags = (
26 location.NETWORK_FLAG_ANONYMOUS_PROXY,
27 location.NETWORK_FLAG_SATELLITE_PROVIDER,
28 location.NETWORK_FLAG_ANYCAST,
29 location.NETWORK_FLAG_DROP,
30 )
31
32 def copy_all(db, writer):
33 # Copy vendor
34 if db.vendor:
35 writer.vendor = db.vendor
36
37 # Copy description
38 if db.description:
39 writer.description = db.description
40
41 # Copy license
42 if db.license:
43 writer.license = db.license
44
45 # Copy all ASes
46 for old in db.ases:
47 new = writer.add_as(old.number)
48 new.name = old.name
49
50 # Copy all networks
51 for old in db.networks:
52 new = writer.add_network("%s" % old)
53
54 # Copy country code
55 new.country_code = old.country_code
56
57 # Copy ASN
58 if old.asn:
59 new.asn = old.asn
60
61 # Copy flags
62 for flag in flags:
63 if old.has_flag(flag):
64 new.set_flag(flag)
65
66 # Copy countries
67 for old in db.countries:
68 new = writer.add_country(old.code)
69
70 # Copy continent code
71 new.continent_code = old.continent_code
72
73 # Copy name
74 new.name = old.name
75
76 def main():
77 """
78 Main Function
79 """
80 parser = argparse.ArgumentParser(
81 description=_("Copies a location database"),
82 )
83
84 # Input File
85 parser.add_argument("input-file", help=_("File to read"))
86
87 # Output File
88 parser.add_argument("output-file", help=_("File to write"))
89
90 # Parse arguments
91 args = parser.parse_args()
92
93 input_file = getattr(args, "input-file")
94 output_file = getattr(args, "output-file")
95
96 # Open the database
97 db = location.Database(input_file)
98
99 # Create a new writer
100 writer = location.Writer()
101
102 # Copy everything
103 copy_all(db, writer)
104
105 # Write the new file
106 writer.write(output_file)
107
108 main()