Skip to content

Commit

Permalink
ci: check that stubs are up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-dupre committed Feb 3, 2025
1 parent f44a7f2 commit b25cc0b
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 65 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ jobs:
- name: Check version consistency
run: poetry run ./check-version.py

# ---
# --- Check that python stubs are up to date
# ---

check-stubs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: opening-hours-py
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: rustfmt, clippy
- uses: actions/setup-python@v4
with:
python-version: 3.12
- name: Install poetry
run: |
python -m pip install --upgrade pip
pip install poetry
- name: Check that Python stubs are up to date
run: cargo run --bin stub_gen -- check

# ---
# --- Calculate coverage using tarpaulin
# ---
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
opening-hours/data/osm_examples.txt
docs
fuzz/artifacts
opening-hours/data/osm_examples.txt
__pycache__
**/target
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.1

### Python

- Add auto-generated Python stubs.

## 1.0.0

That's not really a huge milestone, but:
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opening-hours"
version = "1.0.0"
version = "1.0.1"
authors = ["Rémi Dupré <remi@dupre.io>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down Expand Up @@ -34,9 +34,9 @@ disable-test-timeouts = []

[dependencies]
chrono = "0.4"
compact-calendar = { path = "compact-calendar", version = "1.0.0" }
compact-calendar = { path = "compact-calendar", version = "1.0.1" }
flate2 = "1.0"
opening-hours-syntax = { path = "opening-hours-syntax", version = "1.0.0" }
opening-hours-syntax = { path = "opening-hours-syntax", version = "1.0.1" }
sunrise-next = "1.2"

# Feature: log (default)
Expand All @@ -51,7 +51,7 @@ tzf-rs = { version = "0.4", default-features = false, optional = true }

[build-dependencies]
chrono = "0.4"
compact-calendar = { path = "compact-calendar", version = "1.0.0" }
compact-calendar = { path = "compact-calendar", version = "1.0.1" }
country-boundaries = { version = "1.2", optional = true }
flate2 = "1.0"
rustc_version = "0.4.0"
Expand Down
2 changes: 1 addition & 1 deletion compact-calendar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compact-calendar"
version = "1.0.0"
version = "1.0.1"
authors = ["Rémi Dupré <remi@dupre.io>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions opening-hours-py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opening-hours-py"
version = "1.0.0"
version = "1.0.1"
authors = ["Rémi Dupré <remi@dupre.io>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -23,12 +23,12 @@ pyo3-stub-gen = "0.7"
[dependencies.opening-hours-rs]
package = "opening-hours"
path = ".."
version = "1.0.0"
version = "1.0.1"
features = ["log", "auto-country", "auto-timezone"]

[dependencies.opening-hours-syntax]
path = "../opening-hours-syntax"
version = "1.0.0"
version = "1.0.1"
features = ["log"]

[dependencies.pyo3]
Expand Down
54 changes: 54 additions & 0 deletions opening-hours-py/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Run stub generation.
//! See https://github.com/Jij-Inc/pyo3-stub-gen
use std::fs::File;
use std::io::Read;

const STUB_SOURCE_PATH: &str = "opening_hours.pyi";
const STUB_TARGET_PATH: &str = "../opening_hours.pyi";

// ⚠️ Do not copy this code as it is optimized for concision and not performance.
fn load_file(path: &str) -> Vec<u8> {
let mut file = File::open(path).unwrap_or_else(|err| panic!("could not load {path:?}: {err}"));
let mut res = Vec::new();

file.read_to_end(&mut res)
.unwrap_or_else(|err| panic!("could not read content from {path:?}: {err}"));

res
}

fn main() -> pyo3_stub_gen::Result<()> {
let is_subcommand_check = match std::env::args().nth(1).as_deref() {
None => false,
Some("check") => true,
Some(x) => panic!("Unknown subcommand {x:?}"),
};

let stub = opening_hours::stub_info()?;
stub.generate()?;

std::process::Command::new("poetry")
.args(["run", "ruff", "format", STUB_SOURCE_PATH])
.output()
.expect("failed to format stubs with ruff");

let changed = load_file(STUB_SOURCE_PATH) != load_file(STUB_TARGET_PATH);

if changed {
if is_subcommand_check {
std::fs::remove_file(STUB_SOURCE_PATH).expect("could not remove stub file");
panic!("File changed!");
} else {
println!("Installing new stubs.");

std::fs::rename(STUB_SOURCE_PATH, STUB_TARGET_PATH)
.expect("could not move stub to project's root");
}
} else {
println!("No change detected.");
std::fs::remove_file(STUB_SOURCE_PATH).expect("could not remove stub file");
}

Ok(())
}
18 changes: 18 additions & 0 deletions opening-hours-py/src/types/timezone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use pyo3::prelude::*;
use pyo3_stub_gen::{PyStubType, TypeInfo};

/// Tiny wrapper that is here to extend type with stub generation.
#[derive(FromPyObject, IntoPyObject)]
pub(crate) struct TimeZoneWrapper(chrono_tz::Tz);

impl From<TimeZoneWrapper> for chrono_tz::Tz {
fn from(val: TimeZoneWrapper) -> Self {
val.0
}
}

impl PyStubType for TimeZoneWrapper {
fn type_output() -> TypeInfo {
TypeInfo::with_module("zoneinfo.ZoneInfo", "zoneinfo".into())
}
}
2 changes: 1 addition & 1 deletion opening-hours-syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opening-hours-syntax"
version = "1.0.0"
version = "1.0.1"
authors = ["Rémi Dupré <remi@dupre.io>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
Loading

0 comments on commit b25cc0b

Please sign in to comment.