2021-07-25 08:00:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (c) Ansible Project
|
2022-08-05 10:28:29 +00:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-07-25 08:00:10 +00:00
|
|
|
"""Check BOTMETA file."""
|
|
|
|
|
2024-12-21 15:49:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-07-25 08:00:10 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2023-02-12 20:05:08 +00:00
|
|
|
from voluptuous import Any, MultipleInvalid, PREVENT_EXTRA, Schema
|
2021-07-25 08:00:10 +00:00
|
|
|
from voluptuous.humanize import humanize_error
|
|
|
|
|
|
|
|
|
2021-07-27 18:26:26 +00:00
|
|
|
IGNORE_NO_MAINTAINERS = [
|
2023-12-11 18:09:57 +00:00
|
|
|
'docs/docsite/rst/filter_guide.rst',
|
|
|
|
'docs/docsite/rst/filter_guide_abstract_informations.rst',
|
|
|
|
'docs/docsite/rst/filter_guide_paths.rst',
|
|
|
|
'docs/docsite/rst/filter_guide_selecting_json_data.rst',
|
2021-07-27 18:26:26 +00:00
|
|
|
'plugins/cache/memcached.py',
|
|
|
|
'plugins/cache/redis.py',
|
|
|
|
'plugins/callback/cgroup_memory_recap.py',
|
|
|
|
'plugins/callback/context_demo.py',
|
|
|
|
'plugins/callback/counter_enabled.py',
|
|
|
|
'plugins/callback/jabber.py',
|
|
|
|
'plugins/callback/log_plays.py',
|
|
|
|
'plugins/callback/logdna.py',
|
|
|
|
'plugins/callback/logentries.py',
|
|
|
|
'plugins/callback/null.py',
|
|
|
|
'plugins/callback/selective.py',
|
|
|
|
'plugins/callback/slack.py',
|
|
|
|
'plugins/callback/splunk.py',
|
|
|
|
'plugins/callback/yaml.py',
|
|
|
|
'plugins/inventory/nmap.py',
|
|
|
|
'plugins/inventory/virtualbox.py',
|
|
|
|
'plugins/connection/chroot.py',
|
|
|
|
'plugins/connection/iocage.py',
|
|
|
|
'plugins/connection/lxc.py',
|
|
|
|
'plugins/lookup/cartesian.py',
|
|
|
|
'plugins/lookup/chef_databag.py',
|
|
|
|
'plugins/lookup/consul_kv.py',
|
|
|
|
'plugins/lookup/credstash.py',
|
|
|
|
'plugins/lookup/cyberarkpassword.py',
|
|
|
|
'plugins/lookup/flattened.py',
|
|
|
|
'plugins/lookup/keyring.py',
|
|
|
|
'plugins/lookup/lastpass.py',
|
|
|
|
'plugins/lookup/passwordstore.py',
|
|
|
|
'plugins/lookup/shelvefile.py',
|
|
|
|
'plugins/filter/json_query.py',
|
|
|
|
'plugins/filter/random_mac.py',
|
|
|
|
]
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
FILENAME = '.github/BOTMETA.yml'
|
|
|
|
|
|
|
|
LIST_ENTRIES = frozenset(('supershipit', 'maintainers', 'labels', 'keywords', 'notify', 'ignore'))
|
|
|
|
|
2022-01-05 21:54:21 +00:00
|
|
|
AUTHOR_REGEX = re.compile(r'^\w.*\(@([\w-]+)\)(?![\w.])')
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_authors(filename):
|
|
|
|
data = {}
|
|
|
|
try:
|
2024-12-21 15:49:23 +00:00
|
|
|
documentation = []
|
|
|
|
in_docs = False
|
|
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
|
|
|
for line in f:
|
|
|
|
if line.startswith('DOCUMENTATION ='):
|
|
|
|
in_docs = True
|
|
|
|
elif line.startswith(("'''", '"""')) and in_docs:
|
|
|
|
in_docs = False
|
|
|
|
elif in_docs:
|
|
|
|
documentation.append(line)
|
|
|
|
if in_docs:
|
|
|
|
print(f'{filename}: cannot find DOCUMENTATION end')
|
|
|
|
return []
|
|
|
|
if not documentation:
|
|
|
|
print(f'{filename}: cannot find DOCUMENTATION')
|
|
|
|
return []
|
|
|
|
|
|
|
|
data = yaml.safe_load('\n'.join(documentation))
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{filename}:0:0: Cannot load DOCUMENTATION: {e}')
|
2021-07-25 08:00:10 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
author = data.get('author') or []
|
|
|
|
if isinstance(author, str):
|
|
|
|
author = [author]
|
|
|
|
return author
|
|
|
|
|
|
|
|
|
2021-07-26 09:44:41 +00:00
|
|
|
def extract_author_name(author):
|
|
|
|
m = AUTHOR_REGEX.match(author)
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
if author == 'Ansible Core Team':
|
|
|
|
return '$team_ansible_core'
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-07-25 08:00:10 +00:00
|
|
|
def validate(filename, filedata):
|
2021-07-26 09:44:41 +00:00
|
|
|
if not filename.startswith('plugins/'):
|
|
|
|
return
|
|
|
|
if filename.startswith(('plugins/doc_fragments/', 'plugins/module_utils/')):
|
2021-07-25 08:00:10 +00:00
|
|
|
return
|
2024-12-21 15:49:23 +00:00
|
|
|
# Compile list of all active and inactive maintainers
|
2021-07-25 08:00:10 +00:00
|
|
|
all_maintainers = filedata['maintainers'] + filedata['ignore']
|
2024-12-21 15:49:23 +00:00
|
|
|
if not filename.startswith(('plugins/action/', 'plugins/doc_fragments/', 'plugins/filter/', 'plugins/module_utils/', 'plugins/plugin_utils/')):
|
2021-07-26 14:54:00 +00:00
|
|
|
maintainers = read_authors(filename)
|
|
|
|
for maintainer in maintainers:
|
|
|
|
maintainer = extract_author_name(maintainer)
|
|
|
|
if maintainer is not None and maintainer not in all_maintainers:
|
2024-12-21 15:49:23 +00:00
|
|
|
others = ', '.join(all_maintainers)
|
|
|
|
msg = f'Author {maintainer} not mentioned as active or inactive maintainer for {filename} (mentioned are: {others})'
|
|
|
|
print(f'{FILENAME}:0:0: {msg}')
|
2021-07-27 18:26:26 +00:00
|
|
|
should_have_no_maintainer = filename in IGNORE_NO_MAINTAINERS
|
|
|
|
if not all_maintainers and not should_have_no_maintainer:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: No (active or inactive) maintainer mentioned for {filename}')
|
2021-07-27 18:26:26 +00:00
|
|
|
if all_maintainers and should_have_no_maintainer:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: Please remove {filename} from the ignore list of {sys.argv[0]}')
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main entry point."""
|
|
|
|
try:
|
|
|
|
with open(FILENAME, 'rb') as f:
|
|
|
|
botmeta = yaml.safe_load(f)
|
|
|
|
except yaml.error.MarkedYAMLError as ex:
|
2024-12-21 15:49:23 +00:00
|
|
|
msg = re.sub(r'\s+', ' ', str(ex))
|
|
|
|
print('f{FILENAME}:{ex.context_mark.line + 1}:{ex.context_mark.column + 1}: YAML load failed: {msg}')
|
2021-07-25 08:00:10 +00:00
|
|
|
return
|
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
2024-12-21 15:49:23 +00:00
|
|
|
msg = re.sub(r'\s+', ' ', str(ex))
|
|
|
|
print(f'{FILENAME}:0:0: YAML load failed: {msg}')
|
2021-07-25 08:00:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Validate schema
|
|
|
|
|
|
|
|
MacroSchema = Schema({
|
2021-07-26 09:44:41 +00:00
|
|
|
(str): Any(str, None),
|
2021-07-25 08:00:10 +00:00
|
|
|
}, extra=PREVENT_EXTRA)
|
|
|
|
|
|
|
|
FilesSchema = Schema({
|
|
|
|
(str): {
|
|
|
|
('supershipit'): str,
|
|
|
|
('support'): Any('community'),
|
|
|
|
('maintainers'): str,
|
|
|
|
('labels'): str,
|
|
|
|
('keywords'): str,
|
|
|
|
('notify'): str,
|
|
|
|
('ignore'): str,
|
|
|
|
},
|
|
|
|
}, extra=PREVENT_EXTRA)
|
|
|
|
|
|
|
|
schema = Schema({
|
2021-09-28 20:39:34 +00:00
|
|
|
('notifications'): bool,
|
2021-07-25 08:00:10 +00:00
|
|
|
('automerge'): bool,
|
|
|
|
('macros'): MacroSchema,
|
|
|
|
('files'): FilesSchema,
|
|
|
|
}, extra=PREVENT_EXTRA)
|
|
|
|
|
|
|
|
try:
|
|
|
|
schema(botmeta)
|
|
|
|
except MultipleInvalid as ex:
|
|
|
|
for error in ex.errors:
|
|
|
|
# No way to get line/column numbers
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: {humanize_error(botmeta, error)}')
|
2021-07-25 08:00:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Preprocess (substitute macros, convert to lists)
|
|
|
|
macros = botmeta.get('macros') or {}
|
|
|
|
macro_re = re.compile(r'\$([a-zA-Z_]+)')
|
|
|
|
|
|
|
|
def convert_macros(text, macros):
|
|
|
|
def f(m):
|
2021-07-26 09:44:41 +00:00
|
|
|
macro = m.group(1)
|
|
|
|
replacement = (macros[macro] or '')
|
|
|
|
if macro == 'team_ansible_core':
|
2024-12-21 15:49:23 +00:00
|
|
|
return f'$team_ansible_core {replacement}'
|
2021-07-26 09:44:41 +00:00
|
|
|
return replacement
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
return macro_re.sub(f, text)
|
|
|
|
|
|
|
|
files = {}
|
|
|
|
try:
|
|
|
|
for file, filedata in (botmeta.get('files') or {}).items():
|
|
|
|
file = convert_macros(file, macros)
|
2024-09-01 18:22:53 +00:00
|
|
|
filedata = {k: convert_macros(v, macros) for k, v in filedata.items()}
|
2021-07-25 08:00:10 +00:00
|
|
|
files[file] = filedata
|
|
|
|
for k, v in filedata.items():
|
|
|
|
if k in LIST_ENTRIES:
|
|
|
|
filedata[k] = v.split()
|
|
|
|
except KeyError as e:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: Found unknown macro {e}')
|
2021-07-25 08:00:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Scan all files
|
2021-07-26 09:44:41 +00:00
|
|
|
unmatched = set(files)
|
2023-12-11 18:09:57 +00:00
|
|
|
for dirs in ('docs/docsite/rst', 'plugins', 'tests', 'changelogs'):
|
2024-12-21 15:49:23 +00:00
|
|
|
for dirpath, _dirnames, filenames in os.walk(dirs):
|
2021-07-26 09:44:41 +00:00
|
|
|
for file in sorted(filenames):
|
|
|
|
if file.endswith('.pyc'):
|
|
|
|
continue
|
|
|
|
filename = os.path.join(dirpath, file)
|
|
|
|
if os.path.islink(filename):
|
|
|
|
continue
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
matching_files = []
|
|
|
|
for file, filedata in files.items():
|
|
|
|
if filename.startswith(file):
|
|
|
|
matching_files.append((file, filedata))
|
|
|
|
if file in unmatched:
|
|
|
|
unmatched.remove(file)
|
|
|
|
if not matching_files:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: Did not find any entry for {filename}')
|
2021-07-26 09:44:41 +00:00
|
|
|
|
|
|
|
matching_files.sort(key=lambda kv: kv[0])
|
2024-12-21 15:49:23 +00:00
|
|
|
filedata = {}
|
2021-07-26 09:44:41 +00:00
|
|
|
for k in LIST_ENTRIES:
|
|
|
|
filedata[k] = []
|
|
|
|
for dummy, data in matching_files:
|
|
|
|
for k, v in data.items():
|
|
|
|
if k in LIST_ENTRIES:
|
|
|
|
v = filedata[k] + v
|
|
|
|
filedata[k] = v
|
|
|
|
validate(filename, filedata)
|
|
|
|
|
|
|
|
for file in unmatched:
|
2024-12-21 15:49:23 +00:00
|
|
|
print(f'{FILENAME}:0:0: Entry {file} was not used')
|
2021-07-25 08:00:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|