summaryrefslogtreecommitdiffstats
path: root/library/python/testing/style/rules.py
blob: dde87a45b3bea06d7e93c656f14b2d5f956384fa (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
41
42
# XXX: This module is used in linter tools in `tools/` which are run by test_tool.
# test_tool and `tools/` have different release cycles. Beware when modifying.
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 and '/.yandex_meta/' not 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 WARRANT',  # WARRANTY, WARRANTIES
    ]:
        if substr in data:
            return "file '{}' contains '{}'".format(path, substr)