Skip to content

Commit

Permalink
Merge pull request AmbitionEng#153 from Opus10/_footing_update
Browse files Browse the repository at this point in the history
Updated with latest Python template.
  • Loading branch information
wesleykendall authored Apr 24, 2024
2 parents 62f1067 + deab946 commit d1f027c
Show file tree
Hide file tree
Showing 18 changed files with 322 additions and 149 deletions.
53 changes: 32 additions & 21 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ version: 2.1
orbs:
opus10:
executors:
python-pg:
parameters:
pg_version:
type: "string"
default: "14.4"
python:
working_directory: /code
docker:
- image: opus10/circleci-public-django-app:2023-10-06
- image: opus10/circleci-python-library:2024-04-17
environment:
# Ensure makefile commands are not wrapped in "docker-compose run"
EXEC_WRAPPER: ''
DATABASE_URL: postgres://root@localhost/circle_test?sslmode=disable
TOX_PARALLEL_NO_SPINNER: 1
DATABASE_URL: postgres://root@localhost/circle_test?sslmode=disable
- image: cimg/postgres:<<parameters.pg_version>>
environment:
POSTGRES_USER: root
POSTGRES_DB: circle_test
POSTGRES_PASSWORD: password
parameters:
pg_version:
type: "string"
default: "14.4"
commands:
test:
steps:
- checkout
- restore_cache:
key: v4-{{ checksum "poetry.lock" }}
key: v4-{{ checksum "poetry.lock" }}
- run: make dependencies
- run: make full-test-suite
- save_cache:
key: v4-{{ checksum "poetry.lock" }}
key: v4-{{ checksum "poetry.lock" }}
paths:
- /home/circleci/.cache/pypoetry/
- /code/.venv
Expand All @@ -39,45 +39,54 @@ orbs:
jobs:
test_pg_min:
executor:
name: opus10/python-pg
name: opus10/python
pg_version: "13.11"
steps:
- opus10/test

test_pg_max:
executor:
name: opus10/python-pg
name: opus10/python
pg_version: "16.0"
steps:
- opus10/test

lint:
executor: opus10/python-pg
executor: opus10/python
steps:
- checkout
- restore_cache:
key: v4-{{ checksum "poetry.lock" }}
key: v4-{{ checksum "poetry.lock" }}
- run: make dependencies
- run: make lint

check_changelog:
executor: opus10/python-pg
type_check:
executor: opus10/python
steps:
- checkout
- restore_cache:
key: v4-{{ checksum "poetry.lock" }}
- run: make dependencies
- run: git tidy-log origin/master..
- run: make type-check || true

check_changelog:
executor: opus10/python
steps:
- checkout
- restore_cache:
key: v4-{{ checksum "poetry.lock" }}
- run: make dependencies
- run: git tidy-log origin/main..
- run: make tidy-lint

deploy:
executor: opus10/python-pg
executor: opus10/python
steps:
- checkout
- run: ssh-add -D
- run: echo "${GITHUB_DEVOPS_PRIVATE_SSH_KEY_BASE64}" | base64 --decode | ssh-add - > /dev/null
- restore_cache:
key: v4-{{ checksum "poetry.lock" }}
key: v4-{{ checksum "poetry.lock" }}
- run: make dependencies
- run: poetry run python devops.py deploy

Expand All @@ -88,16 +97,18 @@ workflows:
- test_pg_min
- test_pg_max
- lint
- type_check
- check_changelog:
filters:
branches:
ignore: master
ignore: main
- deploy:
context: public-django-app
context: python-library
requires:
- test_pg_min
- test_pg_max
- lint
- type_check
filters:
branches:
only: master
only: main
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Set up your development environment with:

`make docker-setup` will set up a development environment managed by Docker. Install docker [here](https://www.docker.com/get-started) and be sure it is running when executing any of the commands below.

If you prefer a native development environment, `make conda-setup` will set up a development environment managed by [Conda](https://conda.io). The database must be ran manually.
If you prefer a native development environment, `make conda-setup` will set up a development environment managed by [Conda](https://conda.io). Dependent services, such as databases, must be ran manually.

## Testing and Validation

Expand Down Expand Up @@ -48,7 +48,7 @@ To check if your commits pass linting, do:

make tidy-lint

Note, the above command lints every commit since branching from master. You can also run `make shell` and run `git tidy` commands inside the docker environment to do other flavors of `git tidy` commands.
Note, the above command lints every commit since branching from main. You can also run `make shell` and run `git tidy` commands inside the docker environment to do other flavors of `git tidy` commands.

## Documentation

Expand All @@ -62,7 +62,7 @@ A shortcut for serving them is:

## Releases and Versioning

Anything that is merged into the master branch will be automatically deployed to PyPI. Documentation will be published to a ReadTheDocs at `https://django-pgtrigger.readthedocs.io/`.
Anything that is merged into the main branch will be automatically deployed to PyPI. Documentation will be published to a ReadTheDocs at `https://django-pgtrigger.readthedocs.io/`.

The following files will be generated and should *not* be edited by a user:

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2023, Opus 10
Copyright (c) 2024, Opus 10
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
20 changes: 13 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# docs-serve - Serve documentation
# lint - Run code linting and static checks
# lint-fix - Fix common linting errors
# type-check - Run Pyright type-checking
# test - Run tests using pytest
# full-test-suite - Run full test suite using tox
# shell - Run a shell in a virtualenv
Expand Down Expand Up @@ -37,7 +38,6 @@ endif
# Docker run mounts the local code directory, SSH (for git), and global git config information
DOCKER_RUN_CMD?=$(DOCKER_CMD)-compose run --name $(PACKAGE_NAME) $(DOCKER_RUN_ARGS) -d app


# Print usage of main targets when user types "make" or "make help"
.PHONY: help
help:
Expand All @@ -52,6 +52,7 @@ ifndef run
" tox: Run tests against all versions of Python\n"\
" lint: Run code linting and static checks\n"\
" lint-fix: Fix common linting errors\n"\
" type-check: Run Pyright type-checking\n"\
" docs: Build documentation\n"\
" docs-serve: Serve documentation\n"\
" docker-teardown: Spin down docker resources\n"\
Expand Down Expand Up @@ -152,7 +153,7 @@ full-test-suite:
# Build documentation
.PHONY: docs
docs:
$(EXEC_WRAPPER) mkdocs build
$(EXEC_WRAPPER) mkdocs build -s


# Serve documentation
Expand All @@ -164,23 +165,28 @@ docs-serve:
# Run code linting and static analysis. Ensure docs can be built
.PHONY: lint
lint:
$(EXEC_WRAPPER) black . --check
$(EXEC_WRAPPER) ruff format . --check
$(EXEC_WRAPPER) ruff check ${MODULE_NAME}
$(EXEC_WRAPPER) footing update --check
$(EXEC_WRAPPER) bash -c 'make docs'


# Fix common linting errors
.PHONY: lint-fix
lint-fix:
$(EXEC_WRAPPER) black .
$(EXEC_WRAPPER) ruff format .
$(EXEC_WRAPPER) ruff check ${MODULE_NAME} --fix


# Run Pyright type-checking
.PHONY: type-check
type-check:
$(EXEC_WRAPPER) pyright $(MODULE_NAME)


# Lint commit messages
.PHONY: tidy-lint
tidy-lint:
$(EXEC_WRAPPER) git tidy-lint origin/master..
$(EXEC_WRAPPER) git tidy-lint origin/main..


# Perform a tidy commit
Expand All @@ -192,4 +198,4 @@ tidy-commit:
# Perform a tidy squash
.PHONY: tidy-squash
tidy-squash:
$(EXEC_WRAPPER) git tidy-squash origin/master
$(EXEC_WRAPPER) git tidy-squash origin/main
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ If triggers are new to you, don't worry. The [pgtrigger docs](https://django-pgt
Install `django-pgtrigger` with:

pip3 install django-pgtrigger

After this, add `pgtrigger` to the `INSTALLED_APPS` setting of your Django project.

## Other Material
Expand All @@ -98,7 +97,7 @@ The [DjangoCon 2021 talk](https://www.youtube.com/watch?v=Tte3d4JjxCk) also brea

For information on setting up django-pgtrigger for development and contributing changes, view [CONTRIBUTING.md](CONTRIBUTING.md).

## Primary Authors
## Creators

- [Wes Kendall](https://github.com/wesleykendall)

Expand Down
37 changes: 23 additions & 14 deletions devops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
package deployment, changelog generation, and changelog checking.
This script is generated by the template at
https://github.com/Opus10/public-django-app-template
https://github.com/Opus10/python-library-template
Do not change this script! Any fixes or updates to this script should be made
to https://github.com/Opus10/public-django-app-template
to https://github.com/Opus10/python-library-template
"""

import os
import subprocess
import sys
import tempfile
from typing import IO, Any, Final, Literal, Tuple, TypeAlias, Union

from packaging import version

CIRCLECI_ENV_VAR: Final = "CIRCLECI"

CIRCLECI_ENV_VAR = "CIRCLECI"
File: TypeAlias = Union[IO[Any], int, None]


class Error(Exception):
Expand All @@ -29,25 +32,31 @@ class NotOnCircleCIError(Error):
"""Thrown when not running on CircleCI"""


def _check_git_version():
def _check_git_version() -> None:
"""Verify git version"""
git_version = _shell_stdout("git --version | rev | cut -f 1 -d' ' | rev")
if version.parse(git_version) < version.parse("2.22.0"):
raise RuntimeError(f"Must have git version >= 2.22.0 (version = {git_version})")


def _shell(cmd, check=True, stdin=None, stdout=None, stderr=None): # pragma: no cover
def _shell(
cmd: str,
check: bool = True,
stdin: File = None,
stdout: File = None,
stderr: File = None,
): # pragma: no cover
"""Runs a subprocess shell with check=True by default"""
return subprocess.run(cmd, shell=True, check=check, stdin=stdin, stdout=stdout, stderr=stderr)


def _shell_stdout(cmd, check=True):
def _shell_stdout(cmd: str, check: bool = True) -> str:
"""Runs a shell command and returns stdout"""
ret = _shell(cmd, stdout=subprocess.PIPE, check=check)
return ret.stdout.decode("utf-8").strip() if ret.stdout else ""


def _configure_git():
def _configure_git() -> None:
"""Configure git name/email and verify git version"""
_check_git_version()

Expand All @@ -56,11 +65,11 @@ def _configure_git():
_shell("git config push.default current")


def _find_latest_tag():
def _find_latest_tag() -> str:
return _shell_stdout("git describe --tags --abbrev=0", check=False)


def _find_sem_ver_update():
def _find_sem_ver_update() -> Literal["major", "minor", "patch"]:
"""
Find the semantic version string based on the commit log.
Defaults to returning "patch"
Expand All @@ -86,7 +95,7 @@ def _find_sem_ver_update():
return sem_ver


def _update_package_version():
def _update_package_version() -> Tuple[str, str]:
"""Apply semantic versioning to package based on git commit messages"""
# Obtain the current version
old_version = _shell_stdout("poetry version | rev | cut -f 1 -d' ' | rev")
Expand All @@ -113,7 +122,7 @@ def _update_package_version():
return old_version, new_version


def _generate_changelog_and_tag(old_version, new_version):
def _generate_changelog_and_tag(old_version: str, new_version: str) -> None:
"""Generates a change log using git-tidy and tags repo"""
# Tag the version temporarily so that changelog generation
# renders properly
Expand Down Expand Up @@ -149,7 +158,7 @@ def _generate_changelog_and_tag(old_version, new_version):
_shell(f"git tag -f -a {new_version} -F {commit_msg_file.name} --cleanup=whitespace")


def _publish_to_pypi():
def _publish_to_pypi() -> None:
"""
Uses poetry to publish to pypi
"""
Expand All @@ -161,7 +170,7 @@ def _publish_to_pypi():
_shell("poetry publish -vvv -n", stdout=subprocess.PIPE)


def _build_and_push_distribution():
def _build_and_push_distribution() -> None:
"""
Builds and pushes distribution to PyPI, along with pushing the
tags back to the repo
Expand All @@ -172,7 +181,7 @@ def _build_and_push_distribution():
_shell("git push --follow-tags")


def deploy():
def deploy() -> None:
"""Deploys the package and uploads documentation."""
# Ensure proper environment
if not os.environ.get(CIRCLECI_ENV_VAR): # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
app:
image: opus10/circleci-public-django-app
image: opus10/circleci-python-library
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
depends_on:
Expand Down
Loading

0 comments on commit d1f027c

Please sign in to comment.