Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 57 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__/
.mypy_cache/
.ruff_cache/
dist/
/.dist/
7 changes: 5 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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=<version> the xhandler-rust v<version> 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<version>
# what releasing <version> 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
Expand Down
63 changes: 63 additions & 0 deletions hack/core.sh
Original file line number Diff line number Diff line change
@@ -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-<version>.tar.gz, xhandler.wasm and
# SHA256SUMS. This script is identical in xhandler-go, xhandler-python and xhandler-java.
#
# core.sh download <version> <dir>
# extracts the release's dist into <dir>
# core.sh verify <wasm> <version-file>
# checks that <wasm> is the xhandler.wasm released as the version in <version-file>; a
# version that was never released (a core built from a checkout) only warns
# core.sh verify-release <tag> <wasm> <version-file>
# what releasing <tag> requires: the version in <version-file> is the tag's and <wasm> 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 <asset> in the SHA256SUMS of release <version>; 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=<version>'"
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 <version> <dir> | verify <wasm> <version-file> | verify-release <tag> <wasm> <version-file>" >&2
exit 2
;;
esac
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,6 +23,10 @@ dev = [
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.version]
path = "src/xhandler/_core/VERSION"
pattern = "(?P<version>.+)"

[tool.hatch.build.targets.wheel]
packages = ["src/xhandler"]

Expand Down
1 change: 0 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading