Update build_c_array.py to Python3
This commit is contained in:
parent
ea4bfda214
commit
395260e0eb
|
@ -2,6 +2,7 @@
|
||||||
#-*- coding: utf-8
|
#-*- coding: utf-8
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
from functools import cmp_to_key
|
||||||
|
|
||||||
COUNTRY_CODE_INDEX=1
|
COUNTRY_CODE_INDEX=1
|
||||||
IP_TYPE_INDEX=2
|
IP_TYPE_INDEX=2
|
||||||
|
@ -75,15 +76,15 @@ class IP_ELEMENT(object):
|
||||||
self._level = level
|
self._level = level
|
||||||
|
|
||||||
def printme(self):
|
def printme(self):
|
||||||
print 'static const ip_level %s = {' % (self.name())
|
print('static const ip_level %s = {' % (self.name()))
|
||||||
print '\t.prev = %s,' % (self._prev and '&%s' % (self._prev.name()) or 'NULL')
|
print('\t.prev = %s,' % (self._prev and '&%s' % (self._prev.name()) or 'NULL'))
|
||||||
print '\t.next = %s,' % (self._next and '&%s' % (self._next.name()) or 'NULL')
|
print('\t.next = %s,' % (self._next and '&%s' % (self._next.name()) or 'NULL'))
|
||||||
print '\t.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL')
|
print('\t.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL'))
|
||||||
print '\t.start = %d,' % (self._splitted_start[self._level])
|
print('\t.start = %d,' % (self._splitted_start[self._level]))
|
||||||
print '\t.end = %d,' % (self._splitted_end[self._level])
|
print('\t.end = %d,' % (self._splitted_end[self._level]))
|
||||||
print '\t.average = %d,' % (self._average)
|
print('\t.average = %d,' % (self._average))
|
||||||
print '\t.code = %d,' % (self._country_code and self._country_code or 0)
|
print('\t.code = %d,' % (self._country_code and self._country_code or 0))
|
||||||
print '};'
|
print('};')
|
||||||
|
|
||||||
def get_ip_len(self):
|
def get_ip_len(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
@ -178,8 +179,8 @@ while True:
|
||||||
else:
|
else:
|
||||||
sys.stderr.write('Unknown IP type %s\n' % (information[IP_TYPE_INDEX]))
|
sys.stderr.write('Unknown IP type %s\n' % (information[IP_TYPE_INDEX]))
|
||||||
|
|
||||||
print '/* This file was automatically generated, do not edit it ! */'
|
print('/* This file was automatically generated, do not edit it ! */')
|
||||||
print '#include <stdint.h>\n\n'
|
print('#include <stdint.h>\n\n')
|
||||||
|
|
||||||
def ip_sort(a, b):
|
def ip_sort(a, b):
|
||||||
for i in range(0, a.get_ip_len()):
|
for i in range(0, a.get_ip_len()):
|
||||||
|
@ -257,16 +258,16 @@ def print_ip(ip):
|
||||||
while cur_ip:
|
while cur_ip:
|
||||||
if cur_ip._childs:
|
if cur_ip._childs:
|
||||||
print_ip(cur_ip._childs)
|
print_ip(cur_ip._childs)
|
||||||
print 'static const ip_level %s;' % (cur_ip.name())
|
print('static const ip_level %s;' % (cur_ip.name()))
|
||||||
cur_ip = cur_ip._next
|
cur_ip = cur_ip._next
|
||||||
print ''
|
print('')
|
||||||
cur_ip = ip
|
cur_ip = ip
|
||||||
while cur_ip:
|
while cur_ip:
|
||||||
cur_ip.printme()
|
cur_ip.printme()
|
||||||
cur_ip = cur_ip._next
|
cur_ip = cur_ip._next
|
||||||
|
|
||||||
def build_array(ip_list, array_name, max_depth):
|
def build_array(ip_list, array_name, max_depth):
|
||||||
ip_list.sort(ip_sort)
|
ip_list = sorted(ip_list, key=cmp_to_key(ip_sort))
|
||||||
start_idx = 0
|
start_idx = 0
|
||||||
end_idx = start_idx+1
|
end_idx = start_idx+1
|
||||||
cur_interval = [ip_list[start_idx]]
|
cur_interval = [ip_list[start_idx]]
|
||||||
|
@ -288,18 +289,18 @@ def build_array(ip_list, array_name, max_depth):
|
||||||
res = manage_root(root, cur_interval, 1, max_depth)
|
res = manage_root(root, cur_interval, 1, max_depth)
|
||||||
print_ip(res)
|
print_ip(res)
|
||||||
|
|
||||||
print '\nstatic const ip_level* %s[256] = {' % (array_name)
|
print('\nstatic const ip_level* %s[256] = {' % (array_name))
|
||||||
for i in range(0, 256):
|
for i in range(0, 256):
|
||||||
if root_ips[i]:
|
if root_ips[i]:
|
||||||
print '\t&%s,' % (root_ips[i].name())
|
print('\t&%s,' % (root_ips[i].name()))
|
||||||
else:
|
else:
|
||||||
print '\tNULL, // %d' % (i)
|
print('\tNULL, // %d' % (i))
|
||||||
print '};\n'
|
print('};\n')
|
||||||
|
|
||||||
build_array(array_vals_ipv4.values(), 's_root_ipv4', 3)
|
build_array(list(array_vals_ipv4.values()), 's_root_ipv4', 3)
|
||||||
build_array(array_vals_ipv6.values(), 's_root_ipv6', 15)
|
build_array(list(array_vals_ipv6.values()), 's_root_ipv6', 15)
|
||||||
|
|
||||||
print 'static const uint8_t country_codes[][3] = {'
|
print('static const uint8_t country_codes[][3] = {')
|
||||||
for cc in countries:
|
for cc in countries:
|
||||||
print '\t{"%s"},' % (cc)
|
print('\t{"%s"},' % (cc))
|
||||||
print '};\n'
|
print('};\n')
|
||||||
|
|
|
@ -62,8 +62,8 @@ typedef struct thread_ctx_s{
|
||||||
socket_ctx_t* sockets;
|
socket_ctx_t* sockets;
|
||||||
int nb_cur_sockets;
|
int nb_cur_sockets;
|
||||||
int nb_available_sockets;
|
int nb_available_sockets;
|
||||||
int max_timeout;
|
int max_timeout; // in ms
|
||||||
int max_sockets; // in ms
|
int max_sockets;
|
||||||
int stop;
|
int stop;
|
||||||
int quiet;
|
int quiet;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
@ -481,7 +481,7 @@ int daemonize(struct gengetopt_args_info* params)
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = listen(new_socket, 0);
|
ret = listen(new_socket, params->sockets_per_thread_arg);
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
if (!params->quiet_flag)
|
if (!params->quiet_flag)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user