Skip to content

Commit 4420323

Browse files
authored
Merge pull request #172 from projectsyn/feat/buildinfo
Add `buildinfo()` Python method
2 parents feb7982 + cba65ec commit 4420323

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ serde_yaml = "0.9.34"
3131
walkdir = "2.5.0"
3232
yaml-merge-keys = { version = "0.8.1", features = ["serde_yaml"] }
3333

34+
[build-dependencies]
35+
rustc_version = "0.4.1"
36+
3437
[dev-dependencies]
3538
criterion = "0.6.0"
3639
paste = "1.0.14"

build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
5+
use rustc_version::version_meta;
6+
7+
fn main() {
8+
let rustc_version = version_meta().unwrap().short_version_string;
9+
let out_dir = env::var("OUT_DIR").unwrap();
10+
let dest_path = Path::new(&out_dir).join("rustc_version.rs");
11+
fs::write(
12+
&dest_path,
13+
format!(r#"static RUSTC_VERSION: &str = "{rustc_version}";"#,),
14+
)
15+
.unwrap();
16+
}

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#![allow(clippy::module_name_repetitions)]
88
#![allow(clippy::similar_names)]
99

10+
// embed RUSTC_VERSION definition which is generated by `build.rs` using the `rustc_version` crate
11+
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));
12+
1013
mod config;
1114
mod fsutil;
1215
mod inventory;
@@ -405,6 +408,13 @@ impl Default for Reclass {
405408
}
406409
}
407410

411+
#[pyfunction]
412+
fn buildinfo() -> HashMap<&'static str, &'static str> {
413+
let mut res = HashMap::new();
414+
res.insert("rustc_version", RUSTC_VERSION);
415+
res
416+
}
417+
408418
#[pymodule]
409419
fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
410420
// Register the top-level `Reclass` Python class which is used to configure the library
@@ -417,6 +427,8 @@ fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
417427
m.add_class::<NodeInfo>()?;
418428
// Register the Inventory class
419429
m.add_class::<Inventory>()?;
430+
// Register the buildinfo method
431+
m.add_function(wrap_pyfunction!(buildinfo, m)?)?;
420432
Ok(())
421433
}
422434

tests/test_buildinfo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import subprocess
2+
import pytest
3+
4+
import reclass_rs
5+
6+
7+
def test_buildinfo():
8+
buildinfo = reclass_rs.buildinfo()
9+
10+
try:
11+
rustc_version = (
12+
subprocess.run(
13+
["rustc", "--version"], stderr=subprocess.STDOUT, stdout=subprocess.PIPE
14+
)
15+
.stdout.decode("utf-8")
16+
.strip()
17+
)
18+
except FileNotFoundError:
19+
pytest.skip("No rustc compiler available in test environment")
20+
assert buildinfo == {"rustc_version": rustc_version}

0 commit comments

Comments
 (0)