Skip to content

Commit

Permalink
Merge pull request #204 from jelmer/svp-client
Browse files Browse the repository at this point in the history
Add svp client crate
  • Loading branch information
jelmer authored Feb 22, 2024
2 parents f47d4ea + 0e1a4f2 commit 5aaf4eb
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions crates/svp-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "svp-client"
version = "0.1.0"
authors = ["Jelmer Vernooij <[email protected]>"]
license = "Apache-2.0"
description = "Client for the silver-platter protocol"

[lib]

[dependencies]
log = "0.4.20"
serde = { workspace = true, features = ["derive"] }
serde_json = "1.0.113"
40 changes: 40 additions & 0 deletions crates/svp-client/src/debian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::Success;
use std::collections::HashMap;

#[derive(Debug, serde::Serialize)]
pub struct ChangelogBehaviour {
update: bool,
explanation: String,
}

#[derive(Debug, serde::Serialize)]
pub struct Context {
changelog: Option<ChangelogBehaviour>,
}

pub fn report_success(
versions: HashMap<String, String>,
value: Option<i32>,
context: Option<serde_json::Value>,
changelog: Option<(bool, String)>,
) {
if std::env::var("SVP_API").ok().as_deref() == Some("1") {
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

serde_json::to_writer(
f,
&Success {
versions,
value,
context,
debian: Some(Context {
changelog: changelog.map(|cl| ChangelogBehaviour {
update: cl.0,
explanation: cl.1,
}),
}),
},
)
.unwrap();
}
}
122 changes: 122 additions & 0 deletions crates/svp-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//! # svp-client
//!
//! `svp-client` is a library to interact with the [SVP
//! protocol](https://github.com/jelmer/silver-platter/blob/master/codemod-protocol.md), as supported by
//! the `svp` command-line tool.
use std::collections::HashMap;

pub mod debian;

#[derive(Debug, serde::Serialize)]
struct Failure {
/// The result code, e.g. "header-missing".
result_code: String,

/// The versions of the packages involved.
versions: HashMap<String, String>,

/// A human-readable description of the failure.
description: String,

/// Whether the failure is transient.
transient: Option<bool>,
}

#[derive(Debug, serde::Serialize)]
struct Success {
versions: HashMap<String, String>,
value: Option<i32>,
context: Option<serde_json::Value>,
debian: Option<debian::Context>,
}

/// Report a success to the SVP server.
///
/// # Arguments
/// * `versions` - A map of package names to versions.
pub fn report_success(
versions: HashMap<String, String>,
value: Option<i32>,
context: Option<serde_json::Value>,
) {
if std::env::var("SVP_API").ok().as_deref() == Some("1") {
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

serde_json::to_writer(
f,
&Success {
versions,
value,
context,
debian: None,
},
)
.unwrap();
}
}

pub fn report_nothing_to_do(versions: HashMap<String, String>, description: Option<&str>) -> ! {
let description = description.unwrap_or("Nothing to do");
if std::env::var("SVP_API").ok().as_deref() == Some("1") {
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

serde_json::to_writer(
f,
&Failure {
result_code: "nothing-to-do".to_string(),
versions,
description: description.to_string(),
transient: None,
},
)
.unwrap();
}
log::error!("{}", description);
std::process::exit(0);
}

pub fn report_fatal(
versions: HashMap<String, String>,
code: &str,
description: &str,
hint: Option<&str>,
transient: Option<bool>,
) -> ! {
if std::env::var("SVP_API").ok().as_deref() == Some("1") {
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

serde_json::to_writer(
f,
&Failure {
result_code: code.to_string(),
versions,
description: description.to_string(),
transient,
},
)
.unwrap();
}
log::error!("{}", description);
if let Some(hint) = hint {
log::info!("{}", hint);
}
std::process::exit(1);
}

pub fn load_resume() -> Option<serde_json::Value> {
if std::env::var("SVP_API").ok().as_deref() == Some("1") {
if let Ok(resume_path) = std::env::var("SVP_RESUME") {
let f = std::fs::File::open(resume_path).unwrap();
let resume: serde_json::Value = serde_json::from_reader(f).unwrap();
Some(resume)
} else {
None
}
} else {
None
}
}

pub fn enabled() -> bool {
std::env::var("SVP_API").ok().as_deref() == Some("1")
}
17 changes: 17 additions & 0 deletions silver_platter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

__all__ = [
'create_temp_sprout',
'open_branch',
'open_branch_containing',
'full_branch_url',
'Branch',
'BranchUnavailable',
'BranchTemporarilyUnavailable',
'BranchRateLimited',
'BranchMissing',
'BranchUnsupported',
]


from typing import Optional

from breezy.branch import Branch

from . import _svp_rs

Branch = _svp_rs.Branch
create_temp_sprout = _svp_rs.create_temp_sprout


Expand Down

0 comments on commit 5aaf4eb

Please sign in to comment.