From 7668ec7c383ce0158956fcb8ea118941593ac837 Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Sun, 29 Mar 2020 17:07:54 -0400 Subject: [PATCH] Revert "Revert "Extract pep8 messages for inline comments"" This reverts commit b35f47190a0674e44e4b5d38b8062fc717fb4cf0. Change-Id: I71cd0520ff62c41e4e3ebe9eb9e8a3839147726e --- roles/tox/library/tox_parse_output.py | 92 +++++++++++++++++++++++++++ roles/tox/tasks/main.yaml | 23 +++++++ 2 files changed, 115 insertions(+) create mode 100644 roles/tox/library/tox_parse_output.py diff --git a/roles/tox/library/tox_parse_output.py b/roles/tox/library/tox_parse_output.py new file mode 100644 index 000000000..e522cbd5a --- /dev/null +++ b/roles/tox/library/tox_parse_output.py @@ -0,0 +1,92 @@ +#!/usr/bin/python + +# Copyright (c) 2018 Red Hat +# +# This module 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 3 of the License, or +# (at your option) any later version. +# +# This software 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 this software. If not, see . +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: tox_parse_output +short_description: Parses the output of tox looking for per-line comments +author: Monty Taylor (@mordred) +description: + - Looks for output from the tox command to find content that could be + returned as inline comments. +requirements: + - "python >= 3.5" +options: + tox_output: + description: + - Output from the tox command run + required: true + type: str +''' + +import re + +from ansible.module_utils.basic import AnsibleModule + +PEP8_RE = re.compile(r"^(.*):(\d+):(\d+): (.*)$") + + +def extract_pep8_comments(line): + file_path = None + start_line = None + message = None + + m = PEP8_RE.match(line) + if m: + file_path = m.group(1) + start_line = m.group(2) + message = m.group(4) + + return file_path, start_line, message + + +def extract_file_comments(tox_output): + ret = {} + for line in tox_output.split('\n'): + try: + if not line: + continue + if line[0].isspace(): + continue + file_path, start_line, message = extract_pep8_comments(line) + # Clean up the file path if it has a leading ./ + if file_path.startswith('./'): + file_path = file_path[2:] + ret.setdefault(file_path, []) + ret[file_path].append(dict(line=int(start_line), + message=message)) + except Exception: + pass + return ret + + +def main(): + module = AnsibleModule( + argument_spec=dict( + tox_output=dict(required=True, type='str'), + ) + ) + tox_output = module.params['tox_output'] + + file_comments = extract_file_comments(tox_output) + module.exit_json(changed=False, file_comments=file_comments) + + +if __name__ == '__main__': + main() diff --git a/roles/tox/tasks/main.yaml b/roles/tox/tasks/main.yaml index ea828f033..fcead40d3 100644 --- a/roles/tox/tasks/main.yaml +++ b/roles/tox/tasks/main.yaml @@ -38,3 +38,26 @@ chdir: "{{ zuul_work_dir }}" environment: "{{ tox_environment|combine(tox_constraints_env|default({})) }}" command: "{{ tox_executable }} -e{{ tox_envlist }} {{ tox_extra_args }}" + failed_when: false + register: tox_output + +- name: Look for output + tox_parse_output: + tox_output: '{{ tox_output.stdout }}' + register: file_comments + +- name: Return file comments to Zuul + when: file_comments.file_comments + delegate_to: localhost + zuul_return: + data: + zuul: + file_comments: '{{ file_comments.file_comments }}' + tags: + # Avoid "no action detected in task" linter error + - skip_ansible_lint + +- name: Return tox status + fail: + msg: 'tox exited with return code {{ tox_output.rc }}' + when: tox_output.rc != 0