Debian Codegraph @

test/sanity/code-smell/no-unwanted-files.py

raw
1 """Prevent unwanted files from being added to the source tree."""
2
3 from __future__ import annotations
4
5 import os
6 import sys
7
8
9 def main():
10 """Main entry point."""
11 paths = sys.argv[1:] or sys.stdin.read().splitlines()
12
13 allowed_extensions = {
14 '.cs',
15 '.ps1',
16 '.psm1',
17 '.py',
18 }
19
20 skip_paths = {
21 'lib/ansible/config/ansible_builtin_runtime.yml', # not included in the sanity ignore file since it won't exist until after migration
22 }
23
24 skip_directories = {
25 'lib/ansible/galaxy/data/',
26 }
27
28 allow_yaml = {
29 'lib/ansible/plugins/test',
30 'lib/ansible/plugins/filter',
31 'lib/ansible/_internal/ansible_collections',
32 }
33
34 for path in paths:
35 if path in skip_paths:
36 continue
37
38 if any(path.startswith(skip_directory) for skip_directory in skip_directories):
39 continue
40
41 if path.startswith('lib/') and not path.startswith('lib/ansible/'):
42 print('%s: all "lib" content must reside in the "lib/ansible" directory' % path)
43 continue
44
45 ext = os.path.splitext(path)[1]
46 if ext in ('.yml',) and any(path.startswith(yaml_directory) for yaml_directory in allow_yaml):
47 continue
48
49 if ext not in allowed_extensions:
50 print('%s: extension must be one of: %s' % (path, ', '.join(allowed_extensions)))
51
52
53 if __name__ == '__main__':
54 main()