diff --git a/ChangeLog b/ChangeLog index 57b6c60a..06775a6b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -41,6 +41,8 @@ ver. 0.10.4-dev-1 (20??/??/??) - development edition ### New Features ### Enhancements +* since v.0.10.4, fail2ban-client, fail2ban-server and fail2ban-regex will return version without logo info, + additionally option `-V` can be used to get version in normalized machine-readable short format. ver. 0.10.3 (2018/04/04) - the-time-is-always-right-to-do-what-is-right diff --git a/fail2ban/client/fail2bancmdline.py b/fail2ban/client/fail2bancmdline.py index 269fa174..1268ee9f 100644 --- a/fail2ban/client/fail2bancmdline.py +++ b/fail2ban/client/fail2bancmdline.py @@ -25,7 +25,7 @@ import logging import os import sys -from ..version import version +from ..version import version, normVersion from ..protocol import printFormatted from ..helpers import getLogger, str2LogLevel, getVerbosityFormat @@ -78,12 +78,11 @@ class Fail2banCmdLine(): for o in obj.__dict__: self.__dict__[o] = obj.__dict__[o] - def dispVersion(self): - output("Fail2Ban v" + version) - output("") - output("Copyright (c) 2004-2008 Cyril Jaquier, 2008- Fail2Ban Contributors") - output("Copyright of modifications held by their respective authors.") - output("Licensed under the GNU General Public License v2 (GPL).") + def dispVersion(self, short=False): + if not short: + output("Fail2Ban v" + version) + else: + output(normVersion()) def dispUsage(self): """ Prints Fail2Ban command line options and exits @@ -114,7 +113,7 @@ class Fail2banCmdLine(): output(" --timeout timeout to wait for the server (for internal usage only, don't read configuration)") output(" --str2sec convert time abbreviation format to seconds") output(" -h, --help display this help message") - output(" -V, --version print the version") + output(" -V, --version print the version (-V returns machine-readable short format)") if not caller.endswith('server'): output("") @@ -168,7 +167,7 @@ class Fail2banCmdLine(): self.dispUsage() return True elif o in ["-V", "--version"]: - self.dispVersion() + self.dispVersion(o == "-V") return True return None diff --git a/fail2ban/client/fail2banregex.py b/fail2ban/client/fail2banregex.py index b03c5cf6..6add0eaa 100644 --- a/fail2ban/client/fail2banregex.py +++ b/fail2ban/client/fail2banregex.py @@ -45,7 +45,7 @@ try: # pragma: no cover except ImportError: FilterSystemd = None -from ..version import version +from ..version import version, normVersion from .filterreader import FilterReader from ..server.filter import Filter, FileContainer from ..server.failregex import Regex, RegexException @@ -93,6 +93,10 @@ def journal_lines_gen(flt, myjournal): # pragma: no cover break yield flt.formatJournalEntry(entry) +def dumpNormVersion(*args): + output(normVersion()) + sys.exit(0) + def get_opt_parser(): # use module docstring for help output p = OptionParser( @@ -145,6 +149,8 @@ Report bugs to https://github.com/fail2ban/fail2ban/issues dest="log_level", default='critical', help="Log level for the Fail2Ban logger to use"), + Option('-V', action="callback", callback=dumpNormVersion, + help="get version in machine-readable short format"), Option('-v', '--verbose', action="count", dest="verbose", default=0, help="Increase verbosity"), diff --git a/fail2ban/tests/fail2banclienttestcase.py b/fail2ban/tests/fail2banclienttestcase.py index 5dcbdef5..cde8f4f5 100644 --- a/fail2ban/tests/fail2banclienttestcase.py +++ b/fail2ban/tests/fail2banclienttestcase.py @@ -452,7 +452,10 @@ class Fail2banClientTest(Fail2banClientServerBase): self.assertLogged("Usage: " + CLIENT) self.assertLogged("Report bugs to ") self.pruneLog() - self.execCmd(SUCCESS, (), "-vq", "-V") + self.execCmd(SUCCESS, (), "-V") + self.assertLogged(fail2bancmdline.normVersion()) + self.pruneLog() + self.execCmd(SUCCESS, (), "-vq", "--version") self.assertLogged("Fail2Ban v" + fail2bancmdline.version) self.pruneLog() self.execCmd(SUCCESS, (), "--str2sec", "1d12h30m") diff --git a/fail2ban/tests/fail2banregextestcase.py b/fail2ban/tests/fail2banregextestcase.py index b1786c03..c3919230 100644 --- a/fail2ban/tests/fail2banregextestcase.py +++ b/fail2ban/tests/fail2banregextestcase.py @@ -347,6 +347,11 @@ class Fail2banRegexTest(LogCaptureTestCase): def testExecCmdLine_Usage(self): self.assertNotEqual(_test_exec_command_line(), 0) + self.pruneLog() + self.assertEqual(_test_exec_command_line('-V'), 0) + self.assertLogged(fail2banregex.normVersion()) + self.pruneLog() + self.assertEqual(_test_exec_command_line('--version'), 0) def testExecCmdLine_Direct(self): self.assertEqual(_test_exec_command_line( diff --git a/fail2ban/version.py b/fail2ban/version.py index 41a32469..2a515592 100644 --- a/fail2ban/version.py +++ b/fail2ban/version.py @@ -25,3 +25,7 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2005-2016 Yaroslav Halchenko, __license__ = "GPL-v2+" version = "0.10.4.dev1" + +def normVersion(): + """ Returns fail2ban version in normalized machine-readable format""" + return version.replace('.fix', '').replace('.dev', '.')