-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy path__main__.py
192 lines (153 loc) · 5.04 KB
/
__main__.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
"""
Generate coverage badges for Coverage.py.
"""
import os
import sys
import argparse
import pkg_resources
import coverage
__version__ = '1.1.2'
DEFAULT_COLOR = '#a4a61d'
COLORS = {
'brightgreen': '#4c1',
'green': '#97CA00',
'yellowgreen': '#a4a61d',
'yellow': '#dfb317',
'orange': '#fe7d37',
'red': '#e05d44',
'lightgrey': '#9f9f9f',
}
COLOR_RANGES = [
(95, 'brightgreen'),
(90, 'green'),
(75, 'yellowgreen'),
(60, 'yellow'),
(40, 'orange'),
(0, 'red'),
]
class Devnull(object):
"""
A file like object that does nothing.
"""
def write(self, *args, **kwargs):
pass
def get_total():
"""
Return the rounded total as properly rounded string.
"""
cov = coverage.Coverage()
cov.load()
total = cov.report(file=Devnull())
if hasattr(coverage.results.Numbers, 'set_precision'): # Coverage <= 5
class Precision(coverage.results.Numbers):
"""
A class for using the percentage rounding of the main coverage package,
with any percentage.
To get the string format of the percentage, use the ``pc_covered_str``
property.
"""
def __init__(self, percent):
self.percent = percent
@property
def pc_covered(self):
return self.percent
return Precision(total).pc_covered_str
elif hasattr(coverage.results.Numbers, 'display_covered'): # Coverage 6.x < 7.5
# NOTE: Precision is no longer set globally in the
# `coverage.results.Numbers` class. Instead the precision must be
# passed in as the first argument. We pull the precision from the
# `coverage.Coverage` object because it should pull the correct
# precision from the local .coveragerc file.
return coverage.results.Numbers(precision=cov.config.precision).display_covered(total)
else: # Coverage >= 7.5
return coverage.results.display_covered(total, cov.config.precision)
def get_color(total):
"""
Return color for current coverage precent
"""
try:
xtotal = int(total)
except ValueError:
return COLORS['lightgrey']
for range_, color in COLOR_RANGES:
if xtotal >= range_:
return COLORS[color]
def get_badge(total, color=DEFAULT_COLOR):
"""
Read the SVG template from the package, update total, return SVG as a
string.
"""
template_path = os.path.join('templates', 'flat.svg')
template = pkg_resources.resource_string(__name__, template_path).decode('utf8')
return template.replace('{{ total }}', total).replace('{{ color }}', color)
def parse_args(argv=None):
"""
Parse the command line arguments.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-o', dest='filepath',
help='Save the file to the specified path.')
parser.add_argument('-p', dest='plain_color', action='store_true',
help='Plain color mode. Standard green badge.')
parser.add_argument('-f', dest='force', action='store_true',
help='Force overwrite image, use with -o key.')
parser.add_argument('-q', dest='quiet', action='store_true',
help='Don\'t output any non-error messages.')
parser.add_argument('-v', dest='print_version', action='store_true',
help='Show version.')
# If arguments have been passed in, use them.
if argv:
return parser.parse_args(argv)
# Otherwise, just use sys.argv directly.
else:
return parser.parse_args()
def save_badge(badge, filepath, force=False):
"""
Save badge to the specified path.
"""
# Validate path (part 1)
if filepath.endswith('/'):
print('Error: Filepath may not be a directory.')
sys.exit(1)
# Get absolute filepath
path = os.path.abspath(filepath)
if not path.lower().endswith('.svg'):
path += '.svg'
# Validate path (part 2)
if not force and os.path.exists(path):
print('Error: "{}" already exists.'.format(path))
sys.exit(1)
# Write file
with open(path, 'w') as f:
f.write(badge)
return path
def main(argv=None):
"""
Console scripts entry point.
"""
args = parse_args(argv)
# Print version
if args.print_version:
print('coverage-badge v{}'.format(__version__))
sys.exit(0)
# Check for coverage
if coverage is None:
print('Error: Python coverage module not installed.')
sys.exit(1)
# Generate badge
try:
total = get_total()
except coverage.misc.CoverageException as e:
print('Error: {} Did you run coverage first?'.format(e))
sys.exit(1)
color = DEFAULT_COLOR if args.plain_color else get_color(total)
badge = get_badge(total, color)
# Show or save output
if args.filepath:
path = save_badge(badge, args.filepath, args.force)
if not args.quiet:
print('Saved badge to {}'.format(path))
else:
print(badge, end='')
if __name__ == '__main__':
main()