Skip to content

Commit

Permalink
compatibility fix (virtualenv, running test cases in py3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebres committed Aug 11, 2016
1 parent 77f451c commit 08af8de
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ fail2ban/server/strptime.py
fail2ban/server/ticket.py
fail2ban/server/transmitter.py
fail2ban/server/utils.py
fail2ban/setup.py
fail2ban-testcases-all
fail2ban-testcases-all-python3
fail2ban/tests/action_d/__init__.py
Expand Down
3 changes: 2 additions & 1 deletion bin/fail2ban-testcases
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ if os.path.exists("fail2ban/__init__.py"):
from fail2ban.version import version

from fail2ban.tests.utils import gatherTests
from fail2ban.helpers import updatePyExec, FormatterWithTraceBack, getLogger
from fail2ban.helpers import FormatterWithTraceBack, getLogger
from fail2ban.setup import updatePyExec
from fail2ban.server.mytime import MyTime

from optparse import OptionParser, Option
Expand Down
18 changes: 0 additions & 18 deletions fail2ban/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,6 @@ def format(self, record):
return logging.Formatter.format(self, record)


def updatePyExec(bindir, executable=None):
"""Update fail2ban-python link to current python version (where f2b-modules located/installed)
"""
bindir = os.path.realpath(bindir)
if executable is None:
executable = sys.executable
pypath = os.path.join(bindir, 'fail2ban-python')
# if not exists or point to another version - update link:
isfile = os.path.isfile(pypath)
if not isfile or os.path.realpath(pypath) != os.path.realpath(executable):
if isfile:
os.unlink(pypath)
os.symlink(executable, pypath)
# extend current environment path (e.g. if fail2ban not yet installed):
if bindir not in os.environ["PATH"].split(os.pathsep):
os.environ["PATH"] = os.environ["PATH"] + os.pathsep + bindir;


def getLogger(name):
"""Get logging.Logger instance with Fail2Ban logger name convention
"""
Expand Down
42 changes: 42 additions & 0 deletions fail2ban/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
# vi: set ft=python sts=4 ts=4 sw=4 noet :

# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

__author__ = "Serg G. Brester"
__license__ = "GPL"

import os
import sys


def updatePyExec(bindir, executable=None):
"""Update fail2ban-python link to current python version (where f2b-modules located/installed)
"""
bindir = os.path.realpath(bindir)
if executable is None:
executable = sys.executable
pypath = os.path.join(bindir, 'fail2ban-python')
# if not exists or point to another version - update link:
isfile = os.path.isfile(pypath)
if not isfile or os.path.realpath(pypath) != os.path.realpath(executable):
if isfile:
os.unlink(pypath)
os.symlink(executable, pypath)
# extend current environment path (e.g. if fail2ban not yet installed):
if bindir not in os.environ["PATH"].split(os.pathsep):
os.environ["PATH"] = os.environ["PATH"] + os.pathsep + bindir;
33 changes: 21 additions & 12 deletions fail2ban/tests/misctestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ def testsplitwords(self):
self.assertEqual(splitwords(' 1\n 2, 3'), ['1', '2', '3'])


if sys.version_info >= (2,7):
def _sh_call(cmd):
import subprocess, locale
ret = subprocess.check_output(cmd, shell=True)
if sys.version_info >= (3,):
ret = ret.decode(locale.getpreferredencoding(), 'replace')
return str(ret).rstrip()
else:
def _sh_call(cmd):
import subprocess
ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
return str(ret).rstrip()

def _getSysPythonVersion():
import subprocess, locale
sysVerCmd = "fail2ban-python -c 'import sys; print(tuple(sys.version_info))'"
if sys.version_info >= (2,7):
sysVer = subprocess.check_output(sysVerCmd, shell=True)
else:
sysVer = subprocess.Popen(sysVerCmd, shell=True, stdout=subprocess.PIPE).stdout.read()
if sys.version_info >= (3,):
sysVer = sysVer.decode(locale.getpreferredencoding(), 'replace')
return str(sysVer).rstrip()
return _sh_call("fail2ban-python -c 'import sys; print(tuple(sys.version_info))'")

class SetupTest(unittest.TestCase):

Expand Down Expand Up @@ -144,9 +149,13 @@ def recursive_glob(treeroot, pattern):
'etc/fail2ban/jail.conf'):
self.assertTrue(os.path.exists(os.path.join(tmp, f)),
msg="Can't find %s" % f)
self.assertEqual(
os.path.realpath(os.path.join(tmp, 'usr/local/bin/fail2ban-python')),
os.path.realpath(sys.executable))
# Because the install (test) path in virtual-env differs from some development-env,
# it is not a `tmp + '/usr/local/bin/'`, so search for it:
installedPath = _sh_call('find ' + tmp+ ' -name fail2ban-python').split('\n')
self.assertTrue(len(installedPath) > 0)
for installedPath in installedPath:
self.assertEqual(
os.path.realpath(installedPath), os.path.realpath(sys.executable))

finally:
# clean up
Expand Down
18 changes: 4 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,7 @@
import warnings
from glob import glob


def updatePyExec(bindir, executable=None):
"""Update fail2ban-python link to current python version (where f2b-modules located/installed)
"""
bindir = os.path.realpath(bindir)
if executable is None:
executable = sys.executable
pypath = os.path.join(bindir, 'fail2ban-python')
# if not exists or point to another version - update link:
isfile = os.path.isfile(pypath)
if not isfile or os.path.realpath(pypath) != os.path.realpath(executable):
if isfile:
os.unlink(pypath)
os.symlink(executable, pypath)
from fail2ban.setup import updatePyExec


# Wrapper to install python binding (to current python version):
Expand All @@ -78,6 +65,9 @@ def get_outputs(self):
return outputs


# Update fail2ban-python env to current python version (where f2b-modules located/installed)
updatePyExec(os.path.join(os.path.dirname(__file__), 'bin'))

if setuptools and "test" in sys.argv:
import logging
logSys = logging.getLogger("fail2ban")
Expand Down

0 comments on commit 08af8de

Please sign in to comment.