Skip to content

Commit 49628ca

Browse files
authored
Include object_store version and source in Python dist (#408)
* Include object_store version and source * Add version information
1 parent 1b2fcc0 commit 49628ca

File tree

7 files changed

+134
-10
lines changed

7 files changed

+134
-10
lines changed

Cargo.lock

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

obstore/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ url = { workspace = true }
4545
reqwest = { version = "*", default-features = false, features = [
4646
"rustls-tls-native-roots",
4747
] }
48+
49+
[build-dependencies]
50+
cargo-lock = "10.1.0"

obstore/build.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use cargo_lock::{Lockfile, SourceId, Version};
2+
use std::ffi::OsString;
3+
use std::io::ErrorKind;
4+
use std::path::{Path, PathBuf};
5+
use std::{env, io};
6+
7+
fn main() {
8+
let lockfile_location = get_lockfile_location().unwrap();
9+
let (version, source) = read_lockfile(&lockfile_location);
10+
11+
println!("cargo:rustc-env=OBJECT_STORE_VERSION={}", version);
12+
println!(
13+
"cargo:rustc-env=OBJECT_STORE_SOURCE={}",
14+
source.map(|s| s.to_string()).unwrap_or("".to_string())
15+
);
16+
}
17+
18+
fn get_lockfile_location() -> io::Result<PathBuf> {
19+
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
20+
21+
let cargo_lock = OsString::from("Cargo.lock");
22+
23+
for ancestor in path.as_path().ancestors() {
24+
for entry in ancestor.read_dir()? {
25+
let entry = entry?;
26+
if entry.file_name() == cargo_lock {
27+
return Ok(entry.path());
28+
}
29+
}
30+
}
31+
32+
Err(io::Error::new(
33+
ErrorKind::NotFound,
34+
"Ran out of places to find Cargo.toml",
35+
))
36+
}
37+
38+
fn read_lockfile(path: &Path) -> (Version, Option<SourceId>) {
39+
let lockfile = Lockfile::load(path).unwrap();
40+
let idx = lockfile
41+
.packages
42+
.iter()
43+
.position(|p| p.name.as_str() == "object_store")
44+
.unwrap();
45+
let package = &lockfile.packages[idx];
46+
(package.version.clone(), package.source.clone())
47+
}

obstore/python/obstore/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
from . import _obstore, store
44
from ._obstore import * # noqa: F403
5-
from ._obstore import ___version
65

76
if TYPE_CHECKING:
87
from . import exceptions # noqa: TC004
98

10-
__version__: str = ___version()
119

12-
__all__ = ["__version__", "exceptions", "store"]
10+
__all__ = ["exceptions", "store"]
1311
__all__ += _obstore.__all__

obstore/python/obstore/_obstore.pyi

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ from ._rename import rename, rename_async
4141
from ._scheme import parse_scheme
4242
from ._sign import HTTP_METHOD, SignCapableStore, sign, sign_async
4343

44-
def ___version() -> str: ...
44+
__version__: str
45+
_object_store_version: str
46+
_object_store_source: str
4547

4648
__all__ = [
4749
"HTTP_METHOD",
@@ -65,6 +67,9 @@ __all__ = [
6567
"SuffixRange",
6668
"UpdateVersion",
6769
"WritableFile",
70+
"__version__",
71+
"_object_store_source",
72+
"_object_store_version",
6873
"_store",
6974
"copy",
7075
"copy_async",

obstore/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ mod utils;
2020
use pyo3::prelude::*;
2121

2222
const VERSION: &str = env!("CARGO_PKG_VERSION");
23-
24-
#[pyfunction]
25-
fn ___version() -> &'static str {
26-
VERSION
27-
}
23+
const OBJECT_STORE_VERSION: &str = env!("OBJECT_STORE_VERSION");
24+
const OBJECT_STORE_SOURCE: &str = env!("OBJECT_STORE_SOURCE");
2825

2926
/// Raise RuntimeWarning for debug builds
3027
#[pyfunction]
@@ -51,7 +48,9 @@ fn check_debug_build(_py: Python) -> PyResult<()> {
5148
fn _obstore(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
5249
check_debug_build(py)?;
5350

54-
m.add_wrapped(wrap_pyfunction!(___version))?;
51+
m.add("__version__", VERSION)?;
52+
m.add("_object_store_version", OBJECT_STORE_VERSION)?;
53+
m.add("_object_store_source", OBJECT_STORE_SOURCE)?;
5554

5655
pyo3_object_store::register_store_module(py, m, "obstore", "_store")?;
5756
pyo3_object_store::register_exceptions_module(py, m, "obstore", "exceptions")?;

tests/test_version.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from obstore import __version__, _object_store_source, _object_store_version
2+
3+
4+
def test_versions_are_str():
5+
assert isinstance(__version__, str)
6+
assert isinstance(_object_store_version, str)
7+
assert isinstance(_object_store_source, str)

0 commit comments

Comments
 (0)