-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
executable file
·112 lines (91 loc) · 3.71 KB
/
setup.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
#!/usr/bin/env python
import os
import os.path
import sys
import warnings
from decimal import Decimal
from distutils.cmd import Command
from typing import Optional
from setuptools import setup
from setuptools.command.install import install
# NOTE: This is left here as legacy. It's unclear as of 12/23 if there's a way to
# move these commands to the pyproject.toml file
__version__: Optional[str] = None
# Read in and set version variable without the overhead/requirements
# of the rest of the package.
#
# https://milkr.io/kfei/5-common-patterns-to-version-your-Python-package/5
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, 'records_mover', 'version.py')) as f:
exec(f.read())
assert __version__ is not None
# From https://circleci.com/blog/continuously-deploying-python-packages-to-pypi-with-circleci/
class VerifyVersionCommand(install):
"""Custom command to verify that the git tag matches our version"""
description = 'verify that the git tag matches our version'
def run(self) -> None:
tag = os.getenv('CIRCLE_TAG')
tag_formatted_version = f'v{__version__}'
if tag != tag_formatted_version:
info = "Git tag: {0} does not match the version of this app: {1}".format(
tag, __version__
)
sys.exit(info)
class CoverageRatchetCommand(Command):
description = 'Run coverage ratchet'
user_options = [] # type: ignore
coverage_file: str
coverage_source_file: str
coverage_url: str
type_of_coverage: str
def finalize_options(self) -> None:
pass
def run(self) -> None:
"""Run command."""
import xml.etree.ElementTree as ET
tree = ET.parse(self.coverage_source_file)
new_coverage = Decimal(tree.getroot().attrib["line-rate"]) * 100
if not os.path.exists(self.coverage_file):
with open(self.coverage_file, 'w') as f:
f.write('0')
with open(self.coverage_file, 'r') as f:
high_water_mark = Decimal(f.read())
if new_coverage < high_water_mark:
raise Exception(
f"{self.type_of_coverage} coverage used to be {high_water_mark}; "
f"down to {new_coverage}%. Fix by viewing '{self.coverage_url}'")
elif new_coverage > high_water_mark:
warnings.warn(
f"WARNING: Your current coverage is higher than current high watermark.\n"
f"Consider increasing high watermark in {self.coverage_file} to {new_coverage}.")
else:
print(f"Code coverage steady at {new_coverage}%")
class TestCoverageRatchetCommand(CoverageRatchetCommand):
def initialize_options(self) -> None:
"""Set default values for options."""
self.type_of_coverage = 'Test'
self.coverage_url = 'cover/index.html'
self.coverage_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'metrics',
'coverage_high_water_mark'
)
self.coverage_source_file = "coverage.xml"
class MypyCoverageRatchetCommand(CoverageRatchetCommand):
def initialize_options(self) -> None:
"""Set default values for options."""
self.type_of_coverage = 'Mypy'
self.coverage_url = 'typecover/index.html'
self.coverage_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'metrics',
'mypy_high_water_mark'
)
self.coverage_source_file = "typecover/cobertura.xml"
setup(version=__version__,
cmdclass={
'coverage_ratchet': TestCoverageRatchetCommand,
'mypy_ratchet': MypyCoverageRatchetCommand,
'verify': VerifyVersionCommand,
},
)