aboutsummaryrefslogtreecommitdiff
path: root/setup.py
blob: a4f43c33e8e0594fc9c3f0644c935682bcf161ec (plain)
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
#!/usr/bin/env python

import os
import math
import shutil
import subprocess
from distutils import log
from distutils.core import Command
from setuptools.command.test import test
from distutils.errors import DistutilsError
from setuptools import setup, find_packages


class TestsWithCoverage(test):

    description = "run unit tests with coverage"

    coverage_goal = 100
    missed_branches_goal = 0
    partial_branches_goal = 0

    def initialize_options(self):
        super().initialize_options()
        self.missed_coverage_goals = False

    def enforce_coverage_goals(self, rel_path, analysis):
        # There is no coverage goal for the player package, just the API
        if os.path.split(rel_path)[0] == "pydora":
            return

        coverage_percent = math.ceil(analysis.numbers.pc_covered)
        if coverage_percent != self.coverage_goal:
            self.missed_coverage_goals = True
            self.announce(
                "Coverage: {!r} coverage is {}%, goal  is {}%".format(
                    rel_path, coverage_percent, self.coverage_goal), log.ERROR)

        missed_branches = analysis.numbers.n_missing_branches
        if missed_branches != self.missed_branches_goal:
            self.missed_coverage_goals = True
            self.announce(
                "Coverage: {!r} missed branch count is {}, goal is {}".format(
                    rel_path, missed_branches, self.missed_branches_goal),
                log.ERROR)

        partially_covered_branches = analysis.numbers.n_partial_branches
        if partially_covered_branches != self.partial_branches_goal:
            self.missed_coverage_goals = True
            self.announce(
                "Coverage: {!r} partial branch count is {}, goal is {}".format(
                    rel_path, partially_covered_branches,
                    self.partial_branches_goal), log.ERROR)

    def run(self):
        from coverage import Coverage

        cov = Coverage(source=self.distribution.packages, branch=True)

        cov.start()
        super().run()
        cov.stop()

        # Save HTML report for debugging missed coverage
        cov.html_report()

        # Print coverage report to console for CI log
        cov.report()

        for rep in cov._get_file_reporters():
            self.enforce_coverage_goals(rep.relname, cov._analyze(rep))

        if self.missed_coverage_goals:
            raise DistutilsError("Project missed coverage goals")


class PyPiReleaseCommand(Command):

    user_options = []
    description = "build and release artifacts to pypi"

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def venv_run(self, cmd, *args):
        subprocess.check_call((os.path.join(".release/py3/bin", cmd),) + args)

    def make_release_tree(self):
        if not os.path.exists(".release"):
            log.info("Creating temp release tree")
            os.mkdir(".release")

    def configure_environment(self):
        log.info("Configuring release environment")
        subprocess.check_call(["python3", "-m", "venv", ".release/py3"])
        self.venv_run("pip", "install", "-U",
                      "pip", "setuptools", "virtualenv", "twine")

    def build_py3_artifact(self):
        log.info("Building Python 3 Artifact")
        self.venv_run("python", "setup.py",
                      "release", "bdist_wheel", "--python-tag", "py3")

    def build_sdist_artifact(self):
        log.info("Building Source Dist Artifact")
        self.venv_run("python", "setup.py", "sdist")

    def upload_artifacts(self):
        log.info("Uploading artifacts to PyPi")
        self.venv_run("twine", "upload", "dist/*")

    def cleanup(self):
        log.info("Cleaning up temp release tree")
        shutil.rmtree(".release")

    def run(self):
        try:
            self.make_release_tree()
            self.configure_environment()
            self.build_py3_artifact()
            self.build_sdist_artifact()
            self.upload_artifacts()
        finally:
            self.cleanup()


setup(
    name="pydora",
    version="2.0.0",
    description="Python wrapper for Pandora API",
    long_description=open("README.rst", "r").read(),
    author="Mike Crute",
    author_email="mike@crute.us",
    url="https://github.com/mcrute/pydora",
    test_suite="tests.discover_suite",
    packages=find_packages(exclude=["tests", "tests.*"]),
    cmdclass={
        "test": TestsWithCoverage,
        "pypi_release": PyPiReleaseCommand,
    },
    entry_points={
        "console_scripts": [
            "pydora = pydora.player:main",
            "pydora-configure = pydora.configure:main",
        ],
    },
    setup_requires=[
        "wheel",
        "flake8>=3.3",
        "setuptools>=36.0.1",
        "coverage>=4.1,<5",
    ],
    install_requires=[
        "requests>=2,<3",
        "blowfish>=0.6.1,<1.0",
    ],
    python_requires=">=3.5",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "Intended Audience :: End Users/Desktop",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3 :: Only",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Multimedia :: Sound/Audio :: Players",
    ]
)