Skip to content

Commit

Permalink
Merge pull request #5 from os-checker/feat/crates.io
Browse files Browse the repository at this point in the history
feat: add release_count
  • Loading branch information
zjp-CN authored Nov 19, 2024
2 parents 1fb776b + 5a39ad4 commit 8d43845
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
chmod +x ~/.cargo/bin/os-checker
os-checker layout --help
# - name: Test get_release_count
# run: cargo test -- test_get_release_count --nocapture
#
# - name: Test os-checker layout --list-targets
# run: cargo test -- test_sel4 --nocapture
#
Expand Down
55 changes: 55 additions & 0 deletions src/crates_io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
fn url(pkg: &str) -> String {
const PREFIX: &str =
"https://raw.githubusercontent.com/rust-lang/crates.io-index/refs/heads/master";

// ref: https://doc.rust-lang.org/cargo/reference/registry-index.html#index-files
let components = match pkg.len() {
1 => &["1", pkg][..],
2 => &["2", pkg],
3 => {
let (a, b) = pkg.split_at(1);
&["3", a, b, pkg]
}
_ => {
let (a, b) = pkg.split_at(2);
let (b, _) = b.split_at(2);
&[a, b, pkg]
}
};

// e.g. https://raw.githubusercontent.com/rust-lang/crates.io-index/refs/heads/master/os/-c/os-checker
let mut buf = String::with_capacity(128);
buf.push_str(PREFIX);

for c in components {
buf.push('/');
buf.push_str(c);
}

buf
}

/// NOTE: the result might be spurious due to network failure or invalid text;
///
/// None means no release found; 0 is an invalid value because there at least one
/// release if found.
pub fn get_release_count(pkg: &str) -> Option<usize> {
let url = url(pkg);
let output = duct::cmd!("wget", &url, "-O", "-")
.stdout_capture()
.stderr_null()
.run()
.ok()?;

let text = std::str::from_utf8(&output.stdout).ok()?.trim();
let count = text.lines().count();
if count == 0 {
error!(pkg, url, text, "count is an invalid value 0")
}
Some(count)
}

#[test]
fn test_get_release_count() {
dbg!(get_release_count("os-checker"));
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate eyre;
#[macro_use]
extern crate tracing;

pub mod crates_io;
pub mod database;
pub mod logger;
pub mod nextest;
Expand Down
12 changes: 9 additions & 3 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{database::diag_total_count, prelude::*};
use crate::{crates_io::get_release_count, database::diag_total_count, prelude::*};
use cargo_metadata::Package;
use eyre::ContextCompat;
use output::Output;
Expand Down Expand Up @@ -83,6 +83,7 @@ impl Repo {
let pkg_name = pkg.name.as_str();
let mut output = Output::new(pkg, test_cases.swap_remove(pkg_name));
output.diag_total_count = diag_total_count([&self.user, &self.repo, pkg_name]);
output.release_count = get_release_count(pkg_name);
assert!(
outputs.insert(pkg_name, output).is_none(),
"os-checker can't handle duplicated package names in a repo"
Expand Down Expand Up @@ -114,8 +115,13 @@ impl Repo {
}

pub fn local_base_dir() -> &'static Utf8Path {
static GIT_CLONE_DIR: LazyLock<Utf8PathBuf> =
LazyLock::new(|| Utf8PathBuf::from_iter(["/tmp", "os-checker-plugin-cargo"]));
static GIT_CLONE_DIR: LazyLock<Utf8PathBuf> = LazyLock::new(|| {
let path = Utf8PathBuf::from_iter(["/tmp", "os-checker-plugin-cargo"]);
if let Err(err) = std::fs::create_dir_all(&path) {
error!(?err, ?path, "directory is not created");
};
path
});

&GIT_CLONE_DIR
}
Expand Down
3 changes: 3 additions & 0 deletions src/repo/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Output {
pub categories: Vec<String>,
pub os_categories: Vec<String>,
pub diag_total_count: Option<usize>,
/// crates.io 发版次数
pub release_count: Option<usize>,
}

impl Output {
Expand Down Expand Up @@ -55,6 +57,7 @@ impl Output {
})
.unwrap_or_default(),
diag_total_count: None,
release_count: None,
}
}
}

0 comments on commit 8d43845

Please sign in to comment.