Skip to content

Commit

Permalink
Merge pull request #5 from bnkc/strategies
Browse files Browse the repository at this point in the history
strategies
  • Loading branch information
bnkc authored Apr 8, 2024
2 parents 05c378a + 9ba6a65 commit 7a9a7b0
Show file tree
Hide file tree
Showing 12 changed files with 473 additions and 534 deletions.
287 changes: 105 additions & 182 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use serde::Serialize;
use crate::config::Config;
use crate::exit_codes::ExitCode;
use crate::output::Outcome;
use crate::runtime_assets::get_imports;
use crate::runtime_assets::get_packages;
use crate::runtime_assets::{get_dependencies, Dependency};
use crate::runtime_assets::{get_site_packages, Package, PackageState};
use crate::project_assets::get_imports;
use crate::project_assets::get_packages;
use crate::project_assets::{get_dependencies, Dependency};
use crate::project_assets::{get_site_packages, Package, PackageState};

#[derive(Serialize, Debug, PartialEq, Eq, Clone)]
pub struct AnalysisElement<'a> {
Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn scan(config: Config) -> Result<ExitCode> {
mod tests {

use super::*;
use crate::runtime_assets::{DependencyBuilder, PackageBuilder};
use crate::project_assets::{DependencyBuilder, PackageBuilder};

/// Helper function to create a Package instance.
fn create_package(id: &str, aliases: &[&str]) -> Package {
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;

use std::path::PathBuf;

use crate::runtime_assets::PackageState;
use crate::project_assets::PackageState;

#[derive(Parser)]
#[command(
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use crate::cli::{Env, OutputKind};
use crate::runtime_assets::PackageState;
use crate::project_assets::PackageState;

pub struct Config {
/// The path to the directory to search for Python files.
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod cli;
mod config;
mod exit_codes;
mod output;
mod runtime_assets;
mod project_assets;

use std::env;
use std::path::{Path, PathBuf};
Expand Down
9 changes: 4 additions & 5 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Reverse;
use std::io::Write;

use anyhow::Result;
Expand Down Expand Up @@ -25,7 +26,7 @@ struct Record<'r> {
}

impl<'a> Outcome<'a> {
pub fn print_report(&self, config: &Config, mut stdout: impl Write) -> Result<ExitCode> {
pub fn print_report(&mut self, config: &Config, mut stdout: impl Write) -> Result<ExitCode> {
match config.output {
OutputKind::Human => self.pretty_print(&mut stdout, config),
OutputKind::Json => self.json_print(&mut stdout),
Expand All @@ -39,7 +40,7 @@ impl<'a> Outcome<'a> {
Ok(ExitCode::Success)
}

fn pretty_print(&self, stdout: &mut impl Write, config: &Config) -> Result<ExitCode> {
fn pretty_print(&mut self, stdout: &mut impl Write, config: &Config) -> Result<ExitCode> {
if self.success {
writeln!(
stdout,
Expand All @@ -52,9 +53,7 @@ impl<'a> Outcome<'a> {

writeln!(stdout, "\n 📦 {:?} Packages", config.package_state)?;

// sort elements by size
let mut elements = self.elements.clone();
elements.sort_by_key(|el| el.package.size());
self.elements.sort_by_key(|e| Reverse(e.package.size()));

let records: Vec<Record> = self
.elements
Expand Down
File renamed without changes.
11 changes: 4 additions & 7 deletions src/runtime_assets/imports.rs → src/project_assets/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ fn build_walker(config: &Config) -> Result<WalkParallel> {
let builder = WalkBuilder::new(&config.base_directory)
.hidden(config.ignore_hidden)
.max_depth(config.max_depth)
// .filter_entry(|entry| entry.path().extension().map_or(false, |ext| ext == "py"))
.filter_entry(|entry| {
// Always traverse directories to look for nested Python files.
entry.file_type().map_or(false, |ft| ft.is_dir()) ||
// Only consider files that have a `.py` extension.
entry.path().extension().map_or(false, |ext| ext == "py")
entry.file_type().map_or(false, |ft| ft.is_dir())
|| entry.path().extension().map_or(false, |ext| ext == "py")
})
.build_parallel(); // Builds the walker with parallelism support
.build_parallel();

Ok(builder)
}
Expand Down Expand Up @@ -134,7 +131,7 @@ mod tests {
use tempfile::tempdir;

use crate::cli::{Env, OutputKind};
use crate::runtime_assets::PackageState;
use crate::project_assets::PackageState;

/// Helper function to create a Python file in the temporary directory.
fn create_file(dir: &tempfile::TempDir, filename: &str, content: &str) -> PathBuf {
Expand Down
9 changes: 9 additions & 0 deletions src/project_assets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod dependency;
mod import;
mod package;

#[allow(unused_imports)]
pub(crate) use dependency::{get_dependencies, Dependency, DependencyBuilder};
pub(crate) use import::get_imports;
#[allow(unused_imports)]
pub(crate) use package::{get_packages, get_site_packages, Package, PackageBuilder, PackageState};
Loading

0 comments on commit 7a9a7b0

Please sign in to comment.