check upstream cache

This commit is contained in:
cy 2025-04-01 01:47:01 -04:00
parent 05bfb4c491
commit 073755deeb
Signed by: cy
SSH key fingerprint: SHA256:o/geVWV4om1QhUSkKvDQeW/eAihwnjyXkqMwrVdbuts
5 changed files with 1595 additions and 30 deletions

View file

@ -1,12 +1,10 @@
#![feature(string_from_utf8_lossy_owned)]
use std::{env, path::Path};
use std::process::Command;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use serde_json;
use log::debug;
use tokio;
const UPSTREAM_CACHES: &'static [&'static str] = &[
"https://cache.nixos.org",
@ -28,6 +26,21 @@ struct PathInfo {
}
impl PathInfo {
// find derivations related to package
fn from_package(package: &str) -> Vec<Self> {
let path_infos = Command::new("nix")
.arg("path-info")
.arg("--derivation")
.arg("--json")
.arg(package)
.output()
.expect("path-info failed");
let path_infos: Vec<PathInfo> = serde_json::from_slice(&path_infos.stdout).unwrap();
debug!("PathInfo's from nix path-info: {:#?}", path_infos);
path_infos
}
// find store paths related to derivation
fn get_store_paths(&self) -> Vec<String> {
let mut store_paths: Vec<String> = Vec::new();
@ -47,30 +60,24 @@ impl PathInfo {
}
}
fn main() {
#[tokio::main]
async fn main() {
env_logger::init();
let args: Vec<String> = env::args().collect();
let package = &args[1];
println!("package: {}", package);
// find derivations related to package
let path_infos = Command::new("nix")
.arg("path-info")
.arg("--derivation")
.arg("--json")
.arg(package)
.output()
.expect("path-info failed");
let path_infos: Vec<PathInfo> = serde_json::from_slice(&path_infos.stdout).unwrap();
debug!("PathInfo's from nix path-info: {:#?}", path_infos);
debug!("package: {}", package);
let path_infos = PathInfo::from_package(package);
// filter out store paths that exist in upstream caches
let store_paths = path_infos[0].get_store_paths();
for store_path in store_paths {
let basename = Path::new(&store_path).file_name().unwrap().to_str().unwrap().to_string();
let hash = basename.split("-").nth(0).unwrap();
println!("hash: {}", hash);
for upstream in UPSTREAM_CACHES {
let mut uri = String::from(*upstream);
uri.push_str(format!("/{}.narinfo", hash).as_str());
let res_status = reqwest::Client::new().head(uri).send().await.unwrap().status();
println!("{} responded with {}", *upstream, res_status);
}
}
}