summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_util/controller/sanity/code-smell/symlinks.py
blob: 5cffc69e7ba8a9f98d30f781e3f1b060d5866aad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Check for unwanted symbolic links."""
from __future__ import annotations

import os
import sys


def main():
    """Main entry point."""
    root_dir = os.getcwd() + os.path.sep

    for path in sys.argv[1:] or sys.stdin.read().splitlines():
        if not os.path.islink(path.rstrip(os.path.sep)):
            continue

        if not os.path.exists(path):
            print('%s: broken symlinks are not allowed' % path)
            continue

        if path.endswith(os.path.sep):
            print('%s: symlinks to directories are not allowed' % path)
            continue

        real_path = os.path.realpath(path)

        if not real_path.startswith(root_dir):
            print('%s: symlinks outside content tree are not allowed: %s' % (path, os.path.relpath(real_path, os.path.dirname(path))))
            continue


if __name__ == '__main__':
    main()