Compare commits

..

20 Commits

Author SHA1 Message Date
Drew Bednar 4ddd52d37d Fix links 1 year ago
Drew Bednar 4fa49fb720 Add usage instructions 1 year ago
Drew Bednar c685140890 Merge pull request '[Closes #3] Adds image_registry as a template variable' (#5) from drew/registry-enhancement into master
Reviewed-on: #5
1 year ago
Drew Bednar c48d5ba13f [Closes #3] Adds image_registry as a template variable 1 year ago
Drew Bednar 66f1df5809 Allow multiple documents in single yaml file 1 year ago
Drew Bednar e87ba2f3f2 Fix missing whitespace and isort 1 year ago
Drew Bednar 46040764bb Merge pull request 'Adding simple Docker setup' (#3) from drew/add-docker into master
Reviewed-on: #3
1 year ago
Drew Bednar 8761dbff80 Adding simple Docker setup 1 year ago
Drew Bednar cb137d792d Fix invoke delint 1 year ago
Drew Bednar df128519c7 Adding build task 1 year ago
Drew Bednar 5544977c96 Adding support for packaging 1 year ago
Drew Bednar 06dc9fd451 Update email 1 year ago
Drew Bednar 47cc8af0cb Put invoke tasks in correct directory 1 year ago
Drew Bednar 7490ed0d3f Drive dev deps based on prod deps 1 year ago
Drew Bednar 58763b4eab Adding basic tasks.py file 1 year ago
Drew Bednar fc6977cf46 Add linters to drone CI 1 year ago
Drew Bednar f72e0082d8 Update scripts for better env setup and linting 1 year ago
Drew Bednar 1d544043f8 Add ruff config 1 year ago
Drew Bednar 4859f8a570 Correct Drone config file name 1 year ago
Drew Bednar 6cab703805 Merge pull request 'drew/add-ci-setup' (#1) from drew/add-ci-setup into master
Reviewed-on: #1
1 year ago

@ -1,3 +1,30 @@
# cookiecutter-basic # cookiecutter-basic
A basic cookiecutter template for most Androiddrew style repos. A basic [Cookiecutter](https://cookiecutter.readthedocs.io/en/stable) template for most Androiddrew style repos.
## Usage
With [Cookiecutter installed](https://cookiecutter.readthedocs.io/en/stable/installation.html), execute the following command and provide your template parameters.
```
cookiecutter https://git.runcible.io/androiddrew/cookiecutter-basic
```
Once templated, you can change into the new project directory and execute the `scripts/bootstrap.sh` script.
```
cd <cookiecutter.project_slug>
./scripts/bootstrap.sh
```
This will create a Python virtual environment if one is not already enabled, install [pip-tools](https://github.com/jazzband/pip-tools/), compile the requirements.in and dev-requirements.in files, install the initial dependencies, initialize the project as a git repository, and install the [pre-commit hooks](https://pre-commit.com/).
## Licenses
Within a `pyproject.toml` file the license section expects the string identifier as determined by https://spdx.org/licenses/. This project supports a small subset of these licenses.
Example:
```
[project]
license = {text = "MIT"}
```

@ -1,17 +1,18 @@
{ {
"full_name": "Drew Bednar", "full_name": "Drew Bednar",
"email": "drew@androiddrew.com", "email": "drew@runcible.io",
"github_username": "androiddrew", "github_username": "androiddrew",
"project_name": "python_app", "project_name": "python_app",
"project_slug": "{{ cookiecutter.project_name.lower().strip().replace(' ', '_').replace('-','_')}}", "project_slug": "{{ cookiecutter.project_name.lower().strip().replace(' ', '_').replace('-','_')}}",
"initial_dependencies": "", "initial_dependencies": "",
"description": "An templated Python project", "description": "A templated Python project",
"image_registry": "registry.runcible.io",
"open_source_license": [ "open_source_license": [
"MIT license", "MIT",
"BSD license", "BSD-3-Clause",
"ISC license", "ISC",
"Apache Software License 2.0", "Apache-2.0",
"GNU General Public License v3", "GPL-3.0-or-later",
"Not open source" "Not open source"
] ]
} }

@ -0,0 +1,14 @@
./tests
./scripts
.ruff_cache
.coveragerc
.dockerignore
.git
.gitignore
.pre-commit-config.yaml
dev-requirements.in
dev-requirements.txt
.profile
Dockerfile
requirements.in
tasks.py

@ -1,23 +0,0 @@
kind: pipeline
type: docker
name: CI Test Pipeline
steps:
- name: greeting
image: alpine
commands:
- echo "Welcome to drone\n"
- name: Unit Tests
image: python:3.11-bullseye
commands:
- bash -c './ci_scripts/run_unit_tests.sh'
trigger:
event:
- pull_request
- push
# Secrets used to pull private images
image_pull_secrets:
- dockerconfigjson

@ -0,0 +1,44 @@
kind: pipeline
type: docker
name: CI Test/Lint Pipeline
steps:
- name: Unit Tests and Linters
# Bullseye because drone runner host OS is using older libseccomp2 causing issues
# with thread allocation. See: https://github.com/docker-library/python/issues/835
image: python:3.11-bullseye
commands:
- bash -xc './scripts/run_linters.sh'
- bash -xc './scripts/run_unit_tests.sh'
group: test-lint
trigger:
event:
- pull_request
- push
# Secrets used to pull private images
image_pull_secrets:
- dockerconfigjson
---
kind: pipeline
type: docker
name: Build Production Image
steps:
- name: Build {{ cookiecutter.project_slug }} Container Image
image: plugins/docker
settings:
username: automate
password:
from_secret: automate_password
dockerfile: Dockerfile
registry: {{ cookiecutter.image_registry }}
repo: {{ cookiecutter.image_registry }}/{{ cookiecutter.project_slug }}
tags:
- ${DRONE_COMMIT_SHA}
when:
branch:
- master
event:
- push

@ -6,6 +6,7 @@ repos:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer
- id: check-yaml - id: check-yaml
args: [--allow-multiple-documents]
- id: check-added-large-files - id: check-added-large-files
- id: debug-statements - id: debug-statements
- repo: https://github.com/shellcheck-py/shellcheck-py - repo: https://github.com/shellcheck-py/shellcheck-py

@ -0,0 +1,55 @@
# syntax = docker/dockerfile:1.4
# Best practice: Choose a stable base image and tag.
FROM python:3.11-slim-bookworm
# Install security updates, and some useful packages.
#
# Best practices:
# * Make sure apt-get doesn't run in interactive mode.
# * Update system packages.
# * Pre-install some useful tools.
# * Minimize system package installation.
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get -y upgrade && \
apt-get install -y --no-install-recommends tini procps net-tools && \
apt-get -y clean && \
rm -rf /var/lib/apt/lists/*
# Install dependencies.
#
# Best practices:
# * `COPY` in files only when needed.
# * Reduce disk usage from `pip` installs.
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Create a new user to run as.
#
# Best practices: Don't run as root.
RUN useradd --create-home appuser
USER appuser
WORKDIR /home/appuser
# Copy in the code.
#
# Best practices: Avoid extra chowns.
COPY --chown=appuser . .
# Best practices: Prepare for C crashes.
ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=0
ARG COMMIT_SHA
LABEL io.runcible.repo-sha="${COMMIT_SHA}"
# Run the code when the image is run:
#
# Best practices:
# * Add an `init` process.
# * Make sure images shut down correctly (via ENTRYPOINT [] syntax).
# * '-g' option means killing the container kills all processes, not just the
# entrypoint shell.
ENTRYPOINT ["tini", "-g", "--", "./entrypoint.sh"]

@ -1,4 +1,4 @@
{% if cookiecutter.open_source_license == 'MIT license' %} {% if cookiecutter.open_source_license == 'MIT' %}
MIT License MIT License
Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }} Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }}
@ -8,7 +8,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. 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.
{% elif cookiecutter.open_source_license == 'BSD license' %} {% elif cookiecutter.open_source_license == 'BSD-3-Clause' %}
BSD License BSD License
Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }} Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }}
@ -46,7 +46,7 @@ Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }}
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
{% elif cookiecutter.open_source_license == 'Apache Software License 2.0' %} {% elif cookiecutter.open_source_license == 'Apache-2.0' %}
Apache Software License 2.0 Apache Software License 2.0
Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }} Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }}
@ -62,7 +62,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
{% elif cookiecutter.open_source_license == 'GNU General Public License v3' %} {% elif cookiecutter.open_source_license == 'GPL-3.0-or-later' %}
GNU GENERAL PUBLIC LICENSE GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 29 June 2007

@ -1,6 +1,9 @@
-c ./requirements.txt
black black
build
invoke invoke
isort isort<=5.12.0
pip-tools pip-tools
pre-commit pre-commit
pytest pytest

@ -0,0 +1,8 @@
#!/bin/bash
# Best practice: Bash strict mode.
set -euo pipefail
# Best practice: Make sure the image shuts down correctly by using `exec` in
# entry point shell scripts.
exec "$@"

@ -1,3 +1,27 @@
[project]
name = "{{ cookiecutter.project_slug }}"
version = "0.1.0"
authors = [
{name = "{{ cookiecutter.full_name }}", email = "{{ cookiecutter.email }}"},
]
description = "{{ cookiecutter.description }}"
requires-python = ">=3.10"
{% if cookiecutter.open_source_license != 'Not open source' -%}
license = {text = "{{ cookiecutter.open_source_license }}"}
{% endif -%}
classifiers = [
"Programming Language :: Python :: 3",
]
dynamic = ["readme", "dependencies"]
[tool.setuptools.dynamic]
readme = {file = ["README.md"], content-type = "text/markdown"}
dependencies = {file = ["requirements.txt"]}
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[tool.isort] [tool.isort]
profile = "black" profile = "black"
line_length = 120 line_length = 120
@ -8,3 +32,30 @@ filter_files = true
line-length = 120 line-length = 120
skip-string-normalization = true skip-string-normalization = true
exclude = "(^/\\.git|^/env/|^/venv/|^/node_modules/)" exclude = "(^/\\.git|^/env/|^/venv/|^/node_modules/)"
[tool.ruff]
line-length = 120
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

@ -16,8 +16,17 @@ source "$(dirname "$0")/_common.sh"
function project_bootstrap() { function project_bootstrap() {
_setup_env _setup_env
pip install -U pip pip-tools=='7.3.0' pip install -U pip pip-tools=='7.3.0'
pip-compile requirements.in
pip-compile dev-requirements.in # Check if requirements.txt exists, if not, compile it from requirements.in
if [ ! -f requirements.txt ]; then
pip-compile requirements.in
fi
# Check if dev-requirements.txt exists, if not, compile it from dev-requirements.in
if [ ! -f dev-requirements.txt ]; then
pip-compile dev-requirements.in
fi
pip-sync requirements.txt dev-requirements.txt pip-sync requirements.txt dev-requirements.txt
} }

@ -0,0 +1,28 @@
#! /usr/bin/env bash
# shellcheck source=/dev/null
source "$(dirname "$0")/_common.sh"
if [ "${DRONE}" == "true" ]; then
_setup_env
pip install -r requirements.txt -r dev-requirements.txt
fi
# Run linting commands and capture their return codes
"${VIRTUAL_ENV}/bin/python3" -m isort --check ./speech_collect ./tests ./tasks.py
ISORT_EXIT_CODE=$?
"${VIRTUAL_ENV}/bin/python3" -m black --check ./speech_collect ./tests ./tasks.py
BLACK_EXIT_CODE=$?
"${VIRTUAL_ENV}/bin/python3" -m ruff ./speech_collect ./tests ./tasks.py
RUFF_EXIT_CODE=$?
# Check if any linting command failed
if [ $ISORT_EXIT_CODE -ne 0 ] || [ $BLACK_EXIT_CODE -ne 0 ] || [ $RUFF_EXIT_CODE -ne 0 ]; then
echo "Some linting checks failed"
# Exit with a non-zero status, you can choose which error code to return
exit 1
fi
echo "All linting checks passed"

@ -9,4 +9,4 @@ if [ "${DRONE}" == "true" ]; then
pip install -r requirements.txt -r dev-requirements.txt pip install -r requirements.txt -r dev-requirements.txt
fi fi
exec "${VIRTUAL_ENV}/bin/python" -m pytest -vv --cov "$@" exec "${VIRTUAL_ENV}/bin/python3" -m pytest -vv --cov "$@"

@ -0,0 +1,52 @@
import os
from invoke import task
IMAGE_RESPOSITORY = os.environ.get("IMAGE_RESPOSITORY", "{{ cookiecutter.image_registry }}/{{ cookiecutter.project_slug }}")
@task
def update_deps(c):
"""Updates depenencies"""
c.run("pip-compile requirements.in", pty=True)
c.run("pip-compile dev-requirements.in", pty=True)
@task
def sync_deps(c):
"""Syncs local dependencies"""
c.run("pip-sync requirements.txt dev-requirements.txt")
@task
def lint(c):
"""Runs all linters against the project."""
c.run("./scripts/run_linters.sh", pty=True)
@task
def delint(c):
"""Applies automated linters to project"""
c.run("isort ./{{ cookiecutter.project_slug }} ./tests ./tasks.py", pty=True)
c.run("black ./{{ cookiecutter.project_slug }} ./tests ./tasks.py", pty=True)
@task
def build(c):
"""Builds the project as a Python package."""
c.run("python3 -m build")
@task
def build_image(c, dev=True, registry_user=None, registry_token=None, push=False, login=False):
"""Builds the {{ cookiecutter.project_slug }} container image."""
context_dir = c.run("pwd", hide=True).stdout.strip()
commit_sha = c.run("git rev-parse --short HEAD", hide=True).stdout.strip()
image_name = f"{IMAGE_RESPOSITORY}:{commit_sha}{'-dev' if dev else ''}"
c.run(f"docker build --build-arg='COMMIT_SHA={commit_sha}' -t {image_name} {context_dir}")
if login:
if registry_user is None or registry_token is None:
raise ValueError("--registry_user and --registry_token must be provided if using --login parameter")
c.run(f"docker login -u {registry_user} -p {registry_token}")
if push:
c.run(f"docker push {image_name}")
Loading…
Cancel
Save