From 92f28a591e835096379c4b9c18ecc95201b7bc43 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 19 Jun 2018 13:33:34 +0100 Subject: [PATCH] Allow jjb to be called as a module Enable us to do `python -m jenkins_jobs ...` which avoids the need to install executable in user path. Tests cli execution on each supported python version. Uses default encoding workaround only on python2, where is needed. Change-Id: I4cd79fd51a8309d532e0e76723ecfbbda3e1ca6f Signed-off-by: Sorin Sbarnea --- jenkins_jobs/__main__.py | 4 ++++ jenkins_jobs/cli/entry.py | 17 +++++++---------- tools/test-commands.sh | 8 ++++++++ tox.ini | 2 ++ 4 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 jenkins_jobs/__main__.py create mode 100755 tools/test-commands.sh diff --git a/jenkins_jobs/__main__.py b/jenkins_jobs/__main__.py new file mode 100644 index 000000000..8171c6148 --- /dev/null +++ b/jenkins_jobs/__main__.py @@ -0,0 +1,4 @@ +from jenkins_jobs.cli.entry import main + +if __name__ == "__main__": + main() diff --git a/jenkins_jobs/cli/entry.py b/jenkins_jobs/cli/entry.py index 484b7a3d6..888c21c13 100644 --- a/jenkins_jobs/cli/entry.py +++ b/jenkins_jobs/cli/entry.py @@ -17,7 +17,6 @@ import io import os import logging import platform -import sys from stevedore import extension import yaml @@ -27,9 +26,6 @@ from jenkins_jobs.config import JJBConfig from jenkins_jobs import utils from jenkins_jobs import version -if sys.version_info[0] != 2: - from importlib import reload - logging.basicConfig(level=logging.INFO) logger = logging.getLogger() @@ -147,14 +143,15 @@ def main(): # utf-8 workaround for avoiding unicode errors in stdout/stderr # see https://stackoverflow.com/a/2001767/99834 - import codecs import sys - reload(sys) - sys.setdefaultencoding('utf-8') - sys.stdout = codecs.getwriter('utf8')(sys.stdout) - sys.stderr = codecs.getwriter('utf8')(sys.stderr) - # end of workaround + if sys.version_info[0] == 2: + import codecs + reload(sys) # noqa + sys.setdefaultencoding('utf-8') + sys.stdout = codecs.getwriter('utf8')(sys.stdout) + sys.stderr = codecs.getwriter('utf8')(sys.stderr) + # end of workaround argv = sys.argv[1:] jjb = JenkinsJobs(argv) diff --git a/tools/test-commands.sh b/tools/test-commands.sh new file mode 100755 index 000000000..f44c46b04 --- /dev/null +++ b/tools/test-commands.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -exou pipefail + +VAL1=$(jenkins-jobs --version 2>&1) || exit 1 +VAL2=$(python -m jenkins_jobs --version 2>&1) || exit 2 + +# we assure that both calling methods to get the same output +[ "${VAL1}" == "${VAL2}" ] || exit 3 diff --git a/tox.ini b/tox.ini index 769fc992b..127f86751 100644 --- a/tox.ini +++ b/tox.ini @@ -18,6 +18,8 @@ deps = -r{toxinidir}/test-requirements.txt commands = - find . -type f -name "*.pyc" -delete - find . -type d -name "__pycache__" -delete + # test that we can call jjb using both variants with same results + bash {toxinidir}/tools/test-commands.sh stestr run --slowest {posargs} whitelist_externals = bash