streamlit

Форк
0
141 строка · 4.1 Кб
1
#!/usr/bin/env python
2

3
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16

17
"""Process Mypy line precision report into useful statistics."""
18

19
import glob
20
import itertools
21
import os
22
import os.path
23
import shlex
24
import sys
25
import tempfile
26
from typing import Iterable, List
27

28
import click
29
import mypy.main as mypy_main
30

31
PATHS = ["lib/streamlit/", "scripts/*", "e2e/scripts/*"]
32
EXCLUDE_FILES = {
33
    os.path.join("scripts", "add_license_headers.py"),
34
    os.path.join("e2e", "scripts", "st_reuse_label.py"),
35
    os.path.join("scripts", "license-template.txt"),
36
}
37

38

39
def shlex_join(split_command: Iterable[str]):
40
    """Return a shell-escaped string from *split_command*.
41

42
    This function is backported from Python 3.8 - shlex.join
43
    """
44
    return ' '.join(shlex.quote(arg) for arg in split_command)
45

46

47
class Module:
48
    _COLUMNS = (56, 5, 5, 7)
49
    _HEADERS = ("Module", "Lines", "Typed", "Percent")
50

51
    def __init__(self, name: str, lines: int = 0, precise: int = 0):
52
        self.name = name
53
        self.lines = lines
54
        self.precise = precise
55

56
    @classmethod
57
    def header(cls) -> str:
58
        fmt = "%-*s  %-*s  %-*s  %-*s"
59
        return ("%s\n%s" % (fmt, fmt)) % tuple(
60
            itertools.chain(
61
                *zip(cls._COLUMNS, cls._HEADERS),
62
                *zip(cls._COLUMNS, ["-" * len(h) for h in cls._HEADERS]),
63
            )
64
        )
65

66
    def __str__(self) -> str:
67
        cols = self._COLUMNS
68
        return "%-*s  %*d  %*d % 7.02f%%" % (
69
            cols[0],
70
            self.name,
71
            cols[1],
72
            self.lines,
73
            cols[2],
74
            self.precise,
75
            self.precise * 100 / self.lines,
76
        )
77

78

79
def process_report(path: str) -> None:
80
    modules: List[Module] = []
81
    totals = Module("Total")
82

83
    with open(path) as f:
84
        for line in f.readlines()[2:]:  # Skip two header lines.
85
            parts = line.split()
86
            name = parts[0]
87
            if name.endswith("_pb2"):
88
                continue
89
            total_lines = int(parts[1])
90
            empty_lines = int(parts[5])
91
            lines = total_lines - empty_lines
92
            if not lines:
93
                continue
94
            precise = int(parts[2])
95
            modules.append(Module(name, lines, precise))
96
            totals.lines += lines
97
            totals.precise += precise
98

99
    print(Module.header())
100
    for module in sorted(modules, key=lambda m: m.name):
101
        print(str(module))
102
    print(str(totals))
103

104

105
@click.command()
106
@click.option("--report", is_flag=True, help="Emit line coverage report for all files")
107
@click.option(
108
  "--verbose",
109
  '-v',
110
  is_flag=True,
111
  help=(
112
    "Verbose mode. Causes this command to print mypy command being executed."
113
  )
114
)
115
def main(report: bool = False, verbose: bool = False) -> None:
116
    paths: List[str] = []
117
    for path in PATHS:
118
        if "*" in path:
119
            for filename in glob.glob(path):
120
                if not (filename in EXCLUDE_FILES or filename.endswith(".sh")):
121
                    paths.append(filename)
122
        else:
123
            paths.append(path)
124

125
    args = ["--config-file=lib/mypy.ini", "--namespace-packages"]
126
    if report:
127
        tempdir = tempfile.TemporaryDirectory()
128
        args.append("--lineprecision-report=%s" % tempdir.name)
129
    args.append("--")
130
    args.extend(paths)
131

132
    if verbose:
133
        shell_command = shlex_join(itertools.chain(['mypy'], args))
134
        print("Executing command:", shell_command)
135
    mypy_main.main(stdout=sys.stdout, stderr=sys.stderr, args=args)
136
    if report:
137
        process_report(os.path.join(tempdir.name, "lineprecision.txt"))
138

139

140
if __name__ == "__main__":
141
    main()
142

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.