Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate hashes on releases #33

Merged
merged 7 commits into from
Jan 25, 2025
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
536 changes: 528 additions & 8 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ anyhow = "1.0.95"
bytes = "1.9.0"
clap = { version = "4.5.27", features = ["derive"] }
flate2 = "1.0.35"
futures-util = "0.3.31"
heck = "0.5.0"
hex = "0.4.3"
itertools = "0.14.0"
octocrab = { version = "0.43.0", features = ["stream"] }
reqwest = { version = "0.12.12", features = [
"rustls-tls",
"json",
Expand All @@ -23,6 +25,7 @@ reqwest = { version = "0.12.12", features = [
"deflate",
"hickory-dns",
] }
rinja = "0.3.5"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.137"
sha2 = "0.10.8"
Expand All @@ -41,6 +44,7 @@ rust_2018_idioms = { level = "warn", priority = -1 }
rust_2021_compatibility = { level = "warn", priority = -1 }
rust_2024_compatibility = { level = "warn", priority = -1 }
unused = { level = "warn", priority = -1 }
tail-expr-drop-order = "allow"

[lints.clippy]
pedantic = "warn"
Expand Down
72 changes: 63 additions & 9 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{env, ffi::OsStr, path::PathBuf, sync::LazyLock};
use std::{env, ffi::OsStr, iter::once, path::PathBuf, sync::LazyLock};

use build_tools::download_wasi_sdk;
use clap::ValueEnum;
use sha2::{Digest, Sha256};
use strum::IntoEnumIterator;
use tokio::process::Command;
use tokio::{fs, process::Command};

use crate::run;

Expand Down Expand Up @@ -49,13 +50,28 @@ pub enum SupportedProjects {
///
/// # Errors
/// If the build fails.
pub async fn build(
pub async fn build_and_publish(
project: SupportedProjects,
release_version: &str,
output_dir: Option<PathBuf>,
python_versions: &[PythonVersion],
publish: bool,
) -> anyhow::Result<()> {
let wheel_paths = build(project, release_version, output_dir, python_versions).await?;

if publish {
publish_release(project, release_version, &wheel_paths).await?;
}

Ok(())
}

pub async fn build(
project: SupportedProjects,
release_version: &str,
output_dir: Option<PathBuf>,
python_versions: &[PythonVersion],
) -> anyhow::Result<Vec<PathBuf>> {
let mut wheel_paths = vec![];

for python_version in python_versions {
Expand All @@ -67,11 +83,7 @@ pub async fn build(
wheel_paths.push(wheel_path);
}

if publish {
publish_release(project, release_version, &wheel_paths).await?;
}

Ok(())
Ok(wheel_paths)
}

async fn publish_release(
Expand All @@ -82,13 +94,55 @@ async fn publish_release(
let tag = format!("{project}/v{release_version}");
let notes = format!("Generated using `wasi-wheels build {project} {release_version}`");

let hashes = generate_hashes(wheel_paths).await?;
let temp_dir = tempfile::tempdir()?;
let hashes_path = temp_dir.path().join("hashes.txt");
fs::write(&hashes_path, hashes).await?;

run(Command::new("gh").args(
[
"release", "create", &tag, "--title", &tag, "--notes", &notes,
]
.into_iter()
.map(OsStr::new)
.chain(wheel_paths.iter().map(|p| p.as_os_str())),
.chain(wheel_paths.iter().map(|p| p.as_os_str()))
.chain(once(hashes_path.as_os_str())),
))
.await
}

async fn generate_hashes(wheel_paths: &[PathBuf]) -> anyhow::Result<String> {
let mut hashes = String::new();
for wheel_path in wheel_paths {
let content = fs::read(wheel_path).await?;
let hash = format!("{:x}", Sha256::digest(&content));
let filename = wheel_path.file_name().unwrap().to_string_lossy();
hashes.push_str(&format!("{filename}\t{hash}\n"));
}

Ok(hashes)
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_project_display() -> anyhow::Result<()> {
let wheel_paths = build(
SupportedProjects::PydanticCore,
"2.27.2",
None,
&[PythonVersion::Py3_12],
)
.await?;

let hashes = generate_hashes(&wheel_paths).await?;

for path in wheel_paths {
assert!(hashes.contains(path.file_name().unwrap().to_str().unwrap()));
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/python_registry.rs → src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use serde_json::Value;
use sha2::{Digest, Sha256};
use tar::Archive;

mod wasi;

use crate::build::PACKAGES_DIR;

/// Download the sdist package for the specified project and version
Expand Down
80 changes: 80 additions & 0 deletions src/index/wasi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! Generate a custom index for WASI wheels.

#[cfg(test)]
mod tests {
use std::{collections::HashSet, sync::Arc};

use futures_util::TryStreamExt;
use octocrab::Octocrab;
use rinja::Template;
use tokio::pin;

#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
packages: HashSet<String>,
}

/// GitHub client for loading release information from a repository.
struct GitHubReleaseClient {
client: Arc<Octocrab>,
}

impl GitHubReleaseClient {
/// Creates a new instance of a GitHub Client initialized with the default Octocrab instance.
fn new() -> Self {
Self {
client: octocrab::instance(),
}
}

/// Retrieves a set of package names from a GitHub repository's releases.
/// Assumes the release tags follow the structure <package-name>/v<package-version>.
///
/// # Arguments
/// * `owner` - The owner of the GitHub repository
/// * `repo` - The name of the GitHub repository
///
/// # Returns
/// A `Result` containing a `HashSet` of package names found in release tags.
async fn packages(&self, owner: &str, repo: &str) -> anyhow::Result<HashSet<String>> {
let releases = self
.client
.repos(owner, repo)
.releases()
.list()
.send()
.await?
.into_stream(&self.client);
pin!(releases);

let mut packages = HashSet::new();

while let Some(release) = releases.try_next().await? {
let Some((package, _)) = release.tag_name.split_once("/v") else {
continue;
};
packages.insert(package.to_owned());
}

Ok(packages)
}
}

#[tokio::test]
async fn get_releases() -> anyhow::Result<()> {
let releases = GitHubReleaseClient::new();
let packages = releases.packages("benbrandt", "wasi-wheels").await?;

let index = IndexTemplate {
packages: packages.clone(),
}
.render()?;

assert!(packages
.iter()
.all(|package| index.contains(&format!("<a href=\"/{package}/\">{package}</a>"))));

Ok(())
}
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use anyhow::{bail, Context};
use tokio::process::Command;

mod build;
mod python_registry;
mod index;

pub use build::{build, install_build_tools, PythonVersion, SupportedProjects};
pub use python_registry::download_package;
pub use build::{build_and_publish, install_build_tools, PythonVersion, SupportedProjects};
pub use index::download_package;

/// Run a given command with common error handling behavior
///
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use wasi_wheels::{build, download_package, install_build_tools, PythonVersion, SupportedProjects};
use wasi_wheels::{
build_and_publish, download_package, install_build_tools, PythonVersion, SupportedProjects,
};

#[derive(Debug, Parser)]
#[command(version, about, long_about = None, propagate_version = true)]
Expand Down Expand Up @@ -62,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
publish,
python_versions,
} => {
build(
build_and_publish(
project,
&release_version,
output_dir,
Expand Down
8 changes: 8 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<body>
{% for package in packages %}
<a href="/{{ package }}/">{{ package }}</a>
{% endfor %}
</body>
</html>
Loading