Skip to content

Commit

Permalink
Chapter 7 code companion: CI pipeline (#4)
Browse files Browse the repository at this point in the history
* Start chapter 7 code companion

* Add missing runs-on key

* Update working-directory key

* Add cibuildwheel

* Add working-directory key to cibuildwheel

* Update path for artifacts

* Add python_requires to setup.cfg

* Move python_requires to options section

* Try removing quotes

* Use Python 3.10 for type checking

* Try explicit tox installation

* Add setup-python to all jobs

* Add source dist job and install tox directly

* Fix install command

* Correct black versions

* Use consistent list syntax

* Specify Python version for all jobs

* Add publishing job

* Try single quotes

* Bump package version

* Add artifact name during download

* Use explicit path
  • Loading branch information
daneah authored Dec 25, 2021
1 parent 9c6b086 commit 0012d0a
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 1 deletion.
150 changes: 150 additions & 0 deletions .github/workflows/ch07-packaging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: Packaging (chapter 7)

on:
- push

jobs:
format:
name: Check formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install tox
run: python -m pip install tox

- name: Run black
run: tox -e format
working-directory: ch07/first-python-package # You don't need this in your package

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install tox
run: python -m pip install tox

- name: Run flake8
run: tox -e lint
working-directory: ch07/first-python-package # You don't need this in your package

typecheck:
name: Type check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install tox
run: python -m pip install tox

- name: Run mypy
run: python -m tox -e typecheck
working-directory: ch07/first-python-package # You don't need this in your package

test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
python:
- version: "3.10"
toxenv: "py310"
- version: "3.9"
toxenv: "py39"
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python.version }}

- name: Install tox
run: python -m pip install tox

- name: Run pytest
run: tox -e ${{ matrix.python.toxenv }}
working-directory: ch07/first-python-package # You don't need this in your package

build_source_dist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install build
run: python -m pip install build

- name: Run build
run: python -m build --sdist
working-directory: ch07/first-python-package # You don't need this in your package

- uses: actions/upload-artifact@v2
with:
path: ch07/first-python-package/dist/*.tar.gz

build_wheels:
name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, windows-2019, macOS-10.15]

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install cibuildwheel
run: python -m pip install cibuildwheel==2.3.1

- name: Build wheels
run: python -m cibuildwheel --output-dir wheels
working-directory: ch07/first-python-package # You don't need this in your package

- uses: actions/upload-artifact@v2
with:
path: ./ch07/first-python-package/wheels/*.whl # Update to match root of package

publish:
name: Publish package
if: startsWith(github.event.ref, 'refs/tags/v')
needs:
- format
- lint
- typecheck
- test
- build_source_dist
- build_wheels
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v2
with:
name: artifact
path: ./ch07/first-python-package/dist # Update to match root of package

- uses: pypa/gh-action-pypi-publish@v1.4.2
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: ./ch07/first-python-package/dist/ # You don't need this in your package
2 changes: 1 addition & 1 deletion ch06/first-python-package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build-backend = "setuptools.build_meta"

[tool.black]
line-length = 120
target-version = ['py38', 'py39']
target-version = ['py39', 'py310']
7 changes: 7 additions & 0 deletions ch07/first-python-package/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2021 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions ch07/first-python-package/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graft src
recursive-exclude __pycache__ *.py[cod]
9 changes: 9 additions & 0 deletions ch07/first-python-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# first-python-package

This package does amazing things.

## Installation

```shell
$ python -m pip install first-python-package
```
7 changes: 7 additions & 0 deletions ch07/first-python-package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["setuptools", "wheel", "cython"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 120
target-version = ['py39', 'py310']
96 changes: 96 additions & 0 deletions ch07/first-python-package/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[metadata]
name = pubpypack-harmony-dane-hillard
version = 0.0.2
description = This package does amazing things.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/daneah/publishing-python-packages
author = Dane Hillard
author_email = "Dane Hillard" <github@danehillard.com>
license = MIT
license_files = LICENSE
classifiers =
License :: OSI Approved :: MIT License

[options]
python_requires = >=3.9
package_dir =
=src
packages = find:
include_package_data = True
install_requires =
termcolor>=1.1.0,<2

[options.packages.find]
where = src
exclude =
test*

[options.entry_points]
console_scripts =
harmony = imppkg.harmony:main

######################
# Tool configuration #
######################

[mypy]
python_version = 3.10
warn_unused_configs = True
show_error_context = True
pretty = True
namespace_packages = True

[flake8]
max-line-length = 120

[tool:pytest]
testpaths = test
addopts = --cov --strict-markers
xfail_strict = True

[coverage:run]
source = imppkg
branch = True

[coverage:report]
show_missing = True
skip_covered = True

[coverage:paths]
source =
src/imppkg
.venv/*/site-packages/imppkg

[tox:tox]
envlist = py39,py310
isolated_build = True

[testenv]
deps =
pytest
pytest-cov
commands =
pytest {posargs}

[testenv:typecheck]
deps =
mypy
pytest
types-termcolor
commands =
mypy --ignore-missing-imports {posargs:src test}

[testenv:format]
skip_install = True
deps =
black
commands =
black {posargs:--check --diff src test}

[testenv:lint]
skip_install = True
deps =
flake8
commands =
flake8 {posargs:src test}
7 changes: 7 additions & 0 deletions ch07/first-python-package/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup

from Cython.Build import cythonize

setup(
ext_modules=cythonize("src/imppkg/harmonic_mean.pyx"),
)
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions ch07/first-python-package/src/imppkg/harmonic_mean.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def harmonic_mean(nums):
return len(nums) / sum(1 / num for num in nums)
29 changes: 29 additions & 0 deletions ch07/first-python-package/src/imppkg/harmony.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys

from termcolor import colored

from imppkg.harmonic_mean import harmonic_mean


def _parse_nums(inputs: str) -> list[float]:
try:
return [float(num) for num in inputs]
except ValueError:
return []


def _calculate_result(nums: list[float]) -> float:
try:
return harmonic_mean(nums)
except ZeroDivisionError:
return 0.0


def _format_output(result: float) -> str:
return colored(str(result), "red", "on_cyan", attrs=["bold"])


def main():
nums = _parse_nums(sys.argv[1:])
result = _calculate_result(nums)
print(_format_output(result))
Empty file.
32 changes: 32 additions & 0 deletions ch07/first-python-package/test/test_harmonic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys

import pytest
from termcolor import colored

from imppkg.harmony import main


@pytest.mark.parametrize(
"inputs, expected",
[
(["3", "3", "3"], 3.0),
([], 0.0),
(["foo", "bar"], 0.0),
],
)
def test_harmony_parametrized(inputs, monkeypatch, capsys, expected):
monkeypatch.setattr(sys, "argv", ["harmony"] + inputs)
main()
assert capsys.readouterr().out.strip() == colored(expected, "red", "on_cyan", attrs=["bold"])


FRUITS = ["apple"]


def test_len():
assert len(FRUITS) == 1


def test_append():
FRUITS.append("banana")
assert FRUITS == ["apple", "banana"]

0 comments on commit 0012d0a

Please sign in to comment.