-
Notifications
You must be signed in to change notification settings - Fork 35
/
badge_update.py
executable file
·212 lines (175 loc) · 6.19 KB
/
badge_update.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
# Copyright 2015-2020 by Christopher C. Little.
# This file is part of Abydos.
#
# Abydos is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Abydos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
"""badge_update.py.
This updates the Pylint, pydocstyle, flake8, & SLOCCount badges in README.rst.
"""
import os.path
import re
BADGE_COLORS = (
'brightgreen',
'green',
'yellowgreen',
'yellow',
'orange',
'red',
)
def pylint_color(score):
"""Return Pylint badge color.
Parameters
----------
score : float
A Pylint score
Returns
-------
str
Badge color
"""
# These are the score cutoffs for each color above.
# I.e. score==10 -> brightgreen, down to 7.5 > score >= 5 -> orange
score_cutoffs = (10, 9.5, 8.5, 7.5, 5)
for i in range(len(score_cutoffs)):
if score >= score_cutoffs[i]:
return BADGE_COLORS[i]
# and score < 5 -> red
return BADGE_COLORS[-1]
# def pycodestyle_color(score):
# """Return pycodestyle badge color."""
# # These are the score cutoffs for each color above.
# # I.e. score==0 -> brightgreen, down to 100 < score <= 200 -> orange
# score_cutoffs = (0, 20, 50, 100, 200)
# for i in range(len(score_cutoffs)):
# if score <= score_cutoffs[i]:
# return BADGE_COLORS[i]
# # and score > 200 -> red
# return BADGE_COLORS[-1]
def pydocstyle_color(score):
"""Return pydocstyle badge color.
Parameters
----------
score : float
A pydocstyle score
Returns
-------
str
Badge color
"""
# These are the score cutoffs for each color above.
# I.e. score==0 -> brightgreen, down to 100 < score <= 200 -> orange
score_cutoffs = (0, 10, 25, 50, 100)
for i in range(len(score_cutoffs)):
if score <= score_cutoffs[i]:
return BADGE_COLORS[i]
# and score > 200 -> red
return BADGE_COLORS[-1]
def flake8_color(score):
"""Return flake8 badge color.
Parameters
----------
score : float
A flake8 score
Returns
-------
str
Badge color
"""
# These are the score cutoffs for each color above.
# I.e. score==0 -> brightgreen, down to 100 < score <= 200 -> orange
score_cutoffs = (0, 20, 50, 100, 200)
for i in range(len(score_cutoffs)):
if score <= score_cutoffs[i]:
return BADGE_COLORS[i]
# and score > 200 -> red
return BADGE_COLORS[-1]
if __name__ == '__main__':
if not os.path.isfile('./README.rst'):
exit('README.rst file missing.')
if not os.path.isfile('./pylint.log'):
exit('Please direct Pylint output to pylint.log')
pylint_text = open('pylint.log', 'r', encoding='utf-8').read()
pylint_score = max(
float(
re.search(
'Your code has been rated at' + r' (-?[0-9\.]+)', pylint_text
).group(1)
),
0.0,
)
# if not os.path.isfile('./pycodestyle.log'):
# exit('Please direct pycodestyle output to pycodestyle.log')
# pycodestyle_text = open('pycodestyle.log', 'r', encoding='utf-8').read()
# pycodestyle_score = sum(int(n) for n in re.findall(r'\n([0-9]+) +',
# pycodestyle_text))
if not os.path.isfile('./pydocstyle.log'):
exit('Please direct pydocstyle output to pydocstyle.log')
pydocstyle_score = int(
open('pydocstyle.log', 'r', encoding='utf-8').read().split()[-1]
)
if not os.path.isfile('./flake8.log'):
exit('Please direct flake8 output to flake8.log')
flake8_score = int(
open('flake8.log', 'r', encoding='utf-8').read().split()[-1]
)
if not os.path.isfile('./sloccount.log'):
exit('Please direct sloccount output to sloccount.log')
with open('sloccount.log', 'r', encoding='utf-8') as fh:
pattern = r'Total Physical Source Lines of Code \(SLOC\) += ([0-9,]+)'
sloccount = re.search(pattern, fh.read()).group(1)
if not os.path.isfile('./mypy/index.txt'):
exit('Please direct mypy txt report to mypy/index.txt')
with open('mypy/index.txt', 'r', encoding='utf-8') as fh:
pattern = r'\| Total +?\| +?([0-9.]+)'
imprecision = re.search(pattern, fh.read()).group(1)
readme_text = open('README.rst', 'r', encoding='utf-8').read()
prefix = 'https://img.shields.io/badge/Pylint-'
readme_text = re.sub(
prefix + r'(-?[0-9\.]+/10-[a-z]+)',
prefix + str(pylint_score) + '/10-' + pylint_color(pylint_score),
readme_text,
1,
)
# prefix = 'https://img.shields.io/badge/pycodestyle-'
# readme_text = re.sub(prefix + r'([0-9\.]+-[a-z]+)',
# prefix + str(pycodestyle_score) + '-' +
# pycodestyle_color(pycodestyle_score),
# readme_text, 1)
prefix = 'https://img.shields.io/badge/pydocstyle-'
readme_text = re.sub(
prefix + r'([0-9\.]+-[a-z]+)',
prefix
+ str(pydocstyle_score)
+ '-'
+ pydocstyle_color(pydocstyle_score),
readme_text,
1,
)
prefix = 'https://img.shields.io/badge/flake8-'
readme_text = re.sub(
prefix + r'([0-9\.]+-[a-z]+)',
prefix + str(flake8_score) + '-' + flake8_color(flake8_score),
readme_text,
1,
)
prefix = 'https://img.shields.io/badge/SLOCCount-'
readme_text = re.sub(
prefix + r'[0-9,]+', prefix + str(sloccount), readme_text, 1
)
prefix = 'https://img.shields.io/badge/mypy-'
readme_text = re.sub(
prefix + r'[0-9.]+', prefix + str(imprecision), readme_text, 1
)
with open('README.rst', 'w', encoding='utf-8') as f:
f.write(readme_text)