Skip to content

Commit f1476fa

Browse files
committed
Generate FST using all available ecosystems
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 225d3ff commit f1476fa

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

fst_builder/main.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,11 @@ See https://aboutcode.org for more information about nexB OSS projects.
1010
*/
1111

1212
use fst::SetBuilder;
13-
use std::fs::File;
13+
use std::fs::{File, read_dir};
1414
use std::io::{BufRead, BufReader};
15-
use std::path::Path;
16-
17-
use std::io::Write;
18-
19-
fn main() {
20-
let input_file = Path::new("fst_builder/data/purls.txt");
21-
let output_file = Path::new("purls.fst");
22-
23-
let file = File::create(output_file).expect("Cannot create FST file");
24-
let mut builder = SetBuilder::new(file).expect("Failed to create FST builder");
15+
use std::path::{Path, PathBuf};
2516

17+
fn insert_purls(input_file: &Path, builder: &mut SetBuilder<File>) -> usize {
2618
let f = File::open(input_file).expect("Cannot open input file");
2719
let reader = BufReader::new(f);
2820

@@ -33,17 +25,43 @@ fn main() {
3325
.collect();
3426

3527
lines.sort();
28+
let count = lines.len();
3629

37-
let sorted_file = Path::new("fst_builder/data/purls.txt");
38-
let mut sorted_f = File::create(sorted_file).expect("Cannot create sorted file");
39-
for line in &lines {
40-
writeln!(sorted_f, "{}", line).expect("Failed to write line");
41-
}
42-
30+
println!("Insert PURLs from {:?} in FST", input_file);
4331
for line in lines {
4432
builder.insert(&line).unwrap();
4533
}
4634

35+
count
36+
}
37+
38+
fn get_txt_files(dir: &Path) -> Vec<PathBuf> {
39+
let mut files: Vec<PathBuf> = read_dir(dir)
40+
.expect("Cannot read directory")
41+
.map(|entry| entry.expect("Invalid directory entry").path())
42+
.filter(|path| path.is_file() && path.extension().and_then(|e| e.to_str()) == Some("txt"))
43+
.collect();
44+
45+
files.sort();
46+
47+
files
48+
}
49+
50+
fn main() {
51+
let input_dir = Path::new("fst_builder/data");
52+
let output_file = Path::new("purls.fst");
53+
let mut total_purls: usize = 0;
54+
55+
let file = File::create(output_file).expect("Cannot create FST file");
56+
let mut builder = SetBuilder::new(file).expect("Failed to create FST builder");
57+
58+
let input_files = get_txt_files(input_dir);
59+
60+
for input_file in input_files {
61+
total_purls += insert_purls(&input_file, &mut builder);
62+
}
63+
4764
builder.finish().expect("Failed to finish FST");
65+
println!("FST generated with {} base PackageURLs", total_purls);
4866
println!("FST generated at {:?}", output_file);
4967
}

0 commit comments

Comments
 (0)