Skip to content

Commit efa794d

Browse files
authored
Merge pull request #309 from terraphim/claude/python-terraphim-binding-01Q2ipPnyrDiCBrDj8xPqE7j
Python binding for Terraphim automata with tests
2 parents 925fc16 + 0fc9b1d commit efa794d

19 files changed

Lines changed: 3307 additions & 0 deletions
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
name: Python Bindings CI/CD
2+
3+
on:
4+
push:
5+
branches: [main, develop, "claude/**"]
6+
paths:
7+
- "crates/terraphim_automata_py/**"
8+
- "crates/terraphim_automata/**"
9+
- "crates/terraphim_types/**"
10+
- ".github/workflows/python-bindings.yml"
11+
pull_request:
12+
branches: [main, develop]
13+
paths:
14+
- "crates/terraphim_automata_py/**"
15+
- "crates/terraphim_automata/**"
16+
- "crates/terraphim_types/**"
17+
- ".github/workflows/python-bindings.yml"
18+
release:
19+
types: [published]
20+
21+
env:
22+
CARGO_TERM_COLOR: always
23+
RUST_BACKTRACE: 1
24+
25+
jobs:
26+
lint:
27+
name: Lint Python Code
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v4
31+
32+
- name: Set up Python
33+
uses: actions/setup-python@v5
34+
with:
35+
python-version: "3.12"
36+
37+
- name: Install uv
38+
uses: astral-sh/setup-uv@v5
39+
with:
40+
enable-cache: true
41+
42+
- name: Install dependencies
43+
run: uv pip install --system black ruff mypy
44+
45+
- name: Check formatting with Black
46+
working-directory: crates/terraphim_automata_py
47+
run: black --check python/
48+
49+
- name: Lint with Ruff
50+
working-directory: crates/terraphim_automata_py
51+
run: ruff check python/
52+
53+
- name: Type check with mypy
54+
working-directory: crates/terraphim_automata_py
55+
run: mypy python/terraphim_automata/ --ignore-missing-imports
56+
continue-on-error: true
57+
58+
test:
59+
name: Test on ${{ matrix.os }} - Python ${{ matrix.python-version }}
60+
runs-on: ${{ matrix.os }}
61+
strategy:
62+
fail-fast: false
63+
matrix:
64+
os: [ubuntu-latest, macos-latest, windows-latest]
65+
python-version: ["3.9", "3.10", "3.11", "3.12"]
66+
67+
steps:
68+
- uses: actions/checkout@v4
69+
70+
- name: Set up Rust
71+
uses: dtolnay/rust-toolchain@stable
72+
73+
- name: Cache Rust dependencies
74+
uses: Swatinem/rust-cache@v2
75+
with:
76+
workspaces: "crates/terraphim_automata_py -> target"
77+
78+
- name: Install uv
79+
uses: astral-sh/setup-uv@v5
80+
with:
81+
enable-cache: true
82+
83+
- name: Set up Python ${{ matrix.python-version }}
84+
uses: actions/setup-python@v5
85+
with:
86+
python-version: ${{ matrix.python-version }}
87+
88+
- name: Install maturin
89+
run: uv pip install --system maturin
90+
91+
- name: Build Python package
92+
working-directory: crates/terraphim_automata_py
93+
run: maturin develop
94+
95+
- name: Install test dependencies
96+
run: uv pip install --system pytest pytest-cov
97+
98+
- name: Run tests
99+
working-directory: crates/terraphim_automata_py
100+
run: pytest python/tests/ -v --cov=terraphim_automata --cov-report=xml --cov-report=term
101+
102+
- name: Upload coverage to Codecov
103+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
104+
uses: codecov/codecov-action@v4
105+
with:
106+
files: crates/terraphim_automata_py/coverage.xml
107+
flags: python-bindings
108+
name: python-${{ matrix.python-version }}
109+
fail_ci_if_error: false
110+
continue-on-error: true
111+
112+
benchmark:
113+
name: Benchmark Performance
114+
runs-on: ubuntu-latest
115+
steps:
116+
- uses: actions/checkout@v4
117+
118+
- name: Set up Rust
119+
uses: dtolnay/rust-toolchain@stable
120+
121+
- name: Cache Rust dependencies
122+
uses: Swatinem/rust-cache@v2
123+
with:
124+
workspaces: "crates/terraphim_automata_py -> target"
125+
126+
- name: Install uv
127+
uses: astral-sh/setup-uv@v5
128+
with:
129+
enable-cache: true
130+
131+
- name: Set up Python
132+
uses: actions/setup-python@v5
133+
with:
134+
python-version: "3.12"
135+
136+
- name: Install maturin
137+
run: uv pip install --system maturin
138+
139+
- name: Build Python package (release mode)
140+
working-directory: crates/terraphim_automata_py
141+
run: maturin develop --release
142+
143+
- name: Install benchmark dependencies
144+
run: uv pip install --system pytest pytest-benchmark
145+
146+
- name: Run benchmarks
147+
working-directory: crates/terraphim_automata_py
148+
run: |
149+
pytest python/benchmarks/ -v --benchmark-only \
150+
--benchmark-json=benchmark-results.json \
151+
--benchmark-columns=min,max,mean,stddev,median,ops
152+
153+
- name: Store benchmark results
154+
uses: actions/upload-artifact@v4
155+
with:
156+
name: benchmark-results
157+
path: crates/terraphim_automata_py/benchmark-results.json
158+
159+
build-wheels:
160+
name: Build wheels on ${{ matrix.os }}
161+
runs-on: ${{ matrix.os }}
162+
if: github.event_name == 'release'
163+
strategy:
164+
fail-fast: false
165+
matrix:
166+
os: [ubuntu-latest, macos-latest, windows-latest]
167+
168+
steps:
169+
- uses: actions/checkout@v4
170+
171+
- name: Set up Rust
172+
uses: dtolnay/rust-toolchain@stable
173+
174+
- name: Install uv
175+
uses: astral-sh/setup-uv@v5
176+
177+
- name: Build wheels
178+
uses: PyO3/maturin-action@v1
179+
with:
180+
working-directory: crates/terraphim_automata_py
181+
args: --release --out dist --find-interpreter
182+
sccache: "true"
183+
manylinux: auto
184+
185+
- name: Upload wheels
186+
uses: actions/upload-artifact@v4
187+
with:
188+
name: wheels-${{ matrix.os }}
189+
path: crates/terraphim_automata_py/dist
190+
191+
build-sdist:
192+
name: Build source distribution
193+
runs-on: ubuntu-latest
194+
if: github.event_name == 'release'
195+
steps:
196+
- uses: actions/checkout@v4
197+
198+
- name: Build sdist
199+
uses: PyO3/maturin-action@v1
200+
with:
201+
working-directory: crates/terraphim_automata_py
202+
command: sdist
203+
args: --out dist
204+
205+
- name: Upload sdist
206+
uses: actions/upload-artifact@v4
207+
with:
208+
name: sdist
209+
path: crates/terraphim_automata_py/dist
210+
211+
publish:
212+
name: Publish to PyPI
213+
runs-on: ubuntu-latest
214+
if: github.event_name == 'release'
215+
needs: [lint, test, build-wheels, build-sdist]
216+
environment:
217+
name: pypi
218+
url: https://pypi.org/p/terraphim-automata
219+
permissions:
220+
id-token: write
221+
222+
steps:
223+
- uses: actions/download-artifact@v4
224+
with:
225+
pattern: wheels-*
226+
path: dist
227+
merge-multiple: true
228+
229+
- uses: actions/download-artifact@v4
230+
with:
231+
name: sdist
232+
path: dist
233+
234+
- name: Publish to PyPI
235+
uses: PyO3/maturin-action@v1
236+
with:
237+
command: upload
238+
args: --non-interactive --skip-existing dist/*

Cargo.lock

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Virtual environments
24+
venv/
25+
env/
26+
ENV/
27+
.venv
28+
29+
# Testing
30+
.pytest_cache/
31+
.coverage
32+
coverage.xml
33+
htmlcov/
34+
.tox/
35+
.hypothesis/
36+
benchmark-results.json
37+
38+
# IDEs
39+
.vscode/
40+
.idea/
41+
*.swp
42+
*.swo
43+
*~
44+
.DS_Store
45+
46+
# Rust
47+
target/
48+
Cargo.lock
49+
50+
# Maturin
51+
*.whl

0 commit comments

Comments
 (0)