From faa9ff604d294a4dee96de26a58db7f92c38f48d Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Tue, 26 Mar 2024 16:04:20 +0100 Subject: [PATCH] Bump to python3 --- debian/rules | 66 +++++++++++++++++++++++++++++++++++++----- debian/rules.old | 65 ----------------------------------------- doc/system/conf.py | 16 +++++----- fail2ban-p2p-client.py | 14 ++++----- fail2ban-p2p/config.py | 12 ++++---- fail2ban-p2p/crypto.py | 8 ++--- fail2ban-p2p/log.py | 2 +- fail2ban-p2p/node.py | 4 +-- fail2ban-p2p/odict.py | 14 ++++----- fail2ban-p2p/parser.py | 4 +-- fail2ban-p2p/server.py | 6 ++-- fail2ban-p2p/util.py | 4 +-- setup.py | 8 ++--- 13 files changed, 105 insertions(+), 118 deletions(-) delete mode 100755 debian/rules.old diff --git a/debian/rules b/debian/rules index 69968c8..c81740a 100755 --- a/debian/rules +++ b/debian/rules @@ -1,13 +1,65 @@ #!/usr/bin/make -f -# You must remove unused comment lines for the released package. -#export DH_VERBOSE = 1 +# -*- makefile -*- +# Sample debian/rules that uses debhelper. +# This file was originally written by Joey Hess and Craig Small. +# As a special exception, when this file is copied by dh-make into a +# dh-make output file, you may use that output file without restriction. +# This special exception was added by Craig Small in version 0.37 of dh-make. + +# Uncomment this to turn on verbose mode. +#export DH_VERBOSE=1 -export PYBUILD_NAME=fail2ban-p2p DESTDIR=$(CURDIR)/debian/fail2ban-p2p -%: - dh $@ --with python3 --buildsystem=pybuild --root=$(DESTDIR) --no-compile --install-layout=deb +%: + dh $@ --with python2 + +# Override auto test because upstream do not use the standard unittest discover override_dh_auto_test: - echo "Not running tests - upstream tarball doesn't provide any." - + +# Override of auto_build +override_dh_auto_build: + +# Override of auto_install to remove information from package +override_dh_auto_install: + #dh_auto_install + python setup.py install --root=$(DESTDIR) --no-compile --install-layout=deb + + +clean: clean-inits + dh_testdir + dh_testroot + rm -f build-stamp configure-stamp + rm -rf build + # Does not hurt to ask distutils to do their duty + python setup.py clean + # Enforce removal of *.pyc files. Apparently dh_clean does + # not perform find on provided filename patterns. + find . -name \*.pyc -exec rm -f {} \; + dh_clean + +# +# Just to comply with policy 4.8 +binary-arch: + +# Build architecture-independent files here. +binary-indep: install + dh_testdir + dh_testroot + #dh_installchangelogs ChangeLog + dh_installdocs + dh_installlogrotate + dh_python2 + dh_installinit -- defaults 99 + #dh_installman man/*.1 + dh_link + dh_compress + dh_fixperms + dh_installdeb + dh_gencontrol + dh_md5sums + dh_builddeb + +binary: binary-indep +.PHONY: build clean binary-indep binary-arch binary install configure copy-inits clean-inits diff --git a/debian/rules.old b/debian/rules.old deleted file mode 100755 index de1b42d..0000000 --- a/debian/rules.old +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/make -f -# -*- makefile -*- -# Sample debian/rules that uses debhelper. -# This file was originally written by Joey Hess and Craig Small. -# As a special exception, when this file is copied by dh-make into a -# dh-make output file, you may use that output file without restriction. -# This special exception was added by Craig Small in version 0.37 of dh-make. - -# Uncomment this to turn on verbose mode. -#export DH_VERBOSE=1 - -DESTDIR=$(CURDIR)/debian/fail2ban-p2p - -configure: configure-stamp -configure-stamp: - dh_testdir - touch configure-stamp - -build: - -clean: clean-inits - dh_testdir - dh_testroot - rm -f build-stamp configure-stamp - rm -rf build - # Does not hurt to ask distutils to do their duty - python setup.py clean - # Enforce removal of *.pyc files. Apparently dh_clean does - # not perform find on provided filename patterns. - find . -name \*.pyc -exec rm -f {} \; - dh_clean - -install: build - dh_testdir - dh_testroot - dh_clean -k - dh_installdirs - - # Install the package into debian/fail2ban-p2p. - python setup.py install --root=$(DESTDIR) --no-compile --install-layout=deb - -# -# Just to comply with policy 4.8 -binary-arch: - -# Build architecture-independent files here. -binary-indep: install - dh_testdir - dh_testroot - #dh_installchangelogs ChangeLog - dh_installdocs - dh_installlogrotate - dh_python2 - dh_installinit -- defaults 99 - #dh_installman man/*.1 - dh_link - dh_compress - dh_fixperms - dh_installdeb - dh_gencontrol - dh_md5sums - dh_builddeb - -binary: binary-indep -.PHONY: build clean binary-indep binary-arch binary install configure copy-inits clean-inits diff --git a/doc/system/conf.py b/doc/system/conf.py index 583cd6a..9fa736b 100644 --- a/doc/system/conf.py +++ b/doc/system/conf.py @@ -42,8 +42,8 @@ source_suffix = '.rst' master_doc = 'index' # General information about the project. -project = u'fail2ban-p2p' -copyright = u'2013, Manuel Munz, Johannes Fürmann' +project = 'fail2ban-p2p' +copyright = '2013, Manuel Munz, Johannes Fürmann' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -185,8 +185,8 @@ latex_elements = { # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'fail2ban-p2p.tex', u'fail2ban-p2p Documentation', - u'Manuel Munz, Johannes Fürmann', 'manual'), + ('index', 'fail2ban-p2p.tex', 'fail2ban-p2p Documentation', + 'Manuel Munz, Johannes Fürmann', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -215,8 +215,8 @@ latex_documents = [ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'fail2ban-p2p', u'fail2ban-p2p Documentation', - [u'Manuel Munz, Johannes Fürmann'], 1) + ('index', 'fail2ban-p2p', 'fail2ban-p2p Documentation', + ['Manuel Munz, Johannes Fürmann'], 1) ] # If true, show URL addresses after external links. @@ -229,8 +229,8 @@ man_pages = [ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'fail2ban-p2p', u'fail2ban-p2p Documentation', - u'Manuel Munz, Johannes Fürmann', 'fail2ban-p2p', 'One line description of project.', + ('index', 'fail2ban-p2p', 'fail2ban-p2p Documentation', + 'Manuel Munz, Johannes Fürmann', 'fail2ban-p2p', 'One line description of project.', 'Miscellaneous'), ] diff --git a/fail2ban-p2p-client.py b/fail2ban-p2p-client.py index ac9b0b8..f19f952 100755 --- a/fail2ban-p2p-client.py +++ b/fail2ban-p2p-client.py @@ -52,7 +52,7 @@ if c.loadConfig() == False: exit() if not args.d and not args.b: - print "Please use the -b argument to specify an IP to ban or -d to request information about banned nodes." + print("Please use the -b argument to specify an IP to ban or -d to request information about banned nodes.") exit() dump = False @@ -120,10 +120,10 @@ try: s.send(cmdsigned) ret = s.recv(1048576) # we need about 50 Bytes per banned node if not quiet and args.v: - print ("Message sent: " + cmdsigned) + print(("Message sent: " + cmdsigned)) except: if not quiet: - print ("could not connect to "+c.name+" ("+c.addresses[0]+":"+str(c.port)+")") + print(("could not connect to "+c.name+" ("+c.addresses[0]+":"+str(c.port)+")")) finally: s.close() @@ -135,17 +135,17 @@ if ret: elif args.d == "json": print(ret) elif args.d == "count": - print(len(json.loads(ret))) + print((len(json.loads(ret)))) else: banList = json.loads(ret) if len(banList) > 0: - print("IP".ljust(15, ' ') + "\tTimestamp\t\tBantime\t\tTrustlevel\tStatus") + print(("IP".ljust(15, ' ') + "\tTimestamp\t\tBantime\t\tTrustlevel\tStatus")) for ban in banList: status = "PENDING" if int(c.threshold) <= int(ban['Trustlevel']): status = "BANNED" - print(ban['AttackerIP'].ljust(15, ' ') + "\t" + str(ban['Timestamp']) + "\t\t" + str(ban['BanTime']) + "\t\t" + str(ban['Trustlevel'])) + "\t\t" + status + print((ban['AttackerIP'].ljust(15, ' ') + "\t" + str(ban['Timestamp']) + "\t\t" + str(ban['BanTime']) + "\t\t" + str(ban['Trustlevel'])) + "\t\t" + status) else: print("No hosts in banlist") else: - print("Answer: " + ret) + print(("Answer: " + ret)) diff --git a/fail2ban-p2p/config.py b/fail2ban-p2p/config.py index da5c3dc..b7b35e0 100644 --- a/fail2ban-p2p/config.py +++ b/fail2ban-p2p/config.py @@ -6,7 +6,7 @@ # Licensed under the GNU GENERAL PUBLIC LICENSE Version 3. For details # see the file COPYING or http://www.gnu.org/licenses/gpl-3.0.en.html. -import ConfigParser +import configparser import os import logging @@ -49,15 +49,15 @@ class Config: ''' try: value = config.get(section, option) - except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): + except (configparser.NoOptionError, configparser.NoSectionError): if mandatory: - print "Mandatory option " + option + " not found in config file " + self.configFile + print("Mandatory option " + option + " not found in config file " + self.configFile) return False else: value = default return value - config = ConfigParser.RawConfigParser() + config = configparser.RawConfigParser() self.configFile = os.path.join(self.configPath, 'fail2ban-p2p.conf') if os.access(self.configFile, os.R_OK): @@ -74,6 +74,6 @@ class Config: self.threshold = get_option('Node', 'threshold', False, 80) else: - print('ERROR: Configuration directory "' + self.configPath + '" does not exist.\n' + - 'Please create a configuration or specify another valid configuration directory with the "-c" argument.') + print(('ERROR: Configuration directory "' + self.configPath + '" does not exist.\n' + + 'Please create a configuration or specify another valid configuration directory with the "-c" argument.')) return False diff --git a/fail2ban-p2p/crypto.py b/fail2ban-p2p/crypto.py index 647abd8..920c6cf 100644 --- a/fail2ban-p2p/crypto.py +++ b/fail2ban-p2p/crypto.py @@ -24,8 +24,8 @@ def create_keys(): """ if os.path.isfile(c.privkey) or os.path.isfile(c.pubkey): - print "A keypair for this node already exists." - ask = raw_input('Do you really want to create a new one? [y/N] ') + print("A keypair for this node already exists.") + ask = input('Do you really want to create a new one? [y/N] ') if ask != "y": return M2Crypto.Rand.rand_seed (os.urandom (1024)) @@ -33,11 +33,11 @@ def create_keys(): keypair = M2Crypto.RSA.gen_key (1024, 65537) try: keypair.save_key(c.privkey, None) - os.chmod(c.privkey, 0400) + os.chmod(c.privkey, 0o400) keypair.save_pub_key(c.pubkey) logger.debug("Private key (secret) was saved to " + c.privkey) logger.debug("Public key was saved to " + c.pubkey) - except IOError, e: + except IOError as e: logger.error("Could not save the keypair, check permissions! " + "%s" % e) exit() diff --git a/fail2ban-p2p/log.py b/fail2ban-p2p/log.py index da8d4fe..556c908 100644 --- a/fail2ban-p2p/log.py +++ b/fail2ban-p2p/log.py @@ -47,7 +47,7 @@ def initialize_logging(name="unknown"): log2file.setLevel(c.logLevel) logger.addHandler(log2file) except: - print("--- WARNING --- LOGFILE " + c.logFile + " IS EITHER NONEXISTENT OR NOT WRITABLE") + print(("--- WARNING --- LOGFILE " + c.logFile + " IS EITHER NONEXISTENT OR NOT WRITABLE")) log2stderr = logging.StreamHandler(sys.stderr) log2stderr.setFormatter(formatter) log2stderr.setLevel(c.logLevel) diff --git a/fail2ban-p2p/node.py b/fail2ban-p2p/node.py index e5969c7..6225113 100644 --- a/fail2ban-p2p/node.py +++ b/fail2ban-p2p/node.py @@ -8,7 +8,7 @@ import config import socket -import thread +import _thread import threading from command import Command import server @@ -74,7 +74,7 @@ class Node: for sock in readable: client_socket, address = sock.accept() logger.debug("connection from "+address[0]) - thread.start_new_thread(server.serve, (self, client_socket, address)) + _thread.start_new_thread(server.serve, (self, client_socket, address)) except Exception as e: print(e) diff --git a/fail2ban-p2p/odict.py b/fail2ban-p2p/odict.py index e61f0f3..d431c36 100644 --- a/fail2ban-p2p/odict.py +++ b/fail2ban-p2p/odict.py @@ -5,9 +5,9 @@ # http://opensource.org/licenses/MIT try: - from thread import get_ident as _get_ident + from _thread import get_ident as _get_ident except ImportError: - from dummy_thread import get_ident as _get_ident + from _dummy_thread import get_ident as _get_ident try: from _abcoll import KeysView, ValuesView, ItemsView @@ -90,7 +90,7 @@ class OrderedDict(dict): def clear(self): 'od.clear() -> None. Remove all items from od.' try: - for node in self.__map.itervalues(): + for node in self.__map.values(): del node[:] root = self.__root root[:] = [root, root, None] @@ -173,12 +173,12 @@ class OrderedDict(dict): for key in other: self[key] = other[key] elif hasattr(other, 'keys'): - for key in other.keys(): + for key in list(other.keys()): self[key] = other[key] else: for key, value in other: self[key] = value - for key, value in kwds.items(): + for key, value in list(kwds.items()): self[key] = value __update = update # let subclasses override update without breaking __init__ @@ -214,7 +214,7 @@ class OrderedDict(dict): try: if not self: return '%s()' % (self.__class__.__name__,) - return '%s(%r)' % (self.__class__.__name__, self.items()) + return '%s(%r)' % (self.__class__.__name__, list(self.items())) finally: del _repr_running[call_key] @@ -249,7 +249,7 @@ class OrderedDict(dict): ''' if isinstance(other, OrderedDict): - return len(self)==len(other) and self.items() == other.items() + return len(self)==len(other) and list(self.items()) == list(other.items()) return dict.__eq__(self, other) def __ne__(self, other): diff --git a/fail2ban-p2p/parser.py b/fail2ban-p2p/parser.py index 42cddf5..ff1f7eb 100644 --- a/fail2ban-p2p/parser.py +++ b/fail2ban-p2p/parser.py @@ -30,7 +30,7 @@ def getDictValue(dict, key): try: return dict[key] - except KeyError, e: + except KeyError as e: return False def parse(msg): @@ -49,7 +49,7 @@ def parse(msg): try: signed_dict = json.loads(msg) - except ValueError, e: + except ValueError as e: logger.warning("The received message does not appear to be valid json.") return False diff --git a/fail2ban-p2p/server.py b/fail2ban-p2p/server.py index 3eaa85a..75206b9 100644 --- a/fail2ban-p2p/server.py +++ b/fail2ban-p2p/server.py @@ -42,13 +42,13 @@ def serve(n, connection, address): else: connection.send("OK\n") - except customexceptions.InvalidMessage, e: + except customexceptions.InvalidMessage as e: connection.send("ERROR Invalid message\n") logger.warn("This message made no sense.") - except customexceptions.InvalidSignature, e: + except customexceptions.InvalidSignature as e: connection.send("ERROR invalid signature\n") logger.warn("The Signature could not be verified") - except customexceptions.InvalidProtocolVersion, e: + except customexceptions.InvalidProtocolVersion as e: connection.send("ERROR invalid protocol version\n") else: connection.send("Error\n") diff --git a/fail2ban-p2p/util.py b/fail2ban-p2p/util.py index 8af9d70..3c9ddf7 100644 --- a/fail2ban-p2p/util.py +++ b/fail2ban-p2p/util.py @@ -28,9 +28,9 @@ def sort_recursive(dictionary): OrderedDict([('a', '2'), ('b', OrderedDict([('d', 1), ('e', 4)])), ('c', 3), ('f', 5)]) """ - sorted_list = OrderedDict(sorted(dictionary.items(), key = lambda x: x[0])) + sorted_list = OrderedDict(sorted(list(dictionary.items()), key = lambda x: x[0])) # TODO test for cyclic structures. - for key, value in sorted_list.items(): + for key, value in list(sorted_list.items()): if type(value) is dict: sorted_list[key] = sort_recursive(value) diff --git a/setup.py b/setup.py index 2252f88..cd69ca7 100755 --- a/setup.py +++ b/setup.py @@ -55,9 +55,9 @@ setup( # Update config file if argv[1] == "install": - print - print "Please do not forget to update your configuration files." - print "They are in /etc/fail2ban-p2p/." - print + print() + print("Please do not forget to update your configuration files.") + print("They are in /etc/fail2ban-p2p/.") + print()