Skip to content

Commit

Permalink
misc: update to 0.23 for pyo3
Browse files Browse the repository at this point in the history
  • Loading branch information
sbtq committed Nov 25, 2024
1 parent ba3966d commit 8044faa
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 67 deletions.
26 changes: 14 additions & 12 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ edition = "2021"

[dependencies]
keygen-rs = "0.3.0"
pyo3 = { version = "0.22.6", features = ["extension-module", "gil-refs"] }
pyo3-async-runtimes = { version = "0.22.0", features = ["tokio-runtime"] }
pyo3 = { version = "0.23.1", features = ["extension-module", "serde"] }
pyo3-async-runtimes = { version = "0.23.0", features = ["tokio-runtime"] }
chrono = "0.4.38"
openssl = { version = "0.10", features = ["vendored"] }
serde_json = "1.0.132"
serde = "1.0"
serde_json = "1.0"

[lib]
name = "keygen_sh"
Expand Down
36 changes: 36 additions & 0 deletions src/component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use pyo3::prelude::*;
use crate::utils::create_interface;
use keygen_rs::component::Component as KeygenRsComponent;

#[pymodule(name = "component")]
pub fn component_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.component", m)
})?;

m.add_class::<Component>()?;
Ok(())
}

create_interface!(Component, KeygenRsComponent);

#[pymethods]
impl Component {
#[new]
pub fn new(id: String, fingerprint: String, name: String) -> Self {
Self {
inner: KeygenRsComponent { id, fingerprint, name }
}
}

// pub fn create_object(&self) -> serde_json::Value {
// let object = KeygenRsComponent::create_object(&self.inner);
//
// match object {
// _ => object,
// }
// }
}
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use pyo3::{pyclass, pyfunction, pymethods, pymodule, wrap_pyfunction, Bound, PyR
pub fn config_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import_bound("sys")?
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.config", m)
})?;
Expand Down
14 changes: 10 additions & 4 deletions src/date.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use chrono::{DateTime, Utc};
use pyo3::{IntoPy, Py, PyAny, PyObject, Python};
use pyo3::types::PyString;
use pyo3::{Bound, IntoPyObject, PyErr, Python};

#[derive(Debug, Clone, PartialEq)]
pub struct Date(DateTime<Utc>);

impl IntoPy<Py<PyAny>> for Date {
fn into_py(self, py: Python<'_>) -> PyObject {
self.0.to_rfc3339().into_py(py)
impl<'py> IntoPyObject<'py> for Date {
type Target = PyString;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let date_str = self.0.to_rfc3339();
Ok(PyString::new(py, &date_str))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/entitlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pyo3::{pyclass, pymodule, Bound, PyResult, Python};
pub fn entitlement_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import_bound("sys")?
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.entitlement", m)
})?;
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ create_exception!(errors_module, KeygenError, PyException);
#[pymodule]
#[pyo3(name = "_errors")]
pub fn errors_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("KeygenError", m.py().get_type_bound::<KeygenError>())?;
m.add("KeygenError", m.py().get_type::<KeygenError>())?;
Ok(())
}

Expand Down
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ use pyo3::types::{PyList, PyModule};
use pyo3::{pyfunction, pymodule, wrap_pyfunction, wrap_pymodule, Bound, PyAny, PyResult, Python};
use crate::errors::KeygenError;

// These are the mods which are exposed from the keygen-rs lib:
// pub mod component;
// pub mod config;
// pub mod errors;
// pub mod license;
// pub mod license_file;
// pub mod machine;
// pub mod machine_file;

pub(crate) mod date;
pub(crate) mod utils;
pub mod config;
Expand All @@ -13,6 +22,7 @@ pub mod license;
pub mod license_file;
pub mod machine;
pub mod errors;
pub mod component;

#[pyfunction]
fn verify(scheme: SchemeCode, signed_key: &str) -> PyResult<String> {
Expand All @@ -25,8 +35,8 @@ fn verify(scheme: SchemeCode, signed_key: &str) -> PyResult<String> {
#[pyfunction]
#[pyo3(signature = (fingerprints=None, entitlements=None))]
fn validate<'a>(py: Python<'a>, fingerprints: Option<Bound<'a, PyList>>, entitlements: Option<Bound<'a, PyList>>) -> PyResult<Bound<'a, PyAny>> {
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty_bound(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty_bound(py));
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty(py));

let fingerprints_vec = pylist_to_string_slice(fingerprints)?;
let entitlements_vec = pylist_to_string_slice(entitlements)?;
Expand All @@ -36,9 +46,7 @@ fn validate<'a>(py: Python<'a>, fingerprints: Option<Bound<'a, PyList>>, entitle

match result {
Ok(license) => Ok(License::from(license)),
Err(e) => {
Err(KeygenError::from_error(e))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -51,6 +59,7 @@ fn keygen_sh(_: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pymodule!(entitlement::entitlement_module))?;
m.add_wrapped(wrap_pymodule!(license_file::license_file_module))?;
m.add_wrapped(wrap_pymodule!(errors::errors_module))?;
m.add_wrapped(wrap_pymodule!(component::component_module))?;

m.add_function(wrap_pyfunction!(validate, m)?)?;
m.add_function(wrap_pyfunction!(verify, m)?)?;
Expand Down
52 changes: 16 additions & 36 deletions src/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use crate::machine::Machine;
use crate::utils::{create_interface, pylist_to_string_slice};
use keygen_rs;
use keygen_rs::license::License as KeygenRsLicense;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::PyList;
use crate::errors::KeygenError;
use crate::license_file::LicenseFile;

#[pymodule(name = "license")]
pub fn license_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import_bound("sys")?
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.license", m)
})?;
Expand Down Expand Up @@ -75,8 +75,8 @@ impl License {

#[pyo3(signature = (fingerprints=None, entitlements=None))]
fn validate<'a>(&'a self, py: Python<'a>, fingerprints: Option<Bound<'a, PyList>>, entitlements: Option<Bound<'a, PyList>>) -> PyResult<Bound<PyAny>> {
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty_bound(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty_bound(py));
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty(py));

let fingerprints_vec = pylist_to_string_slice(fingerprints)?;
let entitlements_vec = pylist_to_string_slice(entitlements)?;
Expand All @@ -86,17 +86,15 @@ impl License {

match result {
Ok(license) => Ok(License::from(license)),
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}

#[pyo3(signature = (fingerprints=None, entitlements=None))]
fn validate_key<'a>(&'a self, py: Python<'a>, fingerprints: Option<Bound<'a, PyList>>, entitlements: Option<Bound<'a, PyList>>) -> PyResult<Bound<PyAny>> {
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty_bound(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty_bound(py));
let fingerprints = fingerprints.unwrap_or_else(|| PyList::empty(py));
let entitlements = entitlements.unwrap_or_else(|| PyList::empty(py));

let fingerprints_vec = pylist_to_string_slice(fingerprints)?;
let entitlements_vec = pylist_to_string_slice(entitlements)?;
Expand All @@ -106,19 +104,15 @@ impl License {

match result {
Ok(license) => Ok(License::from(license)),
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}

fn verify(&self) -> PyResult<Vec<u8>> {
match self.inner.verify() {
Ok(resp) => Ok(resp),
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
}

Expand All @@ -130,9 +124,7 @@ impl License {
let result = my_struct.inner.activate(&fingerprint, &[]).await;
match result {
Ok(machine) => Ok(Machine::from(machine)),
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -144,9 +136,7 @@ impl License {
let result = my_struct.inner.deactivate(&id).await;
match result {
Ok(()) => Ok(()),
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -157,12 +147,8 @@ impl License {
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let result = my_struct.inner.machine(&id).await;
match result {
Ok(machine) => {
Ok(Machine::from(machine))
},
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Ok(machine) => Ok(Machine::from(machine)),
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -178,9 +164,7 @@ impl License {
Machine::from(m.clone())
}).collect::<Vec<Machine>>())
},
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -203,9 +187,7 @@ impl License {
)
}).collect::<Vec<Entitlement>>())
},
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand All @@ -220,9 +202,7 @@ impl License {
Ok(lf) => {
Ok(LicenseFile::from(lf))
},
Err(e) => {
Err(PyRuntimeError::new_err(e.to_string()))
},
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
Expand Down
Loading

0 comments on commit 8044faa

Please sign in to comment.