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 <ssbarnea@redhat.com>
This commit is contained in:
Sorin Sbarnea 2018-06-19 13:33:34 +01:00
parent 51a4ccab70
commit 92f28a591e
No known key found for this signature in database
GPG Key ID: B85725D917D27B8A
4 changed files with 21 additions and 10 deletions

4
jenkins_jobs/__main__.py Normal file
View File

@ -0,0 +1,4 @@
from jenkins_jobs.cli.entry import main
if __name__ == "__main__":
main()

View File

@ -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)

8
tools/test-commands.sh Executable file
View File

@ -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

View File

@ -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