aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/testing/style/rules.py
blob: 1f4d283769d8798cf7e42e4054ac7daed338ea1f (plain) (blame)
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
33
34
35
36
37
38
39
40
import os
import six


def style_required(path, data, skip_links=True):
    if get_skip_reason(path, data, skip_links):
        return False
    return True


def get_skip_reason(path, data, skip_links=True):
    return _path_skip_reason(path, skip_links) or _content_skip_reason(path, data)


def _path_skip_reason(path, skip_links=True):
    if '/generated/' in path:
        return "path '{}' contains '/generated/'".format(path)

    if path and '/contrib/' in path:
        return "path '{}' contains '/contrib/'".format(path)

    if path and '/vendor/' in path:
        return "path '{}' contains '/vendor/'".format(path)

    if skip_links and os.path.islink(path):
        return "path '{}' is a symlink".format(path)


def _content_skip_reason(path, data):
    if not isinstance(data, six.string_types):
        data = data.decode()

    for substr in [
        '# DO_NOT_STYLE',
        '// DO_NOT_STYLE',
        'THIS SOFTWARE',
        'WITHOUT WARRANTY',
    ]:
        if substr in data:
            return "file '{}' contains '{}'".format(path, substr)