Merge pull request #10 from xoxys/feat_better_logging

Respect PY_COLORS and refactor drone config
This commit is contained in:
Robert Kaussow 2019-03-25 11:12:38 +01:00 committed by GitHub
commit d7b165d4bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 299 additions and 100 deletions

View File

@ -1,9 +1,12 @@
local PythonVersions(pyversion="2.7", py="27") = {
name: "python" + pyversion,
name: "python" + pyversion + "-ansible",
image: "python:" + pyversion,
pull: "always",
environment: {
PY_COLORS: 1
},
commands: [
"pip install tox -q",
"pip install tox -qq",
"tox -e $(tox -l | grep py" + py + " | xargs | sed 's/ /,/g') -q",
],
depends_on: [
@ -23,7 +26,62 @@ local PipelineTesting = {
PythonVersions(pyversion="3.5", py="35"),
PythonVersions(pyversion="3.6", py="36"),
PythonVersions(pyversion="3.7", py="37"),
{
name: "python-flake8",
image: "python:3.7",
pull: "always",
environment: {
PY_COLORS: 1
},
commands: [
"pip install -r test-requirements.txt -qq",
"pip install -qq .",
"flake8 ./ansiblelater",
],
depends_on: [
"clone",
],
},
{
name: "python-bandit",
image: "python:3.7",
pull: "always",
environment: {
PY_COLORS: 1
},
commands: [
"pip install -r test-requirements.txt -qq",
"pip install -qq .",
"bandit -r ./ansiblelater",
],
depends_on: [
"clone",
],
},
{
name: "codecov",
image: "python:3.7",
pull: "always",
environment: {
PY_COLORS: 1,
CODECOV_TOKEN: { "from_secret": "codecov_token" },
},
commands: [
"pip install codecov",
"coverage combine .tox/py*/.coverage",
"codecov --required"
],
depends_on: [
"python2.7-ansible",
"python3.5-ansible",
"python3.6-ansible",
"python3.7-ansible"
],
}
],
trigger: {
ref: ["refs/heads/master", "refs/tags/**", "refs/pull/**"],
},
};
local PipelineBuild = {
@ -61,6 +119,11 @@ local PipelineBuild = {
detach_sign: true,
files: [ "dist/*" ],
},
when: {
event: {
exclude: ['pull_request'],
},
},
},
{
name: "publish-github",
@ -68,7 +131,10 @@ local PipelineBuild = {
pull: "always",
settings: {
api_key: { "from_secret": "github_token"},
overwrite: true,
files: ["dist/*", "sha256sum.txt"],
title: "${DRONE_TAG}",
note: "CHANGELOG.md",
},
when: {
event: [ "tag" ],
@ -92,6 +158,9 @@ local PipelineBuild = {
depends_on: [
"testing",
],
trigger: {
ref: ["refs/heads/master", "refs/tags/**", "refs/pull/**"],
},
};
local PipelineNotifications = {
@ -118,7 +187,7 @@ local PipelineNotifications = {
"build",
],
trigger: {
event: [ "push", "tag" ],
ref: ["refs/heads/master", "refs/tags/**"],
status: [ "success", "failure" ],
},
};

View File

@ -7,42 +7,97 @@ platform:
arch: amd64
steps:
- name: python2.7
- name: python2.7-ansible
pull: always
image: python:2.7
commands:
- pip install tox -q
- pip install tox -qq
- "tox -e $(tox -l | grep py27 | xargs | sed 's/ /,/g') -q"
environment:
PY_COLORS: 1
depends_on:
- clone
- name: python3.5
- name: python3.5-ansible
pull: always
image: python:3.5
commands:
- pip install tox -q
- pip install tox -qq
- "tox -e $(tox -l | grep py35 | xargs | sed 's/ /,/g') -q"
environment:
PY_COLORS: 1
depends_on:
- clone
- name: python3.6
- name: python3.6-ansible
pull: always
image: python:3.6
commands:
- pip install tox -q
- pip install tox -qq
- "tox -e $(tox -l | grep py36 | xargs | sed 's/ /,/g') -q"
environment:
PY_COLORS: 1
depends_on:
- clone
- name: python3.7
- name: python3.7-ansible
pull: always
image: python:3.7
commands:
- pip install tox -q
- pip install tox -qq
- "tox -e $(tox -l | grep py37 | xargs | sed 's/ /,/g') -q"
environment:
PY_COLORS: 1
depends_on:
- clone
- name: python-flake8
pull: always
image: python:3.7
commands:
- pip install -r test-requirements.txt -qq
- pip install -qq .
- flake8 ./ansiblelater
environment:
PY_COLORS: 1
depends_on:
- clone
- name: python-bandit
pull: always
image: python:3.7
commands:
- pip install -r test-requirements.txt -qq
- pip install -qq .
- bandit -r ./ansiblelater
environment:
PY_COLORS: 1
depends_on:
- clone
- name: codecov
pull: always
image: python:3.7
commands:
- pip install codecov
- "coverage combine .tox/py*/.coverage"
- codecov --required
environment:
CODECOV_TOKEN:
from_secret: codecov_token
PY_COLORS: 1
depends_on:
- python2.7-ansible
- python3.5-ansible
- python3.6-ansible
- python3.7-ansible
trigger:
ref:
- refs/heads/master
- "refs/tags/**"
- "refs/pull/**"
---
kind: pipeline
name: build
@ -76,6 +131,10 @@ steps:
from_secret: gpgsign_key
passphrase:
from_secret: gpgsign_passphrase
when:
event:
exclude:
- pull_request
- name: publish-github
pull: always
@ -86,6 +145,9 @@ steps:
files:
- "dist/*"
- sha256sum.txt
note: CHANGELOG.md
overwrite: true
title: "${DRONE_TAG}"
when:
event:
- tag
@ -104,6 +166,12 @@ steps:
event:
- tag
trigger:
ref:
- refs/heads/master
- "refs/tags/**"
- "refs/pull/**"
depends_on:
- testing
@ -128,9 +196,9 @@ steps:
from_secret: matrix_username
trigger:
event:
- push
- tag
ref:
- refs/heads/master
- "refs/tags/**"
status:
- success
- failure

View File

@ -1,3 +1,2 @@
- BUGFIX
- Controls like `when` used at block level will be added as metadata to each task inside this block [#9](https://github.com/xoxys/ansible-later/issues/9)
- Commands can be used in place of modules if the task use register [#8](https://github.com/xoxys/ansible-later/issues/8)
- ENHANCEMENT
- Respect `PY_COLORS` to get colorized output for nontty environments ([#10](https://github.com/xoxys/ansible-later/pull/10))

View File

@ -4,6 +4,7 @@
[![](https://img.shields.io/pypi/pyversions/ansible-later.svg)](https://pypi.org/project/ansible-later/)
[![](https://img.shields.io/pypi/status/ansible-later.svg)](https://pypi.org/project/ansible-later/)
[![](https://img.shields.io/pypi/v/ansible-later.svg)](https://pypi.org/project/ansible-later/)
[![codecov](https://codecov.io/gh/xoxys/ansible-later/branch/master/graph/badge.svg)](https://codecov.io/gh/xoxys/ansible-later)
This is a fork of Will Thames [ansible-review](https://github.com/willthames/ansible-review) so credits goes to him
for his work on ansible-review and ansible-lint.
@ -18,6 +19,8 @@ The project name is an acronym for **L**ovely **A**utomation **TE**sting f**R**m
## Table of Content
- [ansible-later](#ansible-later)
- [Table of Content](#table-of-content)
- [Setup](#setup)
- [Using pip](#using-pip)
- [From source](#from-source)

View File

@ -3,42 +3,51 @@ from __future__ import print_function
import importlib
import logging
import os
import subprocess
import sys
import re
import colorama
from distutils.version import LooseVersion
from ansible.module_utils.parsing.convert_bool import boolean as to_bool
try:
import ConfigParser as configparser
except ImportError:
import configparser
try:
from ansible.utils.color import stringc
except ImportError:
from ansible.color import stringc
# from yamlhelper import *
def should_do_markup():
py_colors = os.environ.get('PY_COLORS', None)
if py_colors is not None:
return to_bool(py_colors, strict=False)
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
colorama.init(autoreset=True, strip=not should_do_markup())
def abort(message, file=sys.stderr):
print(stringc("FATAL: %s" % message, 'red'), file=file)
return color_text(colorama.Fore.RED, "FATAL: {}".format(message))
sys.exit(1)
def error(message, file=sys.stderr):
print(stringc("ERROR: %s" % message, 'red'), file=file)
return color_text(colorama.Fore.RED, "ERROR: {}".format(message))
def warn(message, settings, file=sys.stdout):
if settings.log_level <= logging.WARNING:
print(stringc("WARN: %s" % message, 'yellow'), file=file)
return color_text(colorama.Fore.YELLOW, "WARN: {}".format(message))
def info(message, settings, file=sys.stdout):
if settings.log_level <= logging.INFO:
print(stringc("INFO: %s" % message, 'green'), file=file)
return color_text(colorama.Fore.BLUE, "INFO: {}".format(message))
def color_text(color, msg):
print('{}{}{}'.format(color, msg, colorama.Style.RESET_ALL))
def count_spaces(c_string):
@ -104,18 +113,6 @@ def read_config(config_file):
return Settings(config, config_file)
def execute(cmd):
result = ExecuteResult()
encoding = 'UTF-8'
env = dict(os.environ)
env['PYTHONIOENCODING'] = encoding
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=env)
result.output = proc.communicate()[0].decode(encoding)
result.rc = proc.returncode
return result
class Settings(object):
def __init__(self, config, config_file):
self.rulesdir = None
@ -130,7 +127,3 @@ class Settings(object):
self.custom_modules = [x.strip() for x in modules.split(',')]
self.configfile = config_file
class ExecuteResult(object):
pass

View File

@ -1,3 +0,0 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

View File

@ -6,5 +6,10 @@ license_file = LICENSE
universal = 1
[flake8]
ignore = E501, W503, F401, N813
max-line-length = 100
exclude = .git,.hg,.svn,test,setup.py,__pycache__
exclude = .git,.tox,__pycache__,build,dist,tests,*.pyc,*.egg-info,.cache,.eggs
[tool:pytest]
filterwarnings =
ignore:.*collections.*:DeprecationWarning

View File

@ -65,7 +65,8 @@ setup(
"unidiff",
"flake8",
"yamllint",
"nested-lookup"
"nested-lookup",
"colorama"
],
entry_points={
'console_scripts': [

8
test-requirements.txt Normal file
View File

@ -0,0 +1,8 @@
flake8
flake8-colors
pep8-naming
wheel
pytest
pytest-mock
pytest-cov
bandit

0
tests/__init__.py Normal file
View File

View File

@ -0,0 +1,82 @@
from __future__ import print_function
import colorama
import logging
from ansiblelater.utils import info, warn, abort, error, should_do_markup
def test_abort(capsys, mocker):
abort('foo')
stdout, _ = capsys.readouterr()
print('{}{}{}'.format(colorama.Fore.RED, 'FATAL: foo'.rstrip(),
colorama.Style.RESET_ALL))
x, _ = capsys.readouterr()
assert x == stdout
def test_error(capsys, mocker):
error('foo')
stdout, _ = capsys.readouterr()
print('{}{}{}'.format(colorama.Fore.RED, 'ERROR: foo'.rstrip(),
colorama.Style.RESET_ALL))
x, _ = capsys.readouterr()
assert x == stdout
def test_warn(capsys, mocker):
settings = mocker.MagicMock()
settings.log_level = getattr(logging, 'WARNING')
warn('foo', settings)
stdout, _ = capsys.readouterr()
print('{}{}{}'.format(colorama.Fore.YELLOW, 'WARN: foo'.rstrip(),
colorama.Style.RESET_ALL))
x, _ = capsys.readouterr()
assert x == stdout
def test_info(capsys, mocker):
settings = mocker.MagicMock()
settings.log_level = getattr(logging, 'INFO')
info('foo', settings)
stdout, _ = capsys.readouterr()
print('{}{}{}'.format(colorama.Fore.BLUE, 'INFO: foo'.rstrip(),
colorama.Style.RESET_ALL))
x, _ = capsys.readouterr()
assert x == stdout
def test_markup_detection_pycolors0(monkeypatch):
monkeypatch.setenv('PY_COLORS', '0')
assert not should_do_markup()
def test_markup_detection_pycolors1(monkeypatch):
monkeypatch.setenv('PY_COLORS', '1')
assert should_do_markup()
def test_markup_detection_tty_yes(mocker):
mocker.patch('sys.stdout.isatty', return_value=True)
mocker.patch('os.environ', {'TERM': 'xterm'})
assert should_do_markup()
mocker.resetall()
mocker.stopall()
def test_markup_detection_tty_no(mocker):
mocker.patch('os.environ', {})
mocker.patch('sys.stdout.isatty', return_value=False)
assert not should_do_markup()
mocker.resetall()
mocker.stopall()

50
tox.ini
View File

@ -1,46 +1,20 @@
[tox]
minversion = 3.5
envlist = py27-ansible{21},py{27,35,36,37}-ansible{22,23,24,25,26,27},py{27,35,36,37}-flake8
minversion = 3.7.0
envlist =
py{27,35,36,37}-ansible{25,26,27,devel}
isolated_build = True
[testenv]
usedevelop = True
passenv = PY_COLORS
setenv = COVERAGE_FILE={envdir}/.coverage
deps =
flake8
pep8-naming
wheel
flake8-colors
pytest
ansible21: ansible>=2.1,<2.2
ansible22: ansible>=2.2,<2.3
ansible23: ansible>=2.3,<2.4
ansible24: ansible>=2.4,<2.5
-rtest-requirements.txt
ansible25: ansible>=2.5,<2.6
ansible26: ansible>=2.6,<2.7
ansible27: ansible>=2.7,<2.8
commands = ansible-later -c tests/config/config.ini tests/data/yaml_success.yml
passenv = HOME
[testenv:py27-flake8]
commands = flake8 ansiblelater
usedevelop = True
[testenv:py36-flake8]
commands = flake8 ansiblelater
usedevelop = True
[flake8]
ignore = E501, W503, F401, N813
exclude =
.tox,
.git,
__pycache__,
build,
dist,
tests/fixtures/*,
*.pyc,
*.egg-info,
.cache,
.eggs
format = %(path)s:%(row)d:%(col)d: ${red}%(code)s %(text)s${reset}
ansibledevel: git+https://github.com/ansible/ansible.git
commands =
ansible-later --help
pytest tests/ --cov={toxinidir}/ansiblelater/ --no-cov-on-fail