File: copy.py

package info (click to toggle)
libloc 0.9.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 7,602; python: 2,893; makefile: 511; sh: 39; perl: 13
file content (108 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
###############################################################################
#                                                                             #
# libloc - A library to determine the location of someone on the Internet     #
#                                                                             #
# Copyright (C) 2024 IPFire Development Team <info@ipfire.org>                #
#                                                                             #
# This library is free software; you can redistribute it and/or               #
# modify it under the terms of the GNU Lesser General Public                  #
# License as published by the Free Software Foundation; either                #
# version 2.1 of the License, or (at your option) any later version.          #
#                                                                             #
# This library is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           #
# Lesser General Public License for more details.                             #
#                                                                             #
###############################################################################

import argparse

import location
from location.i18n import _

flags = (
	location.NETWORK_FLAG_ANONYMOUS_PROXY,
	location.NETWORK_FLAG_SATELLITE_PROVIDER,
	location.NETWORK_FLAG_ANYCAST,
	location.NETWORK_FLAG_DROP,
)

def copy_all(db, writer):
	# Copy vendor
	if db.vendor:
		writer.vendor = db.vendor

	# Copy description
	if db.description:
		writer.description = db.description

	# Copy license
	if db.license:
		writer.license = db.license

	# Copy all ASes
	for old in db.ases:
		new = writer.add_as(old.number)
		new.name = old.name

	# Copy all networks
	for old in db.networks:
		new = writer.add_network("%s" % old)

		# Copy country code
		new.country_code = old.country_code

		# Copy ASN
		if old.asn:
			new.asn = old.asn

		# Copy flags
		for flag in flags:
			if old.has_flag(flag):
				new.set_flag(flag)

	# Copy countries
	for old in db.countries:
		new = writer.add_country(old.code)

		# Copy continent code
		new.continent_code = old.continent_code

		# Copy name
		new.name = old.name

def main():
	"""
		Main Function
	"""
	parser = argparse.ArgumentParser(
		description=_("Copies a location database"),
	)

	# Input File
	parser.add_argument("input-file", help=_("File to read"))

	# Output File
	parser.add_argument("output-file", help=_("File to write"))

	# Parse arguments
	args = parser.parse_args()

	input_file  = getattr(args, "input-file")
	output_file = getattr(args, "output-file")

	# Open the database
	db = location.Database(input_file)

	# Create a new writer
	writer = location.Writer()

	# Copy everything
	copy_all(db, writer)

	# Write the new file
	writer.write(output_file)

main()