Update build_c_array.py to Python3

This commit is contained in:
Gregory Soutade 2024-08-31 14:28:58 +02:00
parent ea4bfda214
commit 395260e0eb
2 changed files with 27 additions and 26 deletions

View File

@ -2,6 +2,7 @@
#-*- coding: utf-8
import sys
from functools import cmp_to_key
COUNTRY_CODE_INDEX=1
IP_TYPE_INDEX=2
@ -75,15 +76,15 @@ class IP_ELEMENT(object):
self._level = level
def printme(self):
print 'static const ip_level %s = {' % (self.name())
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.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL')
print '\t.start = %d,' % (self._splitted_start[self._level])
print '\t.end = %d,' % (self._splitted_end[self._level])
print '\t.average = %d,' % (self._average)
print '\t.code = %d,' % (self._country_code and self._country_code or 0)
print '};'
print('static const ip_level %s = {' % (self.name()))
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.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL'))
print('\t.start = %d,' % (self._splitted_start[self._level]))
print('\t.end = %d,' % (self._splitted_end[self._level]))
print('\t.average = %d,' % (self._average))
print('\t.code = %d,' % (self._country_code and self._country_code or 0))
print('};')
def get_ip_len(self):
raise NotImplementedError()
@ -178,8 +179,8 @@ while True:
else:
sys.stderr.write('Unknown IP type %s\n' % (information[IP_TYPE_INDEX]))
print '/* This file was automatically generated, do not edit it ! */'
print '#include <stdint.h>\n\n'
print('/* This file was automatically generated, do not edit it ! */')
print('#include <stdint.h>\n\n')
def ip_sort(a, b):
for i in range(0, a.get_ip_len()):
@ -257,16 +258,16 @@ def print_ip(ip):
while cur_ip:
if 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
print ''
print('')
cur_ip = ip
while cur_ip:
cur_ip.printme()
cur_ip = cur_ip._next
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
end_idx = start_idx+1
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)
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):
if root_ips[i]:
print '\t&%s,' % (root_ips[i].name())
print('\t&%s,' % (root_ips[i].name()))
else:
print '\tNULL, // %d' % (i)
print '};\n'
print('\tNULL, // %d' % (i))
print('};\n')
build_array(array_vals_ipv4.values(), 's_root_ipv4', 3)
build_array(array_vals_ipv6.values(), 's_root_ipv6', 15)
build_array(list(array_vals_ipv4.values()), 's_root_ipv4', 3)
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:
print '\t{"%s"},' % (cc)
print '};\n'
print('\t{"%s"},' % (cc))
print('};\n')

View File

@ -62,8 +62,8 @@ typedef struct thread_ctx_s{
socket_ctx_t* sockets;
int nb_cur_sockets;
int nb_available_sockets;
int max_timeout;
int max_sockets; // in ms
int max_timeout; // in ms
int max_sockets;
int stop;
int quiet;
pthread_mutex_t mutex;
@ -481,7 +481,7 @@ int daemonize(struct gengetopt_args_info* params)
return -2;
}
ret = listen(new_socket, 0);
ret = listen(new_socket, params->sockets_per_thread_arg);
if (ret)
{
if (!params->quiet_flag)