diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 202cd75..28ef857 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,3 +32,5 @@ jobs: - run: uv run ruff check - run: uv run ruff format --check - run: uv run mypy src + # The committed core is the xhandler.wasm of the xhandler-rust release in its VERSION + - run: make verify-core diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..36e02b4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,57 @@ +name: Release + +# Runs when a release is published (release-tools' release-xhandler workflow creates it after +# syncing the core of the xhandler-rust release of the same version) and publishes to PyPI. +# The package version is the core's (src/xhandler/_core/VERSION), e.g. 6.0.0a1 for +# v6.0.0-alpha.1. + +on: + release: + types: [published] + workflow_dispatch: + inputs: + tag: + description: "Existing release tag to publish again, e.g. v6.0.0-alpha.1" + required: true + type: string + +permissions: + contents: read + +env: + TAG: ${{ github.event.release.tag_name || inputs.tag }} + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + ref: ${{ env.TAG }} + - uses: astral-sh/setup-uv@v7 + # A release carries the core released by xhandler-rust as the same version (release-tools + # checks it too, before tagging) + - run: make verify-release TAG="$TAG" + - run: uv run pytest + - run: uv build + - uses: actions/upload-artifact@v7.0.2 + with: + name: dist + path: dist/ + + pypi: + needs: build + runs-on: ubuntu-latest + # PyPI trusted publishing: the publisher on pypi.org names this repository, release.yml and + # this environment + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v8.0.2 + with: + name: dist + path: dist/ + - uses: pypa/gh-action-pypi-publish@v1.14.3 + with: + skip-existing: true diff --git a/.gitignore b/.gitignore index 9c4f103..e278bda 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__/ .mypy_cache/ .ruff_cache/ dist/ +/.dist/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bfcd21b..6d233a3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,11 +23,14 @@ XHANDLER_E2E=true uv run pytest tests/test_e2e.py # crea, firma y envía una fa Rust (`src/xhandler/_core/xhandler.wasm`). Esos cambios van en xhandler-rust; luego aquí: ```shell - make sync RUST_REPO=../xhandler-rust + make sync RUST_REPO=../xhandler-rust # el núcleo de tu checkout, para desarrollar + make sync CORE=6.0.0-alpha.1 # el núcleo de una versión de xhandler-rust ``` `make sync` copia el núcleo, los modelos (`src/xhandler/xbuilder/models.py`) y el corpus de - conformidad (`testdata/conformance/`). No los edites a mano. + conformidad (`testdata/conformance/`). No los edites a mano. La versión del paquete es la del + núcleo, y una versión solo se publica con el núcleo de la versión igual de xhandler-rust: + `make verify-core` lo comprueba. - En Python está lo que necesita IO: el cliente HTTP (httpx), los reintentos y el caché de tokens (`src/xhandler/xsender/`). - Los nombres (`FileSender`, `SunatClient`, `SigningKey`...) se mantienen iguales en Go, Python y diff --git a/Makefile b/Makefile index 45aedfc..caba9ff 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,38 @@ -# RUST_REPO is a checkout of xhandler-rust; `make sync` copies what its -# `make -C xhandler-wasm dist` produced: the WebAssembly module, the generated -# models and the conformance corpus. +# The core (src/xhandler/_core/xhandler.wasm, src/xhandler/xbuilder/models.py and +# testdata/conformance) comes from xhandler-rust: +# +# make sync CORE= the xhandler-rust v release +# make sync a checkout of xhandler-rust (RUST_REPO), for development +# make verify-core checks that the committed core is the one released as its VERSION +# make verify-release TAG=v +# what releasing requires: the core of the xhandler-rust +# release of the same version +# +# The package version is the core's (src/xhandler/_core/VERSION). RUST_REPO ?= ../xhandler-rust -DIST := $(RUST_REPO)/dist +CORE ?= +DIST := $(if $(CORE),.dist,$(RUST_REPO)/dist) -.PHONY: sync dist test lint - -dist: - $(MAKE) -C $(RUST_REPO)/xhandler-wasm dist +.PHONY: sync dist verify-core verify-release test lint sync: dist cp $(DIST)/xhandler.wasm $(DIST)/VERSION src/xhandler/_core/ cp $(DIST)/models/python/models.py src/xhandler/xbuilder/models.py rm -rf testdata/conformance && cp -r $(DIST)/conformance testdata/conformance + $(if $(CORE),rm -rf $(DIST)) + +dist: +ifeq ($(CORE),) + $(MAKE) -C $(RUST_REPO)/xhandler-wasm dist +else + ./hack/core.sh download $(CORE) $(DIST) +endif + +verify-core: + ./hack/core.sh verify src/xhandler/_core/xhandler.wasm src/xhandler/_core/VERSION + +verify-release: + ./hack/core.sh verify-release $(TAG) src/xhandler/_core/xhandler.wasm src/xhandler/_core/VERSION test: uv run pytest diff --git a/hack/core.sh b/hack/core.sh new file mode 100755 index 0000000..4f7462c --- /dev/null +++ b/hack/core.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# The xhandler core (xhandler.wasm, its models and the conformance corpus) comes from the +# releases of xhandler-rust, which attach xhandler-dist-.tar.gz, xhandler.wasm and +# SHA256SUMS. This script is identical in xhandler-go, xhandler-python and xhandler-java. +# +# core.sh download +# extracts the release's dist into +# core.sh verify +# checks that is the xhandler.wasm released as the version in ; a +# version that was never released (a core built from a checkout) only warns +# core.sh verify-release +# what releasing requires: the version in is the tag's and is +# the xhandler.wasm of that xhandler-rust release +set -euo pipefail + +releases=${XHANDLER_RUST_RELEASES:-https://github.com/project-openubl/xhandler-rust/releases/download} + +sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1"; else shasum -a 256 "$1"; fi | cut -d' ' -f1; } +# The checksum of in the SHA256SUMS of release ; empty if there is none +released_sha256() { curl -fsSL "$releases/v$1/SHA256SUMS" 2>/dev/null | awk -v asset="$2" '$2 == asset {print $1}' || true; } + +case "${1:-}" in +download) + version=$2 dir=$3 asset="xhandler-dist-$2.tar.gz" + tmp=$(mktemp -d) + trap 'rm -rf "$tmp"' EXIT + curl -fsSL -o "$tmp/$asset" "$releases/v$version/$asset" + if [ "$(sha256 "$tmp/$asset")" != "$(released_sha256 "$version" "$asset")" ]; then + echo "error: $asset does not match the SHA256SUMS of xhandler-rust v$version" >&2 + exit 1 + fi + rm -rf "$dir" && mkdir -p "$dir" && tar -xzf "$tmp/$asset" -C "$dir" + ;; +verify | verify-release) + if [ "$1" = verify-release ]; then + tag=$2 + shift + fi + wasm=$2 version=$(cat "$3") + if [ -n "${tag:-}" ] && [ "v$version" != "$tag" ]; then + echo "error: the core is $version, not ${tag#v}: run make sync CORE=${tag#v}" >&2 + exit 1 + fi + expected=$(released_sha256 "$version" xhandler.wasm) + if [ -z "$expected" ]; then + message="the core $version ($wasm) is not an xhandler-rust release: sync a release with 'make sync CORE='" + if [ -n "${tag:-}" ]; then + echo "error: $message" >&2 + exit 1 + fi + echo "warning: $message" >&2 + elif [ "$(sha256 "$wasm")" != "$expected" ]; then + echo "error: $wasm is not the xhandler.wasm of xhandler-rust v$version" >&2 + exit 1 + else + echo "$wasm is the xhandler.wasm of xhandler-rust v$version" + fi + ;; +*) + echo "usage: core.sh download | verify | verify-release " >&2 + exit 2 + ;; +esac diff --git a/pyproject.toml b/pyproject.toml index b6e98ab..ee4299e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,7 @@ [project] name = "xhandler" -version = "0.1.0" +# The version of the core it carries (make sync), e.g. 6.0.0-alpha.1 -> 6.0.0a1 +dynamic = ["version"] description = "Create, sign and send the electronic documents (UBL XML) of SUNAT Peru" readme = "README.md" license = "Apache-2.0" @@ -22,6 +23,10 @@ dev = [ requires = ["hatchling"] build-backend = "hatchling.build" +[tool.hatch.version] +path = "src/xhandler/_core/VERSION" +pattern = "(?P.+)" + [tool.hatch.build.targets.wheel] packages = ["src/xhandler"] diff --git a/uv.lock b/uv.lock index 14fb6a6..828914e 100644 --- a/uv.lock +++ b/uv.lock @@ -742,7 +742,6 @@ wheels = [ [[package]] name = "xhandler" -version = "0.1.0" source = { editable = "." } dependencies = [ { name = "httpx" },