Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions kernels-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod git;
pub mod hf;
pub mod lock;
pub mod metadata;
pub mod signing;
pub mod version;
1 change: 1 addition & 0 deletions kernels-common/src/signing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod receipt;
30 changes: 30 additions & 0 deletions kernels-common/src/signing/receipt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};

use crate::git::Oid;

/// Kernel location.
///
/// Every variant must change when the kernel changes, e.g. through
/// an update. For a remote kernel, this is determined by the revision,
/// for local kernels this could e.g. be based on the file
/// names/sizes/mtimes.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum KernelLocation {
RemoteKernel {
repo_id: String,
revision: Oid,
variant: String,
},
}

impl KernelLocation {
/// A Hub kernel.
pub fn remote(repo_id: impl Into<String>, revision: Oid, variant: impl Into<String>) -> Self {
KernelLocation::RemoteKernel {
repo_id: repo_id.into(),
revision,
variant: variant.into(),
}
}
}
50 changes: 50 additions & 0 deletions kernels/rust/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::str::FromStr;

use kernels_common::git::Oid;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

/// A git object identifier.
#[pyclass(name = "Oid", frozen, eq, hash, ord)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct PyOid {
inner: Oid,
}

impl From<Oid> for PyOid {
fn from(inner: Oid) -> Self {
Self { inner }
}
}

impl PyOid {
pub(crate) fn into_inner(self) -> Oid {
self.inner
}
}

/// Parse a git object id, mapping a parse failure to a Python `ValueError`.
pub(crate) fn parse_oid(s: &str) -> PyResult<Oid> {
Oid::from_str(s).map_err(|err| PyValueError::new_err(err.to_string()))
}

#[pymethods]
impl PyOid {
/// Parse a full SHA-1 or SHA-256 object id.
///
/// Abbreviated identifiers are rejected: an object id must identify the
/// object unambiguously and permanently.
#[staticmethod]
#[pyo3(name = "from_str")]
fn py_from_str(s: &str) -> PyResult<Self> {
parse_oid(s).map(Into::into)
}

fn __str__(&self) -> &str {
self.inner.as_str()
}

fn __repr__(&self) -> String {
format!("Oid({:?})", self.inner.as_str())
}
}
10 changes: 8 additions & 2 deletions kernels/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ use pyo3::exceptions::{PyException, PyOSError, PyRuntimeError, PyValueError};
use pyo3::prelude::*;

mod config;
mod git;
mod lock;
mod signing;
mod version;

use config::{PyBuild, PyGeneral};
use git::PyOid;
use lock::{PyKernelLock, PyKernelLocks, PyKernelPaths, PyNixKernelLock, PyNixKernelLocks};
use signing::PyKernelLocation;
use version::PyVersion;

/// A validated kernel name matching `^[a-z][-a-z0-9]*[a-z0-9]$`.
Expand Down Expand Up @@ -192,8 +196,8 @@ impl From<GitStatus> for PyGitStatus {
#[pymethods]
impl PyGitStatus {
#[getter]
fn commit(&self) -> &str {
self.commit.as_str()
fn commit(&self) -> PyOid {
self.commit.clone().into()
}

#[getter]
Expand Down Expand Up @@ -758,6 +762,7 @@ fn data_py(m: &PyBound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyGitStatus>()?;
m.add_class::<PyKernelBuilderVersion>()?;
m.add_class::<PyKernelName>()?;
m.add_class::<PyOid>()?;
m.add_class::<PyKernelVersion>()?;
m.add_class::<PyKernelDependency>()?;
m.add_class::<PyKernelLock>()?;
Expand All @@ -772,6 +777,7 @@ fn data_py(m: &PyBound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyDigestAlgorithm>()?;
m.add_class::<PyDigest>()?;
m.add_class::<PyDigestViolation>()?;
m.add_class::<PyKernelLocation>()?;
m.add(
"DigestValidationError",
m.py().get_type::<DigestValidationError>(),
Expand Down
15 changes: 5 additions & 10 deletions kernels/rust/lock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::str::FromStr;

use kernels_common::git::Oid;
use kernels_common::lock::{KernelLock, KernelLocks, KernelPaths, NixKernelLock, NixKernelLocks};
Expand All @@ -9,11 +8,7 @@ use pyo3::exceptions::{PyKeyError, PyValueError};
use pyo3::prelude::*;

use crate::PyKernelDependency;

/// Parse a git object id, mapping a parse failure to a Python `ValueError`.
fn parse_oid(s: &str) -> PyResult<Oid> {
Oid::from_str(s).map_err(|err| PyValueError::new_err(err.to_string()))
}
use crate::git::{PyOid, parse_oid};

/// A locked kernel revision.
#[pyclass(name = "KernelLock", frozen, eq, hash)]
Expand Down Expand Up @@ -48,8 +43,8 @@ impl PyKernelLock {
}

#[getter]
fn commit(&self) -> &str {
self.commit.as_str()
fn commit(&self) -> PyOid {
self.commit.clone().into()
}

/// Parse a `KernelLock` from a JSON string.
Expand Down Expand Up @@ -247,8 +242,8 @@ impl PyNixKernelLock {
}

#[getter]
fn commit(&self) -> &str {
self.commit.as_str()
fn commit(&self) -> PyOid {
self.commit.clone().into()
}

#[getter]
Expand Down
41 changes: 41 additions & 0 deletions kernels/rust/signing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use kernels_common::signing::receipt::KernelLocation;
use pyo3::prelude::*;

use crate::git::PyOid;

/// The location of a kernel that a verification applies to.
#[pyclass(name = "KernelLocation", frozen, eq, hash)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) struct PyKernelLocation {
inner: KernelLocation,
}

impl From<KernelLocation> for PyKernelLocation {
fn from(inner: KernelLocation) -> Self {
Self { inner }
}
}

#[pymethods]
impl PyKernelLocation {
/// The location of a kernel variant in a Hub repository.
#[staticmethod]
fn remote(repo_id: String, revision: PyOid, variant: String) -> Self {
KernelLocation::remote(repo_id, revision.into_inner(), variant).into()
}

fn __repr__(&self) -> String {
match &self.inner {
KernelLocation::RemoteKernel {
repo_id,
revision,
variant,
} => format!(
"KernelLocation.remote(repo_id={:?}, revision={:?}, variant={:?})",
repo_id,
revision.as_str(),
variant
),
}
}
}
Loading
Loading