Skip to content

Commit

Permalink
Make package (#14)
Browse files Browse the repository at this point in the history
Create an installable package so it can be uploaded to PyPI.



* add makefile and version

* Add initial unit test

* add requirements-test.txt

* update setup.py

* add the actual source

* update github workflows

* fix linter workflow by installing linter

* debugging

* bug fix?

* bug fix again?

* renamed makefile

* bug fix

* add requirements-test.txt

* bugfixes mypy

* small bug fixes

* add pyproject.toml file
  • Loading branch information
joshuawe committed Nov 10, 2023
1 parent 59dcc00 commit f95f097
Show file tree
Hide file tree
Showing 14 changed files with 942 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .github/release_message.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
previous_tag=$(git tag --sort=-creatordate | sed -n 2p)
git shortlog "${previous_tag}.." | s
146 changes: 125 additions & 21 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,129 @@
name: API workflow
# name: API workflow

on: [push, pull_request]
# on: [push, pull_request]

# jobs:
# build:
# runs-on: ubuntu-latest
# name: Test python API
# steps:
# - uses: actions/checkout@v1
# - name: Install requirements
# run: pip install -r requirements.txt
# - name: Run tests and collect coverage
# run: pytest --cov .
# - name: Upload coverage reports to Codecov
# run: |
# # Replace `linux` below with the appropriate OS
# # Options are `alpine`, `linux`, `macos`, `windows`
# curl -Os https://uploader.codecov.io/latest/linux/codecov
# chmod +x codecov
# ./codecov -t ${CODECOV_TOKEN}
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}




# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
name: Test python API
linter:
strategy:
fail-fast: false
matrix:
python-version: [3.9]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Debugging
run: |
ls -la
cat Makefile
make virtualenv
- name: Install project
run: |
make virtualenv
source .venv/bin/activate
make install
- name: Run linter
run: make lint

tests_linux:
needs: linter
strategy:
fail-fast: false
matrix:
python-version: [3.9]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- name: Install requirements
run: pip install -r requirements.txt
- name: Run tests and collect coverage
run: pytest --cov .
- name: Upload coverage reports to Codecov
run: |
# Replace `linux` below with the appropriate OS
# Options are `alpine`, `linux`, `macos`, `windows`
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -t ${CODECOV_TOKEN}
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install project
run: make install
- name: Run tests
run: make test
- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v3
# with:
# fail_ci_if_error: true

# tests_mac:
# needs: linter
# strategy:
# fail-fast: false
# matrix:
# python-version: [3.9]
# os: [macos-latest]
# runs-on: ${{ matrix.os }}
# steps:
# - uses: actions/checkout@v3
# - uses: actions/setup-python@v4
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install project
# run: make install
# - name: Run tests
# run: make test

# tests_win:
# needs: linter
# strategy:
# fail-fast: false
# matrix:
# python-version: [3.9]
# os: [windows-latest]
# runs-on: ${{ matrix.os }}
# steps:
# - uses: actions/checkout@v3
# - uses: actions/setup-python@v4
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install Pip
# run: pip install --user --upgrade pip
# - name: Install project
# run: pip install -e .[test]
# - name: run tests
# run: pytest -s -vvvv -l --tb=long tests
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Pylint

on: [push]
on: [push, pull_request]

jobs:
build:
Expand Down
50 changes: 50 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Upload Python Package

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- '*' # Push events to matching v*, i.e. v1.0, v20.15.10

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
release:
name: Create Release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
# by default, it uses a depth of 1
# this fetches all history so that we can read each commit
fetch-depth: 0
- name: Generate Changelog
run: .github/release_message.sh > release_message.md
- name: Release
uses: softprops/action-gh-release@v1
with:
body_path: release_message.md

deploy:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
126 changes: 126 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
.ONESHELL:
ENV_PREFIX=$(shell python -c "if __import__('pathlib').Path('.venv/bin/pip').exists(): print('.venv/bin/')")
USING_POETRY=$(shell grep "tool.poetry" pyproject.toml && echo "yes")

.PHONY: help
help: ## Show the help.
@echo "Usage: make <target>"
@echo ""
@echo "Targets:"
@fgrep "##" Makefile | fgrep -v fgrep


.PHONY: show
show: ## Show the current environment.
@echo "Current environment:"
@if [ "$(USING_POETRY)" ]; then poetry env info && exit; fi
@echo "Running using $(ENV_PREFIX)"
@$(ENV_PREFIX)python -V
@$(ENV_PREFIX)python -m site

.PHONY: install
install: ## Install the project in dev mode.
@if [ "$(USING_POETRY)" ]; then poetry install && exit; fi
@echo "Don't forget to run 'make virtualenv' if you got errors."
$(ENV_PREFIX)pip install -e .[test]

.PHONY: fmt
fmt: ## Format code using black & isort.
$(ENV_PREFIX)isort plotsandgraphs/
$(ENV_PREFIX)black -l 79 plotsandgraphs/
$(ENV_PREFIX)black -l 79 tests/

.PHONY: lint
lint: ## Run pep8, black, mypy linters.
@echo "Running linters ..."
@echo "--- Running flake8 ---"
$(ENV_PREFIX)flake8 plotsandgraphs/
@echo "--- Running black ---"
$(ENV_PREFIX)black -l 79 --check plotsandgraphs/
$(ENV_PREFIX)black -l 79 --check tests/
@echo "--- Running mypy ---"
$(ENV_PREFIX)mypy --ignore-missing-imports plotsandgraphs/

.PHONY: test
test: lint ## Run tests and generate coverage report.
$(ENV_PREFIX)pytest -v --cov-config .coveragerc --cov=plotsandgraphs -l --tb=short --maxfail=1 tests/
$(ENV_PREFIX)coverage xml
$(ENV_PREFIX)coverage html

.PHONY: watch
watch: ## Run tests on every change.
ls **/**.py | entr $(ENV_PREFIX)pytest -s -vvv -l --tb=long --maxfail=1 tests/

.PHONY: clean
clean: ## Clean unused files.
@find ./ -name '*.pyc' -exec rm -f {} \;
@find ./ -name '__pycache__' -exec rm -rf {} \;
@find ./ -name 'Thumbs.db' -exec rm -f {} \;
@find ./ -name '*~' -exec rm -f {} \;
@rm -rf .cache
@rm -rf .pytest_cache
@rm -rf .mypy_cache
@rm -rf build
@rm -rf dist
@rm -rf *.egg-info
@rm -rf htmlcov
@rm -rf .tox/
@rm -rf docs/_build

.PHONY: virtualenv
virtualenv: ## Create a virtual environment.
@if [ "$(USING_POETRY)" ]; then poetry install && exit; fi
@echo "creating virtualenv ..."
@rm -rf .venv
@python3 -m venv .venv
@./.venv/bin/pip install -U pip
@./.venv/bin/pip install -e .[test]
@echo
@echo "!!! Please run 'source .venv/bin/activate' to enable the environment !!!"

.PHONY: release
release: ## Create a new tag for release.
@echo "WARNING: This operation will create s version tag and push to github"
@read -p "Version? (provide the next x.y.z semver) : " TAG
@echo "$${TAG}" > plotsandgraphs/VERSION
@$(ENV_PREFIX)gitchangelog > HISTORY.md
@git add plotsandgraphs/VERSION HISTORY.md
@git commit -m "release: version $${TAG} 🚀"
@echo "creating git tag : $${TAG}"
@git tag $${TAG}
@git push -u origin HEAD --tags
@echo "Github Actions will detect the new tag and release the new version."

.PHONY: docs
docs: ## Build the documentation.
@echo "building documentation ..."
@$(ENV_PREFIX)mkdocs build
URL="site/index.html"; xdg-open $$URL || sensible-browser $$URL || x-www-browser $$URL || gnome-open $$URL || open $$URL

.PHONY: switch-to-poetry
switch-to-poetry: ## Switch to poetry package manager.
@echo "Switching to poetry ..."
@if ! poetry --version > /dev/null; then echo 'poetry is required, install from https://python-poetry.org/'; exit 1; fi
@rm -rf .venv
@poetry init --no-interaction --name=a_flask_test --author=rochacbruno
@echo "" >> pyproject.toml
@echo "[tool.poetry.scripts]" >> pyproject.toml
@echo "plotsandgraphs = 'plotsandgraphs.__main__:main'" >> pyproject.toml
@cat requirements.txt | while read in; do poetry add --no-interaction "$${in}"; done
@cat requirements-test.txt | while read in; do poetry add --no-interaction "$${in}" --dev; done
@poetry install --no-interaction
@mkdir -p .github/backup
@mv requirements* .github/backup
@mv setup.py .github/backup
@echo "You have switched to https://python-poetry.org/ package manager."
@echo "Please run 'poetry shell' or 'poetry run plotsandgraphs'"

.PHONY: init
init: ## Initialize the project based on an application template.
@./.github/init.sh


# This project has been generated from rochacbruno/python-project-template
# __author__ = 'rochacbruno'
# __repo__ = https://github.com/rochacbruno/python-project-template
# __sponsor__ = https://github.com/sponsors/rochacbruno/
1 change: 1 addition & 0 deletions plotsandgraphs/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.01
2 changes: 2 additions & 0 deletions plotsandgraphs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import binary_classifier
from . import compare_distributions

0 comments on commit f95f097

Please sign in to comment.