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 .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto eol=lf
tests/*.json -text
76 changes: 76 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Python installed wheels
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
permissions:
contents: read
concurrency:
group: python-ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
native:
name: Native stage (${{ matrix.os }})
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: '1.95.0'
- name: Build the pinned public native library once
run: python scripts/ci.py build-native
- uses: actions/upload-artifact@v4
with:
name: native-${{ matrix.os }}
include-hidden-files: true
path: .native/*
if-no-files-found: error
retention-days: 3
- uses: actions/upload-artifact@v4
if: always()
with:
name: native-evidence-${{ matrix.os }}
path: .ci-evidence/*
include-hidden-files: true
if-no-files-found: warn
retention-days: 3
installed:
name: Installed wheel (${{ matrix.os }}, Python ${{ matrix.python }})
needs: native
runs-on: ${{ matrix.os }}
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- uses: actions/download-artifact@v4
with:
name: native-${{ matrix.os }}
path: .native
- name: Build the wheel and install it in a fresh environment
run: python scripts/ci.py install-wheel
- name: Test every repository fixture against the installed wheel
run: python scripts/ci.py test-installed
- uses: actions/upload-artifact@v4
if: always()
with:
name: evidence-${{ matrix.os }}-${{ matrix.python }}
include-hidden-files: true
path: .ci-evidence/*
if-no-files-found: warn
retention-days: 7
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/build/
/dist/
*.egg-info/
__pycache__/
# CI working folders
.ci-evidence/
.ci-venv/
.ci-build-venv/
.native/
.native-src/
dist/
33 changes: 33 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Licenses & Copyright
====================

This license file adheres to the formatting guidelines of
[readable-licenses](https://github.com/nevir/readable-licenses).


Ribose's BSD 2-Clause License
-----------------------------

Copyright (c) 2021-2025, [Ribose Inc](https://www.ribose.com).
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# unitsml-python
# UnitsML for Python

UnitsML unit models, rendering and conversion library.
`unitsml.evaluate(request)` evaluates exact string magnitudes and unit
operands. The structural request accepts a producer model or terms
containing unit identifiers, optional prefixes and exact powers. For an
`expression` operand, the installed AsciiUnits package supplies that same
structural model. Both packages use their native libraries.

Implementation is being prepared on a separate task branch.
```python
import unitsml

result = unitsml.evaluate({
"op": "convert",
"left": {"value": "1", "expression": "km"},
"right": {"expression": "m"},
})
print(result["magnitude"])
```

The native result preserves exact rational text and quantity metadata.
`evaluate_wire` accepts JSON text or UTF-8 bytes and exposes the full
ABI outcome. `evaluate` raises typed errors carrying the outcome class,
native code and detail. AsciiUnits refusals preserve their byte offsets.

Install the matching AsciiUnits and UnitsML host wheels together. The
UnitsML wheel verifies its native payload digest on load.
44 changes: 44 additions & 0 deletions ci/native-source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"package": "unitsml",
"planned_repository": "unitsml/unitsml-python",
"qualified_source_head": "f10d4aaf87345680108394367570ddf179bfa72c",
"rust_toolchain": "1.95.0",
"native": {
"kind": "prebuilt-library",
"repository": "unitsml/unitsml-rs",
"local": "workspace/units-native/unitsml-rs",
"revision": "fd3b3cbdd3bb91f732e5c8f2d76ff86a8168ec49",
"public": true,
"deps": {
"unitsdb_rs": {
"repository": "unitsml/unitsdb-rs",
"revision": "c631488e03f58c5cfd0ada369948b946a23a4f42",
"public": true,
"checkout_as": "unitsdb-rs"
},
"asciiunits_python": {
"repository": "unitsml/asciiunits-python",
"revision": "2e85f6c9320199cbcba1e91556b6722723ef5d8d",
"public": true,
"build_wheel": true,
"note": "merged unitsml/asciiunits-python main; carries the qualified 5c185e27 sources plus delivery CI"
},
"f40_corpus": {
"repository": "unitsml/unitsml-testsuite",
"revision": "877530fd9a04ed8c9026dafc2ee5127215e2bd00",
"public": true
}
},
"library_stem": "unitsml_rs",
"binary_policy": "CI builds the pinned clean source with scripts/build-native-receipt.py; build-wheel.py --build-receipt validates commit, Cargo.lock, platform, metadata and the actual library hash. Without a receipt the fixed library_sha256 pin is enforced."
},
"data": null,
"tests": [
"tests/test_installed.py"
],
"python_runtime": {
"repository": "parsanol/parsanol-python",
"revision": "7bb1e554ec29dcd82ee7a2c467bc46309170ceeb",
"note": "only for packages that load Parsanol at runtime; none of these five does (native stages are self-contained)"
}
}
13 changes: 13 additions & 0 deletions native-source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"native_source_commit": "fd3b3cbdd3bb91f732e5c8f2d76ff86a8168ec49",
"library_sha256": "b27039135260c41d801f49064f0d066bb8e4ba7636c6af8cac2d75b63b4baa9a",
"cargo_lock_sha256": "26c4f9e73ce68a99d08dc2bfb69b8f268cc0fb8aa3b3928d7cc03ecaccdd33d1",
"unitsdb_commit": "c631488e03f58c5cfd0ada369948b946a23a4f42",
"unitsdb_data_sha256": "660b471fd895d99a74849adaf1cfb0ec4570cd5dbcd9ebf695970af0dc0b3c97",
"asciiunits_python_commit": "5c185e271220604648da2a036b4dc94c729fa952",
"asciiunits_wheel_sha256": "0f83d4cb6c9a9897a9ce317f5442c961ccc0d8bb781d4caa893d0eab48471de9",
"f40_corpus_commit": "877530fd9a04ed8c9026dafc2ee5127215e2bd00",
"f40_payload_sha256": "2cacb0b5b3c66f7c8a960b73e638b892afb86dd44ee4838371f132c2c5b60218",
"target": "x86_64-unknown-linux-gnu",
"platform_tag": "linux_x86_64"
}
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[build-system]
requires = ["setuptools>=69", "wheel>=0.43"]
build-backend = "setuptools.build_meta"

[project]
name = "unitsml"
version = "0.1.0"
description = "Exact native UnitsML quantity operations"
requires-python = ">=3.9"
license = {file = "LICENSE.md"}
readme = "README.md"
dependencies = ["asciiunits==0.1.0"]

[tool.setuptools.packages.find]
where = ["src"]
85 changes: 85 additions & 0 deletions scripts/build-native-receipt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Build the pinned native library from a clean pinned checkout and write a build receipt.

The receipt records what CI actually built: source commit, Cargo.lock hash, target and
platform, compiler and Cargo versions, the exact build invocation, and the resulting
library hash. scripts/build-wheel.py --build-receipt validates this receipt against
the repository pins and the actual binary before packing. It does not replace the pinned
library_sha256 path, which stays the default.
"""

import argparse
from hashlib import sha256
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import sysconfig

ROOT = Path(__file__).resolve().parents[1]


def digest(path):
return sha256(Path(path).read_bytes()).hexdigest()


def main():
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument("--rust-source", required=True, type=Path)
cli.add_argument("--target-dir", required=True, type=Path)
cli.add_argument("--out-dir", required=True, type=Path)
cli.add_argument("--pins", type=Path, default=ROOT / "native-source.json")
cli.add_argument("--library-name", required=True, help="cdylib file name, e.g. libomml.so")
args = cli.parse_args()
source = args.rust_source.resolve()
pins = json.loads(args.pins.read_bytes())
revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=source, text=True).strip()
if revision != pins["native_source_commit"]:
raise SystemExit(f"refusing to build: checkout is {revision}, pinned {pins['native_source_commit']}")
if subprocess.check_output(["git", "status", "--porcelain", "--untracked-files=all"], cwd=source):
raise SystemExit("refusing to build: native checkout is not clean")
lock = digest(source / "Cargo.lock")
if lock != pins["cargo_lock_sha256"]:
raise SystemExit(f"refusing to build: Cargo.lock {lock} differs from pinned {pins['cargo_lock_sha256']}")
library_name = args.library_name
rustc = subprocess.check_output(["rustc", "-vV"], text=True)
host = next(l.removeprefix("host: ") for l in rustc.splitlines() if l.startswith("host: "))
cargo = subprocess.check_output(["cargo", "-V"], text=True).strip()
target = args.target_dir.resolve()
invocation = ["cargo", "build", "--locked", "--release", "--lib"]
for feature in pins.get("features", []):
invocation += ["--features", feature]
env = dict(os.environ, CARGO_TARGET_DIR=str(target))
subprocess.run(invocation, cwd=source, env=env, check=True)
built = target / "release" / library_name
if not built.is_file():
raise SystemExit(f"cargo did not produce {library_name}")
out = args.out_dir.resolve()
out.mkdir(parents=True, exist_ok=True)
library = out / library_name
shutil.copy2(built, library)
metadata = out / "cargo-metadata.json"
metadata.write_bytes(subprocess.check_output(
["cargo", "metadata", "--locked", "--format-version", "1", "--filter-platform", host], cwd=source, env=env))
receipt = {
"kind": "native-build-receipt/v1",
"native_source_commit": revision,
"cargo_lock_sha256": lock,
"platform_tag": sysconfig.get_platform().replace("-", "_").replace(".", "_"),
"target": host,
"rustc": rustc.strip().splitlines()[0],
"rustc_verbose": rustc.strip(),
"cargo": cargo,
"invocation": invocation,
"profile": "release",
"library": library_name,
"library_sha256": digest(library),
"cargo_metadata_sha256": digest(metadata),
}
(out / "build-receipt.json").write_text(json.dumps(receipt, indent=2) + "\n")
print(json.dumps({k: receipt[k] for k in ("native_source_commit", "platform_tag", "library", "library_sha256")}))


if __name__ == "__main__":
sys.exit(main())
Loading
Loading