Debian Codegraph @

ansible_collections/community/routeros/tests/update-docs.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2022, Felix Fontein (@felixfontein) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 '''
9 Updates DOCUMENTATION of modules using module_utils._api_data with the correct list of supported paths.
10 '''
11
12 import sys
13
14 from ansible_collections.community.routeros.plugins.module_utils._api_data import (
15 PATHS,
16 join_path,
17 )
18
19
20 MODULES = [
21 'plugins/modules/api_info.py',
22 'plugins/modules/api_modify.py',
23 ]
24
25
26 def update_file(file: str, begin_line: str, end_line: str, choice_line: str, path_choices: list[str]) -> bool:
27 with open(file, 'r', encoding='utf-8') as f:
28 lines = f.read().splitlines()
29 begin_index = lines.index(begin_line)
30 end_index = lines.index(end_line, begin_index + 1)
31 new_lines = lines[:begin_index + 1] + [choice_line.format(choice=choice) for choice in path_choices] + lines[end_index:]
32 if lines == new_lines:
33 return False
34 print(f'{file} has been updated')
35 with open(file, 'w', encoding='utf-8') as f:
36 f.write('\n'.join(new_lines) + '\n')
37 return True
38
39
40 def main(args: list[str]) -> int:
41 changes = False
42 for file in MODULES:
43 if file == 'plugins/modules/api_modify.py':
44 path_choices = sorted([
45 join_path(path) for path, path_info in PATHS.items()
46 if path_info.fully_understood and not path_info.has_identifier and not path_info.modify_not_supported
47 ])
48 else:
49 path_choices = sorted([join_path(path) for path, path_info in PATHS.items() if path_info.fully_understood])
50 changes |= update_file(file, ' # BEGIN PATH LIST', ' # END PATH LIST', ' - {choice}', path_choices)
51
52 lint = "--lint" in args
53 if not lint or not changes:
54 return 0
55
56 print("Run 'nox -Re update-docs'!")
57 return 1
58
59
60 if __name__ == '__main__':
61 sys.exit(main(sys.argv[1:]))