Skip to content

Commit 21b9122

Browse files
committed
worker/jobs/index: Extract NormalizeIndex into dedicated module
1 parent 6caabd9 commit 21b9122

File tree

2 files changed

+105
-93
lines changed

2 files changed

+105
-93
lines changed

src/worker/jobs/index/mod.rs

Lines changed: 6 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ use crate::worker::Environment;
55
use anyhow::Context;
66
use chrono::Utc;
77
use crates_io_env_vars::var_parsed;
8-
use crates_io_index::{Crate, Repository};
8+
use crates_io_index::Repository;
99
use crates_io_worker::BackgroundJob;
1010
use diesel::prelude::*;
1111
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
1212
use sentry::Level;
1313
use std::fs::{self, File};
14-
use std::io::{BufRead, BufReader, ErrorKind, Write};
14+
use std::io::{ErrorKind, Write};
1515
use std::process::Command;
1616
use std::sync::Arc;
1717
use url::Url;
1818

19+
mod normalize;
20+
21+
pub use normalize::NormalizeIndex;
22+
1923
#[derive(Serialize, Deserialize)]
2024
pub struct SyncToGitIndex {
2125
krate: String,
@@ -221,94 +225,3 @@ impl BackgroundJob for SquashIndex {
221225
.await
222226
}
223227
}
224-
225-
#[derive(Serialize, Deserialize)]
226-
pub struct NormalizeIndex {
227-
dry_run: bool,
228-
}
229-
230-
impl NormalizeIndex {
231-
pub fn new(dry_run: bool) -> Self {
232-
Self { dry_run }
233-
}
234-
}
235-
236-
impl BackgroundJob for NormalizeIndex {
237-
const JOB_NAME: &'static str = "normalize_index";
238-
const QUEUE: &'static str = "repository";
239-
240-
type Context = Arc<Environment>;
241-
242-
async fn run(&self, env: Self::Context) -> anyhow::Result<()> {
243-
info!("Normalizing the index");
244-
245-
let dry_run = self.dry_run;
246-
spawn_blocking(move || {
247-
let repo = env.lock_index()?;
248-
249-
let files = repo.get_files_modified_since(None)?;
250-
let num_files = files.len();
251-
252-
for (i, file) in files.iter().enumerate() {
253-
if i % 50 == 0 {
254-
info!(num_files, i, ?file);
255-
}
256-
257-
let crate_name = file.file_name().unwrap().to_str().unwrap();
258-
let path = repo.index_file(crate_name);
259-
if !path.exists() {
260-
continue;
261-
}
262-
263-
let mut body: Vec<u8> = Vec::new();
264-
let file = fs::File::open(&path)?;
265-
let reader = BufReader::new(file);
266-
let mut versions = Vec::new();
267-
for line in reader.lines() {
268-
let line = line?;
269-
if line.is_empty() {
270-
continue;
271-
}
272-
273-
let mut krate: Crate = serde_json::from_str(&line)?;
274-
for dep in &mut krate.deps {
275-
// Remove deps with empty features
276-
dep.features.retain(|d| !d.is_empty());
277-
// Set null DependencyKind to Normal
278-
dep.kind =
279-
Some(dep.kind.unwrap_or(crates_io_index::DependencyKind::Normal));
280-
}
281-
krate.deps.sort();
282-
versions.push(krate);
283-
}
284-
for version in versions {
285-
serde_json::to_writer(&mut body, &version).unwrap();
286-
body.push(b'\n');
287-
}
288-
fs::write(path, body)?;
289-
}
290-
291-
info!("Committing normalization");
292-
let msg = "Normalize index format\n\n\
293-
More information can be found at https://github.com/rust-lang/crates.io/pull/5066";
294-
repo.run_command(Command::new("git").args(["commit", "-am", msg]))?;
295-
296-
let branch = match dry_run {
297-
false => "master",
298-
true => "normalization-dry-run",
299-
};
300-
301-
info!(?branch, "Pushing to upstream repository");
302-
repo.run_command(Command::new("git").args([
303-
"push",
304-
"origin",
305-
&format!("HEAD:{branch}"),
306-
]))?;
307-
308-
info!("Index normalization completed");
309-
310-
Ok(())
311-
})
312-
.await
313-
}
314-
}

src/worker/jobs/index/normalize.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use crate::tasks::spawn_blocking;
2+
use crate::worker::Environment;
3+
use crates_io_index::Crate;
4+
use crates_io_worker::BackgroundJob;
5+
use std::fs;
6+
use std::io::{BufRead, BufReader};
7+
use std::process::Command;
8+
use std::sync::Arc;
9+
10+
#[derive(Serialize, Deserialize)]
11+
pub struct NormalizeIndex {
12+
dry_run: bool,
13+
}
14+
15+
impl NormalizeIndex {
16+
pub fn new(dry_run: bool) -> Self {
17+
Self { dry_run }
18+
}
19+
}
20+
21+
impl BackgroundJob for NormalizeIndex {
22+
const JOB_NAME: &'static str = "normalize_index";
23+
const QUEUE: &'static str = "repository";
24+
25+
type Context = Arc<Environment>;
26+
27+
async fn run(&self, env: Self::Context) -> anyhow::Result<()> {
28+
info!("Normalizing the index");
29+
30+
let dry_run = self.dry_run;
31+
spawn_blocking(move || {
32+
let repo = env.lock_index()?;
33+
34+
let files = repo.get_files_modified_since(None)?;
35+
let num_files = files.len();
36+
37+
for (i, file) in files.iter().enumerate() {
38+
if i % 50 == 0 {
39+
info!(num_files, i, ?file);
40+
}
41+
42+
let crate_name = file.file_name().unwrap().to_str().unwrap();
43+
let path = repo.index_file(crate_name);
44+
if !path.exists() {
45+
continue;
46+
}
47+
48+
let mut body: Vec<u8> = Vec::new();
49+
let file = fs::File::open(&path)?;
50+
let reader = BufReader::new(file);
51+
let mut versions = Vec::new();
52+
for line in reader.lines() {
53+
let line = line?;
54+
if line.is_empty() {
55+
continue;
56+
}
57+
58+
let mut krate: Crate = serde_json::from_str(&line)?;
59+
for dep in &mut krate.deps {
60+
// Remove deps with empty features
61+
dep.features.retain(|d| !d.is_empty());
62+
// Set null DependencyKind to Normal
63+
dep.kind =
64+
Some(dep.kind.unwrap_or(crates_io_index::DependencyKind::Normal));
65+
}
66+
krate.deps.sort();
67+
versions.push(krate);
68+
}
69+
for version in versions {
70+
serde_json::to_writer(&mut body, &version).unwrap();
71+
body.push(b'\n');
72+
}
73+
fs::write(path, body)?;
74+
}
75+
76+
info!("Committing normalization");
77+
let msg = "Normalize index format\n\n\
78+
More information can be found at https://github.com/rust-lang/crates.io/pull/5066";
79+
repo.run_command(Command::new("git").args(["commit", "-am", msg]))?;
80+
81+
let branch = match dry_run {
82+
false => "master",
83+
true => "normalization-dry-run",
84+
};
85+
86+
info!(?branch, "Pushing to upstream repository");
87+
repo.run_command(Command::new("git").args([
88+
"push",
89+
"origin",
90+
&format!("HEAD:{branch}"),
91+
]))?;
92+
93+
info!("Index normalization completed");
94+
95+
Ok(())
96+
})
97+
.await
98+
}
99+
}

0 commit comments

Comments
 (0)