]> git.proxmox.com Git - ceph.git/blob - ceph/src/script/run_mypy.sh
import 15.2.0 Octopus source
[ceph.git] / ceph / src / script / run_mypy.sh
1 #!/usr/bin/env bash
2
3 # needs to be executed from the src directory.
4 # generates a report at src/mypy_report.txt
5
6 set -e
7
8 python3 -m venv .mypy_venv
9
10 . .mypy_venv/bin/activate
11
12 ! pip install $(find -name requirements.txt -not -path './frontend/*' -printf '-r%p ')
13 pip install mypy
14
15 MYPY_INI="$PWD"/mypy.ini
16
17 export MYPYPATH="$PWD/pybind/rados:$PWD/pybind/rbd:$PWD/pybind/cephfs"
18
19 echo -n > mypy_report.txt
20 pushd pybind
21 mypy --config-file="$MYPY_INI" *.py | awk '{print "pybind/" $0}' >> ../mypy_report.txt
22 popd
23
24 pushd pybind/mgr
25 mypy --config-file="$MYPY_INI" $(find * -name '*.py' | grep -v -e venv -e tox -e env -e gyp -e node_modules) | awk '{print "pybind/mgr/" $0}' >> ../../mypy_report.txt
26 popd
27
28 pushd ceph-volume/ceph_volume
29 mypy --config-file="$MYPY_INI" $(find * -name '*.py' | grep -v -e venv -e tox -e env -e gyp -e node_modules -e tests) | awk '{print "ceph-volume/ceph_volume/" $0}' >> ../../mypy_report.txt
30 popd
31
32 SORT_MYPY=$(cat <<-EOF
33 #!/bin/python3
34 import re
35 from collections import namedtuple
36
37 class Line(namedtuple('Line', 'prefix no rest')):
38 @classmethod
39 def parse(cls, l):
40 if not l:
41 return cls('', 0, '')
42 if re.search('Found [0-9]+ errors in [0-9]+ files', l):
43 return cls('', 0, '')
44 p, *rest = l.split(':', 2)
45 if len(rest) == 1:
46 return cls(p, 0, rest[0])
47 elif len(rest) == 2:
48 try:
49 return cls(p, int(rest[0]), rest[1])
50 except ValueError:
51 return cls(p, 0, rest[0] + ':' + rest[1])
52 assert False, rest
53
54 class Group(object):
55 def __init__(self, line):
56 self.line = line
57 self.lines = []
58
59 def matches(self, other):
60 return Line.parse(self.line).prefix == Line.parse(other).prefix
61
62 def __bool__(self):
63 return bool(self.lines) or ': note: In' not in self.line
64
65 def __str__(self):
66 return '\n'.join([self.line] + self.lines)
67
68 def key(self):
69 l1 = Line.parse(self.line)
70 if l1.no:
71 return l1.prefix, int(l1.no)
72 if not self.lines:
73 return l1.prefix, None
74 return l1.prefix, Line.parse(self.lines[0]).no
75
76 def parse(text):
77 groups = []
78
79 def group():
80 try:
81 return groups[-1]
82 except IndexError:
83 groups.append(Group(''))
84 return groups[-1]
85
86 for l in text:
87 l = l.strip()
88 if ': note: In' in l or not group().matches(l):
89 groups.append(Group(l))
90 elif not l:
91 pass
92 else:
93 group().lines.append(l)
94
95 return (g for g in groups if g)
96
97 def render(groups):
98 groups = sorted(groups, key=Group.key)
99 return '\n'.join(map(str, groups))
100
101 with open('mypy_report.txt') as f:
102 new = render(parse(f))
103 with open('mypy_report.txt', 'w') as f:
104 f.write(new)
105 EOF
106 )
107
108 python <(echo "$SORT_MYPY")