zuul-jobs/roles/golangci-lint/library/golangci_lint_parse_output.py
James E. Blair 9e22cfdb0f Remove shebang from all python ansible modules
This commit in Ansible:
9142be2f6c

now allows Python modules to specify their interpreter with the shebang.
We expect our roles to use the discovered python interpreter on remote
nodes, and on the executor, we need them to use the virtualenv.  Removing
the specific shebang accomplishes this under Ansible 6, and has no effect
under older versions of Ansible.

Without this, for example, the log upload roles would not have access to
their cloud libraries.

Also update our ansible/cli check in our module files.  Many of our modules
can be run from the command line for ease of testing, but the check that we
perform to determine if the module is being invoked from the command line
or Ansible fails on Ansible 5.  Update it to a check that should work in
all 4 versions of Ansible that Zuul uses.

Change-Id: I4e6e85156459cca032e6c3e1d8a9284be919ccca
2022-09-15 13:48:10 -07:00

86 lines
2.6 KiB
Python

# Copyright 2020 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: golangci_lint_parse_output
short_description: Parse the output of golangci-lint and return comments
author: Mohammed Naser (@mnaser)
description:
- Parse the output of golangci-lint and return content for inline comments.
requirements:
- "python >= 3.5"
options:
workdir:
description:
- Path for the project to strip for comments
required: true
type: str
output:
description:
- Output from the golangci-lint command run
required: true
type: str
'''
import re
from ansible.module_utils.basic import AnsibleModule
BUILD_RE = re.compile(r'^.*\[(.*)\]"$')
RE = re.compile(r"^(.*):(\d+):\d+: (.*)$")
def parse_output(output, workdir):
comments = {}
for line in output.split('\n'):
# If we have a build failure, we need to match that first and extract
# the error message. We'll also need to remove 'workdir' as it
# contains the full path.
m = BUILD_RE.match(line)
if m:
line = re.sub(r'\\(.)', r'\1', m.group(1))
# We find everything up until workdir, strip the length of workdir
# and then remove one extra character to remove leading slash.
index = line.find(workdir) + len(workdir) + 1
line = line[index:].replace(workdir, '')
m = RE.match(line)
if m:
file_path = m.group(1)
start_line = m.group(2)
message = m.group(3)
comments.setdefault(file_path, [])
comments[file_path].append(dict(line=int(start_line),
message=message))
return comments
def main():
module = AnsibleModule(
argument_spec=dict(
output=dict(required=True, type='str', no_log=True),
workdir=dict(required=True, str='str'),
)
)
comments = parse_output(module.params['output'], module.params['workdir'])
module.exit_json(changed=False, comments=comments)
if __name__ == '__main__':
main()