Skip to content

Commit

Permalink
Write imports to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Nov 26, 2023
1 parent f3ef861 commit 8fcd683
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/import_graph/graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use pathfinding::prelude::{bfs, bfs_reach};
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;

Expand Down Expand Up @@ -32,6 +33,33 @@ impl ImportGraph {
.collect()
}

pub fn imports(&self) -> HashMap<String, HashSet<String>> {
self.imports
.iter()
.map(|(module, imported_modules)| {
(
module.pypath.clone(),
imported_modules
.iter()
.map(|imported_module| imported_module.pypath.clone())
.collect(),
)
})
.collect()
}

pub fn imports_flat(&self) -> Vec<(String, String)> {
self.imports()
.into_iter()
.flat_map(|(module, imported_modules)| {
imported_modules
.into_iter()
.map(|imported_module| (module.clone(), imported_module))
.collect::<Vec<_>>()
})
.collect()
}

pub fn package_from_module(&self, module: &str) -> Result<String> {
let module = match self.modules_by_pypath.get(module) {
Some(module) => module,
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
let root_package_path = Path::new(&args[1]);
let import_graph = ImportGraphBuilder::new(root_package_path).build()?;
dbg!(import_graph.modules().len());

if args.len() == 2 {
let imports = import_graph.imports_flat();
println!("source, target");
for (from_module, to_module) in imports {
println!("{}, {}", from_module, to_module);
}
} else {
let imports = import_graph.modules_directly_imported_by(&args[2]);
dbg!(&imports);
}

Ok(())
}

0 comments on commit 8fcd683

Please sign in to comment.