BF: Require Python 2.7+ for badips.py action

pull/641/head
Steven Hiscocks 2014-03-12 21:54:15 +00:00
parent 5c7630c4be
commit dfb46cfda6
3 changed files with 64 additions and 52 deletions

View File

@ -17,11 +17,13 @@
# along with Fail2Ban; if not, write to the Free Software # along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
if sys.version_info < (2, 7):
raise ImportError("badips.py action requires Python >= 2.7")
import json import json
from functools import partial from functools import partial
import threading import threading
import logging import logging
import sys
if sys.version_info >= (3, ): if sys.version_info >= (3, ):
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from urllib.parse import urlencode from urllib.parse import urlencode

View File

@ -31,7 +31,11 @@ if sys.version_info >= (3, 3):
import importlib.machinery import importlib.machinery
else: else:
import imp import imp
from collections import Mapping, OrderedDict from collections import Mapping
try:
from collections import OrderedDict
except ImportError:
OrderedDict = None
from .banmanager import BanManager from .banmanager import BanManager
from .jailthread import JailThread from .jailthread import JailThread
@ -62,7 +66,10 @@ class Actions(JailThread, Mapping):
JailThread.__init__(self) JailThread.__init__(self)
## The jail which contains this action. ## The jail which contains this action.
self._jail = jail self._jail = jail
if OrderedDict is not None:
self._actions = OrderedDict() self._actions = OrderedDict()
else:
self._actions = dict()
## The ban manager. ## The ban manager.
self.__banManager = BanManager() self.__banManager = BanManager()

View File

@ -32,7 +32,8 @@ if os.path.exists('config/fail2ban.conf'):
else: else:
CONFIG_DIR='/etc/fail2ban' CONFIG_DIR='/etc/fail2ban'
class BadIPsActionTest(unittest.TestCase): if sys.version_info >= (2,7):
class BadIPsActionTest(unittest.TestCase):
def setUp(self): def setUp(self):
"""Call before every test case.""" """Call before every test case."""
@ -59,7 +60,8 @@ class BadIPsActionTest(unittest.TestCase):
self.assertTrue(len(categories) >= 10) self.assertTrue(len(categories) >= 10)
self.assertRaises( self.assertRaises(
ValueError, setattr, self.action, "category", "invalid-category") ValueError, setattr, self.action, "category",
"invalid-category")
# Not valid for reporting category... # Not valid for reporting category...
self.assertRaises( self.assertRaises(
@ -74,7 +76,8 @@ class BadIPsActionTest(unittest.TestCase):
def testBanaction(self): def testBanaction(self):
self.assertRaises( self.assertRaises(
ValueError, setattr, self.action, "banaction", "invalid-action") ValueError, setattr, self.action, "banaction",
"invalid-action")
self.action.banaction = "test" self.action.banaction = "test"
def testUpdateperiod(self): def testUpdateperiod(self):