Debian Codegraph @

test/sanity/code-smell/deprecated-config.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2018, Matt Martz <[email protected]>
3 #
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18
19 from __future__ import annotations
20
21 import mmap
22 import os
23 import re
24 import sys
25
26 from ansible.module_utils.compat.version import StrictVersion
27
28 import yaml
29
30 import ansible.config
31
32 from ansible.plugins.loader import fragment_loader
33 from ansible.release import __version__ as ansible_version
34 from ansible.utils.plugin_docs import get_docstring
35
36 DOC_RE = re.compile(b'^DOCUMENTATION', flags=re.M)
37 ANSIBLE_MAJOR = StrictVersion('.'.join(ansible_version.split('.')[:2]))
38
39
40 def find_deprecations(obj, path=None):
41 if not isinstance(obj, (list, dict)):
42 return
43
44 try:
45 items = obj.items()
46 except AttributeError:
47 items = enumerate(obj)
48
49 for key, value in items:
50 if path is None:
51 this_path = []
52 else:
53 this_path = path[:]
54
55 this_path.append(key)
56
57 if key != 'deprecated':
58 yield from find_deprecations(value, path=this_path)
59 else:
60 try:
61 version = value['version']
62 this_path.append('version')
63 except KeyError:
64 version = value['removed_in']
65 this_path.append('removed_in')
66 if StrictVersion(version) <= ANSIBLE_MAJOR:
67 yield (this_path, version)
68
69
70 def main():
71 plugins = []
72 for path in sys.argv[1:] or sys.stdin.read().splitlines():
73 with open(path, 'rb') as f:
74 try:
75 mm_file = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
76 except ValueError:
77 continue
78 if DOC_RE.search(mm_file):
79 plugins.append(path)
80 mm_file.close()
81
82 for plugin in plugins:
83 data = {}
84 data['doc'], data['examples'], data['return'], data['metadata'] = get_docstring(filename=os.path.abspath(plugin), fragment_loader=fragment_loader)
85 for result in find_deprecations(data['doc']):
86 print('%s: %s is scheduled for removal in %s' % (plugin, '.'.join(str(i) for i in result[0][:-2]), result[1]))
87
88 base = os.path.join(os.path.dirname(ansible.config.__file__), 'base.yml')
89 root_path = os.path.dirname(os.path.dirname(os.path.dirname(ansible.__file__)))
90 relative_base = os.path.relpath(base, root_path)
91
92 with open(base) as f:
93 data = yaml.safe_load(f)
94
95 for result in find_deprecations(data):
96 print('%s: %s is scheduled for removal in %s' % (relative_base, '.'.join(str(i) for i in result[0][:-2]), result[1]))
97
98
99 if __name__ == '__main__':
100 main()