forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#10466 - samueltardieu:popular-crates, r=llogiq
Add the `popular-crates` binary This program downloads crates info from <https://crates.io/> and builds a TOML file that can be fed to `lintcheck`. I have been asked, on various pull requests, what the result of `lintcheck` was. However, the default configuration file for lintcheck is limited. This `popular-crates` program allows building a recent list of the recently most downloaded crates from <https://crates.io> and feed it to `lintcheck`. Using it, it was easy to test two new lints against the 500 recently most downloaded crates to ensure that there was no regression. changelog: none
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![deny(clippy::pedantic)] | ||
|
||
use clap::Parser; | ||
use crates_io_api::{CratesQueryBuilder, Sort, SyncClient}; | ||
use indicatif::ProgressBar; | ||
use std::collections::HashSet; | ||
use std::fs::File; | ||
use std::io::{BufWriter, Write}; | ||
use std::path::PathBuf; | ||
use std::time::Duration; | ||
|
||
#[derive(Parser)] | ||
struct Opts { | ||
/// Output TOML file name | ||
output: PathBuf, | ||
/// Number of crate names to download | ||
#[clap(short, long, default_value_t = 100)] | ||
number: usize, | ||
/// Do not output progress | ||
#[clap(short, long)] | ||
quiet: bool, | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let opts = Opts::parse(); | ||
let mut output = BufWriter::new(File::create(opts.output)?); | ||
output.write_all(b"[crates]\n")?; | ||
let client = SyncClient::new( | ||
"clippy/lintcheck (github.com/rust-lang/rust-clippy/)", | ||
Duration::from_secs(1), | ||
)?; | ||
let mut seen_crates = HashSet::new(); | ||
let pb = if opts.quiet { | ||
None | ||
} else { | ||
Some(ProgressBar::new(opts.number as u64)) | ||
}; | ||
let mut query = CratesQueryBuilder::new() | ||
.sort(Sort::RecentDownloads) | ||
.page_size(100) | ||
.build(); | ||
while seen_crates.len() < opts.number { | ||
let retrieved = client.crates(query.clone())?.crates; | ||
if retrieved.is_empty() { | ||
eprintln!("No more than {} crates available from API", seen_crates.len()); | ||
break; | ||
} | ||
for c in retrieved { | ||
if seen_crates.insert(c.name.clone()) { | ||
output.write_all( | ||
format!( | ||
"{} = {{ name = '{}', versions = ['{}'] }}\n", | ||
c.name, c.name, c.max_version | ||
) | ||
.as_bytes(), | ||
)?; | ||
if let Some(pb) = &pb { | ||
pb.inc(1); | ||
} | ||
} | ||
} | ||
query.set_page(query.page() + 1); | ||
} | ||
Ok(()) | ||
} |