|
| 1 | +# |
| 2 | +# coding=utf-8 |
| 3 | +"""Development related tasks to be run with 'invoke'. |
| 4 | +
|
| 5 | +Make sure you satisfy the following Python module requirements if you are trying to publish a release to PyPI: |
| 6 | + - twine >= 1.11.0 |
| 7 | + - wheel >= 0.31.0 |
| 8 | + - setuptools >= 39.1.0 |
| 9 | +""" |
| 10 | +import os |
| 11 | +import shutil |
| 12 | + |
| 13 | +import invoke |
| 14 | + |
| 15 | +# shared function |
| 16 | +def rmrf(items, verbose=True): |
| 17 | + "Silently remove a list of directories or files" |
| 18 | + if isinstance(items, str): |
| 19 | + items = [items] |
| 20 | + |
| 21 | + for item in items: |
| 22 | + if verbose: |
| 23 | + print("Removing {}".format(item)) |
| 24 | + shutil.rmtree(item, ignore_errors=True) |
| 25 | + # rmtree doesn't remove bare files |
| 26 | + try: |
| 27 | + os.remove(item) |
| 28 | + except FileNotFoundError: |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +# create namespaces |
| 33 | +namespace = invoke.Collection() |
| 34 | +namespace_clean = invoke.Collection('clean') |
| 35 | +namespace.add_collection(namespace_clean, 'clean') |
| 36 | + |
| 37 | +##### |
| 38 | +# |
| 39 | +# pytest, tox, pylint, and codecov |
| 40 | +# |
| 41 | +##### |
| 42 | +@invoke.task |
| 43 | +def pytest(context): |
| 44 | + "Run tests and code coverage using pytest" |
| 45 | + context.run("pytest --cov=tableformatter --cov-report=term --cov-report=html") |
| 46 | +namespace.add_task(pytest) |
| 47 | + |
| 48 | +@invoke.task |
| 49 | +def pytest_clean(context): |
| 50 | + "Remove pytest cache and code coverage files and directories" |
| 51 | + #pylint: disable=unused-argument |
| 52 | + dirs = ['.pytest_cache', '.cache', 'htmlcov', '.coverage'] |
| 53 | + rmrf(dirs) |
| 54 | +namespace_clean.add_task(pytest_clean, 'pytest') |
| 55 | + |
| 56 | +@invoke.task |
| 57 | +def mypy(context): |
| 58 | + "Run mypy optional static type checker" |
| 59 | + context.run("mypy main.py") |
| 60 | + namespace.add_task(mypy) |
| 61 | +namespace.add_task(mypy) |
| 62 | + |
| 63 | +@invoke.task |
| 64 | +def mypy_clean(context): |
| 65 | + "Remove mypy cache directory" |
| 66 | + #pylint: disable=unused-argument |
| 67 | + dirs = ['.mypy_cache'] |
| 68 | + rmrf(dirs) |
| 69 | +namespace_clean.add_task(mypy_clean, 'mypy') |
| 70 | + |
| 71 | +@invoke.task |
| 72 | +def tox(context): |
| 73 | + "Run unit and integration tests on multiple python versions using tox" |
| 74 | + context.run("tox") |
| 75 | +namespace.add_task(tox) |
| 76 | + |
| 77 | +@invoke.task |
| 78 | +def tox_clean(context): |
| 79 | + "Remove tox virtualenvs and logs" |
| 80 | + #pylint: disable=unused-argument |
| 81 | + rmrf('.tox') |
| 82 | +namespace_clean.add_task(tox_clean, 'tox') |
| 83 | + |
| 84 | + |
| 85 | +##### |
| 86 | +# |
| 87 | +# documentation |
| 88 | +# |
| 89 | +##### |
| 90 | +DOCS_SRCDIR = 'docs' |
| 91 | +DOCS_BUILDDIR = os.path.join('docs', '_build') |
| 92 | + |
| 93 | +@invoke.task() |
| 94 | +def docs(context, builder='html'): |
| 95 | + "Build documentation using sphinx" |
| 96 | + cmdline = 'python -msphinx -M {} {} {}'.format(builder, DOCS_SRCDIR, DOCS_BUILDDIR) |
| 97 | + context.run(cmdline) |
| 98 | +namespace.add_task(docs) |
| 99 | + |
| 100 | +@invoke.task |
| 101 | +def docs_clean(context): |
| 102 | + "Remove rendered documentation" |
| 103 | + #pylint: disable=unused-argument |
| 104 | + rmrf(DOCS_BUILDDIR) |
| 105 | +namespace_clean.add_task(docs_clean, name='docs') |
| 106 | + |
| 107 | +@invoke.task |
| 108 | +def livehtml(context): |
| 109 | + "Launch webserver on http://localhost:8000 with rendered documentation" |
| 110 | + builder = 'html' |
| 111 | + outputdir = os.path.join(DOCS_BUILDDIR, builder) |
| 112 | + cmdline = 'sphinx-autobuild -b {} {} {}'.format(builder, DOCS_SRCDIR, outputdir) |
| 113 | + context.run(cmdline, pty=True) |
| 114 | +namespace.add_task(livehtml) |
| 115 | + |
| 116 | + |
| 117 | +##### |
| 118 | +# |
| 119 | +# build and distribute |
| 120 | +# |
| 121 | +##### |
| 122 | +BUILDDIR = 'build' |
| 123 | +DISTDIR = 'dist' |
| 124 | + |
| 125 | +@invoke.task |
| 126 | +def build_clean(context): |
| 127 | + "Remove the build directory" |
| 128 | + #pylint: disable=unused-argument |
| 129 | + rmrf(BUILDDIR) |
| 130 | +namespace_clean.add_task(build_clean, 'build') |
| 131 | + |
| 132 | +@invoke.task |
| 133 | +def dist_clean(context): |
| 134 | + "Remove the dist directory" |
| 135 | + #pylint: disable=unused-argument |
| 136 | + rmrf(DISTDIR) |
| 137 | +namespace_clean.add_task(dist_clean, 'dist') |
| 138 | + |
| 139 | +@invoke.task |
| 140 | +def eggs_clean(context): |
| 141 | + "Remove egg directories" |
| 142 | + #pylint: disable=unused-argument |
| 143 | + dirs = set() |
| 144 | + dirs.add('.eggs') |
| 145 | + for name in os.listdir(os.curdir): |
| 146 | + if name.endswith('.egg-info'): |
| 147 | + dirs.add(name) |
| 148 | + if name.endswith('.egg'): |
| 149 | + dirs.add(name) |
| 150 | + rmrf(dirs) |
| 151 | +namespace_clean.add_task(eggs_clean, 'eggs') |
| 152 | + |
| 153 | +@invoke.task |
| 154 | +def pycache_clean(context): |
| 155 | + "Remove __pycache__ directories" |
| 156 | + #pylint: disable=unused-argument |
| 157 | + dirs = set() |
| 158 | + for root, dirnames, _ in os.walk(os.curdir): |
| 159 | + if '__pycache__' in dirnames: |
| 160 | + dirs.add(os.path.join(root, '__pycache__')) |
| 161 | + print("Removing __pycache__ directories") |
| 162 | + rmrf(dirs, verbose=False) |
| 163 | +namespace_clean.add_task(pycache_clean, 'pycache') |
| 164 | + |
| 165 | +# |
| 166 | +# make a dummy clean task which runs all the tasks in the clean namespace |
| 167 | +clean_tasks = list(namespace_clean.tasks.values()) |
| 168 | +@invoke.task(pre=list(namespace_clean.tasks.values()), default=True) |
| 169 | +def clean_all(context): |
| 170 | + "Run all clean tasks" |
| 171 | + #pylint: disable=unused-argument |
| 172 | + pass |
| 173 | +namespace_clean.add_task(clean_all, 'all') |
| 174 | + |
| 175 | +@invoke.task(pre=[clean_all]) |
| 176 | +def sdist(context): |
| 177 | + "Create a source distribution" |
| 178 | + context.run('python setup.py sdist') |
| 179 | +namespace.add_task(sdist) |
| 180 | + |
| 181 | +@invoke.task(pre=[clean_all]) |
| 182 | +def wheel(context): |
| 183 | + "Build a wheel distribution" |
| 184 | + context.run('python setup.py bdist_wheel') |
| 185 | +namespace.add_task(wheel) |
| 186 | + |
| 187 | +@invoke.task(pre=[sdist, wheel]) |
| 188 | +def pypi(context): |
| 189 | + "Build and upload a distribution to pypi" |
| 190 | + context.run('twine upload dist/*') |
| 191 | +namespace.add_task(pypi) |
| 192 | + |
| 193 | +@invoke.task(pre=[sdist, wheel]) |
| 194 | +def pypi_test(context): |
| 195 | + "Build and upload a distribution to https://test.pypi.org" |
| 196 | + context.run('twine upload --repository-url https://test.pypi.org/legacy/ dist/*') |
| 197 | +namespace.add_task(pypi_test) |
| 198 | + |
0 commit comments