Skip to content

Commit

Permalink
Merge pull request #191 from openzim/adopt_python_bootstrap
Browse files Browse the repository at this point in the history
Adopt python bootstrap conventions
  • Loading branch information
benoit74 authored Aug 18, 2023
2 parents 1ea3cc1 + 0cc670e commit 927af14
Show file tree
Hide file tree
Showing 94 changed files with 1,119 additions and 772 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/Publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Build and upload to PyPI

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-22.04
permissions:
id-token: write # mandatory for PyPI trusted publishing

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version-file: pyproject.toml
architecture: x64

- name: Build packages
run: |
pip install -U pip build
python -m build --sdist --wheel
- name: Upload to PyPI
uses: pypa/gh-action-pypi-publish@release/v1.8

- name: Build and push Docker image
uses: openzim/docker-publish-action@v10
with:
image-name: openzim/gutenberg
tag-pattern: /^v([0-9.]+)$/
latest-on-tag: true
restrict-to: openzim/gutenberg
registries: ghcr.io
credentials:
GHCRIO_USERNAME=${{ secrets.GHCR_USERNAME }}
GHCRIO_TOKEN=${{ secrets.GHCR_TOKEN }}
repo_description: auto
repo_overview: auto
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
name: Docker
name: Publish Docker dev image

on:
push:
branches:
- main

jobs:
build-and-push:
name: Deploy Docker Image
publish:
runs-on: ubuntu-22.04

steps:
- uses: actions/[email protected]
- name: Build and push
- uses: actions/checkout@v3

- name: Build and push Docker image
uses: openzim/docker-publish-action@v10
with:
image-name: openzim/gutenberg
on-master: dev
tag-pattern: /^v([0-9.]+)$/
latest-on-tag: true
manual-tag: dev
latest-on-tag: false
restrict-to: openzim/gutenberg
registries: ghcr.io
credentials:
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/QA.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: QA

on:
pull_request:
push:
branches:
- main

jobs:
check-qa:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version-file: pyproject.toml
architecture: x64

- name: Install dependencies (and project)
run: |
pip install -U pip
pip install -e .[lint,scripts,test,check]
- name: Check black formatting
run: inv lint-black

- name: Check ruff
run: inv lint-ruff

- name: Check pyright
run: inv check-pyright
61 changes: 61 additions & 0 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Tests

on:
pull_request:
push:
branches:
- main

jobs:
run-tests:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version-file: pyproject.toml
architecture: x64

- name: Install dependencies (and project)
run: |
pip install -U pip
pip install -e .[test,scripts]
- name: Run the tests
run: inv coverage --args "-vvv"

- name: Upload coverage report to codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}

build_python:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version-file: pyproject.toml
architecture: x64

- name: Ensure we can build Python targets
run: |
pip install -U pip build
python3 -m build --sdist --wheel
build_docker:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Ensure we can build the Docker image
run: |
docker build -t gutenberg .
- name: Ensure we can start the Docker image
run: |
docker run --rm gutenberg
49 changes: 0 additions & 49 deletions .github/workflows/release.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: "23.3.0"
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.272
hooks:
- id: ruff
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.323
hooks:
- id: pyright
name: pyright (system)
description: 'pyright static type checker'
entry: pyright
language: system
'types_or': [python, pyi]
require_serial: true
minimum_pre_commit_version: '2.9.2'
55 changes: 33 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
FROM python:3.11.4-bookworm

# Install necessary packages
RUN apt-get update -y && \
apt-get install -y --no-install-recommends advancecomp libxml2-dev libxslt-dev python3-pillow rsync libjpeg-dev libpng-dev libmagic1 locales jpegoptim pngquant gifsicle && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
advancecomp \
libxml2-dev \
libxslt-dev \
python3-pillow \
rsync \
libjpeg-dev \
libpng-dev \
libmagic1 \
locales \
jpegoptim \
pngquant \
gifsicle \
&& rm -rf /var/lib/apt/lists/* \
&& python -m pip install --no-cache-dir -U \
pip \
&& sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
&& locale-gen "en_US.UTF-8"

# Install gutenberg (from source)
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen "en_US.UTF-8"
COPY requirements.pip /src/
RUN python3 -m pip install -r /src/requirements.pip
COPY LICENSE /src/
COPY pypi-readme.rst /src/
COPY MANIFEST.in /src/
COPY setup.py /src/
COPY get_js_deps.sh /src/
COPY gutenberg2zim /src/
COPY gutenbergtozim /src/gutenbergtozim
WORKDIR /src/
RUN python3 ./setup.py install
# Copy code + associated artifacts
COPY src /src/src
COPY pyproject.toml *.md *.rst get_js_deps.sh LICENSE *.py /src/

# Boot commands
# Install + cleanup
RUN pip install --no-cache-dir /src \
&& rm -rf /src

# default output directory
RUN mkdir -p /output
WORKDIR /output

ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
CMD gutenberg2zim --help ; /bin/bash
ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8

CMD ["gutenberg2zim", "--help"]
4 changes: 0 additions & 4 deletions MANIFEST.in

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ storing content for offline usage.
[![Docker](https://ghcr-badge.deta.dev/openzim/gutenberg/latest_tag?label=docker)](https://ghcr.io/openzim/gutenberg)
[![CodeFactor](https://www.codefactor.io/repository/github/openzim/gutenberg/badge)](https://www.codefactor.io/repository/github/openzim/gutenberg)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![codecov](https://codecov.io/gh/openzim/gutenberg/branch/main/graph/badge.svg)](https://codecov.io/gh/openzim/gutenberg)

## Coding guidelines
Main coding guidelines comes from the [openZIM Wiki](https://github.com/openzim/overview/wiki)
Expand Down
2 changes: 1 addition & 1 deletion get_js_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fi

# Absolute path this script is in.
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
ASSETS_PATH="${SCRIPT_PATH}/gutenbergtozim/templates"
ASSETS_PATH="${SCRIPT_PATH}/src/gutenberg2zim/templates"

echo "About to download JS assets to ${ASSETS_PATH}"

Expand Down
20 changes: 0 additions & 20 deletions gutenbergtozim/__init__.py

This file was deleted.

37 changes: 37 additions & 0 deletions hatch_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import subprocess
from pathlib import Path

from hatchling.builders.hooks.plugin.interface import BuildHookInterface

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# update list in constants.py as well
JS_DEPS = [
"datatables/datatables.min.css",
"datatables/datatables.min.js",
]


class GetJsDepsHook(BuildHookInterface):
def initialize(self, version, build_data):
if self.deps_already_installed():
logger.info("JS dependencies are already installed, skipping it")
return
subprocess.run(
str(Path(self.root).joinpath("get_js_deps.sh")),
check=True,
)
return super().initialize(version, build_data)

def deps_already_installed(self) -> bool:
for dep in JS_DEPS:
if (
not Path(self.root)
.joinpath("gutebergtozim/templates")
.joinpath(dep)
.exists()
):
return False
return True
Loading

0 comments on commit 927af14

Please sign in to comment.