pydep-mapper 0.1.5

Fast Rust CLI for analyzing Python dependencies with external package declarations support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::graph::{DependencyGraph, DependencyType, utils::add_containment_relationships};
use crate::imports::{ModuleIdentifier, ModuleOrigin, extract_module_deps};
use anyhow::Result;
use indicatif::{ProgressBar, ProgressStyle};
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// Builds a dependency graph from all Python files in a directory (recursive).
pub fn build_directory_dependency_graph(dir_path: &Path) -> Result<DependencyGraph> {
    let python_files = analyze_python_directory_recursive(dir_path)?;
    let mut graph = DependencyGraph::new();

    if python_files.is_empty() {
        return Ok(graph);
    }

    let pb = ProgressBar::new(python_files.len() as u64);
    pb.set_style(
        ProgressStyle::default_bar()
            .template(
                "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos:>7}/{len:7} {msg}",
            )
            .map_err(|e| anyhow::anyhow!("Failed to set progress bar style: {}", e))?
            .progress_chars("##-"),
    );
    pb.set_message("Analyzing Python files");

    for file_path in &python_files {
        pb.set_message(format!(
            "Processing {}",
            file_path.file_name().unwrap_or_default().to_string_lossy()
        ));

        match analyze_python_file_with_package(file_path, dir_path) {
            Ok((module_id, dependencies)) => {
                graph.add_module(module_id.clone()); // Ignore duplicates - module might be added as dependency first
                for dep in &dependencies {
                    graph.add_module(dep.clone()); // Ignore duplicates
                    graph.add_dependency(&module_id, dep, DependencyType::Imports)?;
                }
            }
            Err(e) => {
                eprintln!(
                    "Warning: Failed to analyze '{}': {}",
                    file_path.display(),
                    e
                );
                continue;
            }
        }
        pb.inc(1);
    }

    pb.finish_with_message("Analysis complete");

    // Add containment relationships based on module hierarchy
    add_containment_relationships(&mut graph)?;

    Ok(graph)
}

/// Discovers all Python files in a directory (non-recursive).
pub fn analyze_python_directory(dir_path: &Path) -> Result<Vec<std::path::PathBuf>> {
    if !dir_path.is_dir() {
        return Err(anyhow::anyhow!(
            "Path '{}' is not a directory",
            dir_path.display()
        ));
    }

    let mut python_files = Vec::new();
    for entry in fs::read_dir(dir_path)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() {
            if let Some(extension) = path.extension() {
                if extension == "py" {
                    python_files.push(path);
                }
            }
        }
    }

    // Sort files for consistent output
    python_files.sort();

    Ok(python_files)
}

/// Discovers all Python files in a directory and its subdirectories (recursive).
pub fn analyze_python_directory_recursive(dir_path: &Path) -> Result<Vec<PathBuf>> {
    if !dir_path.is_dir() {
        return Err(anyhow::anyhow!(
            "Path '{}' is not a directory",
            dir_path.display()
        ));
    }

    let mut python_files = Vec::new();

    let walker = WalkDir::new(dir_path)
        .follow_links(false)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| {
            // Skip directories starting with dot or named 'tests'
            if e.file_type().is_dir() {
                if let Some(name) = e.file_name().to_str() {
                    if name.starts_with('.') || name == "tests" {
                        return false;
                    }
                }
            }
            e.file_type().is_file()
        });

    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg}")
            .map_err(|e| anyhow::anyhow!("Failed to set progress bar style: {}", e))?,
    );
    pb.set_message("Discovering Python files...");

    for entry in walker {
        let path = entry.path();
        if let Some(extension) = path.extension() {
            if extension == "py" {
                python_files.push(path.to_path_buf());
                pb.set_message(format!("Found {} Python files", python_files.len()));
            }
        }
        pb.tick();
    }

    pb.finish_and_clear();

    // Sort files for consistent output
    python_files.sort();

    Ok(python_files)
}

/// Analyzes a single Python file and returns the module identifier and its dependencies.
pub fn analyze_python_file(file_path: &Path) -> Result<(ModuleIdentifier, Vec<ModuleIdentifier>)> {
    let python_code = fs::read_to_string(file_path)?;
    let dependencies = extract_module_deps(&python_code, None)?;

    // Create module identifier for this file
    let module_name = file_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string();
    let module_id = ModuleIdentifier {
        origin: ModuleOrigin::Internal,
        canonical_path: module_name,
    };

    Ok((module_id, dependencies))
}

/// Analyzes a single Python file with package context and returns module info and dependencies.
pub fn analyze_python_file_with_package(
    file_path: &Path,
    project_root: &Path,
) -> Result<(ModuleIdentifier, Vec<ModuleIdentifier>)> {
    let python_code = fs::read_to_string(file_path)?;

    // Create module identifier with proper package path
    let module_name = crate::pyproject::compute_module_name(file_path, project_root)?;

    // Extract dependencies with current module context for relative import resolution
    let dependencies = extract_module_deps(&python_code, Some(&module_name))?;

    let module_id = ModuleIdentifier {
        origin: ModuleOrigin::Internal,
        canonical_path: module_name,
    };
    Ok((module_id, dependencies))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn create_temp_python_file(dir: &Path, filename: &str, content: &str) -> PathBuf {
        let file_path = dir.join(filename);
        fs::write(&file_path, content).expect("Failed to write test file");
        file_path
    }

    #[test]
    fn test_analyze_python_directory_empty() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let dir_path = temp_dir.path();

        let result = analyze_python_directory(dir_path);
        assert!(result.is_ok());
        let files = result.unwrap();
        assert_eq!(files.len(), 0);
    }

    #[test]
    fn test_analyze_python_directory_with_python_files() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let dir_path = temp_dir.path();

        create_temp_python_file(dir_path, "module1.py", "import os");
        create_temp_python_file(dir_path, "module2.py", "import sys");
        create_temp_python_file(dir_path, "not_python.txt", "not python");

        let result = analyze_python_directory(dir_path);
        assert!(result.is_ok());
        let files = result.unwrap();

        assert_eq!(files.len(), 2);
        assert!(files.iter().any(|f| f.file_name().unwrap() == "module1.py"));
        assert!(files.iter().any(|f| f.file_name().unwrap() == "module2.py"));

        // Files should be sorted
        assert!(files[0].file_name().unwrap() <= files[1].file_name().unwrap());
    }

    #[test]
    fn test_analyze_python_directory_nonexistent() {
        let nonexistent_path = Path::new("/nonexistent/directory");
        let result = analyze_python_directory(nonexistent_path);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not a directory"));
    }

    #[test]
    fn test_analyze_python_directory_file_not_directory() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let file_path = create_temp_python_file(temp_dir.path(), "test.py", "import os");

        let result = analyze_python_directory(&file_path);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not a directory"));
    }

    #[test]
    fn test_analyze_python_file_simple() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let file_path =
            create_temp_python_file(temp_dir.path(), "test_module.py", "import os\nimport sys");

        let result = analyze_python_file(&file_path);
        assert!(result.is_ok());

        let (module_id, dependencies) = result.unwrap();
        assert_eq!(module_id.canonical_path, "test_module");
        assert_eq!(module_id.origin, ModuleOrigin::Internal);

        assert_eq!(dependencies.len(), 2);
        let dep_names: Vec<&str> = dependencies
            .iter()
            .map(|d| d.canonical_path.as_str())
            .collect();
        assert!(dep_names.contains(&"os"));
        assert!(dep_names.contains(&"sys"));
    }

    #[test]
    fn test_analyze_python_file_no_imports() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let file_path = create_temp_python_file(
            temp_dir.path(),
            "simple.py",
            "def hello():\n    return 'world'",
        );

        let result = analyze_python_file(&file_path);
        assert!(result.is_ok());

        let (module_id, dependencies) = result.unwrap();
        assert_eq!(module_id.canonical_path, "simple");
        assert_eq!(dependencies.len(), 0);
    }

    #[test]
    fn test_analyze_python_file_complex_imports() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let content = r#"
import json
from os import path
from collections import defaultdict
import numpy as np
"#;
        let file_path = create_temp_python_file(temp_dir.path(), "complex.py", content);

        let result = analyze_python_file(&file_path);
        assert!(result.is_ok());

        let (module_id, dependencies) = result.unwrap();
        assert_eq!(module_id.canonical_path, "complex");

        let dep_names: Vec<&str> = dependencies
            .iter()
            .map(|d| d.canonical_path.as_str())
            .collect();
        assert!(dep_names.contains(&"json"));
        assert!(dep_names.contains(&"os"));
        assert!(dep_names.contains(&"collections"));
        assert!(dep_names.contains(&"numpy"));
    }

    #[test]
    fn test_analyze_python_file_nonexistent() {
        let nonexistent_path = Path::new("/nonexistent/file.py");
        let result = analyze_python_file(nonexistent_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_build_directory_dependency_graph_empty() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        let result = build_directory_dependency_graph(temp_dir.path());
        assert!(result.is_ok());

        let graph = result.unwrap();
        assert_eq!(graph.module_count(), 0);
        assert_eq!(graph.dependency_count(), 0);
    }

    #[test]
    fn test_build_directory_dependency_graph_single_file() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        create_temp_python_file(temp_dir.path(), "main.py", "import os\nimport sys");

        let result = build_directory_dependency_graph(temp_dir.path());
        assert!(result.is_ok());

        let graph = result.unwrap();
        assert_eq!(graph.module_count(), 3); // main + os + sys
        assert_eq!(graph.dependency_count(), 2); // main -> os, main -> sys
    }

    #[test]
    fn test_build_directory_dependency_graph_multiple_files() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        create_temp_python_file(
            temp_dir.path(),
            "module1.py",
            "import json\nfrom os import path",
        );
        create_temp_python_file(temp_dir.path(), "module2.py", "import sys\nimport json");
        create_temp_python_file(temp_dir.path(), "module3.py", "# No imports");

        let result = build_directory_dependency_graph(temp_dir.path());
        assert!(result.is_ok());

        let graph = result.unwrap();

        // Should have: module1, module2, module3, json, os, sys
        assert_eq!(graph.module_count(), 6);
        // Dependencies: module1->json, module1->os, module2->sys, module2->json
        assert_eq!(graph.dependency_count(), 4);

        // Verify specific modules exist
        let all_modules: Vec<&str> = graph
            .all_modules()
            .map(|m| m.canonical_path.as_str())
            .collect();
        assert!(all_modules.contains(&"module1"));
        assert!(all_modules.contains(&"module2"));
        assert!(all_modules.contains(&"module3"));
        assert!(all_modules.contains(&"json"));
        assert!(all_modules.contains(&"os"));
        assert!(all_modules.contains(&"sys"));
    }

    #[test]
    fn test_build_directory_dependency_graph_with_shared_dependencies() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        create_temp_python_file(temp_dir.path(), "app.py", "import common\nimport json");
        create_temp_python_file(temp_dir.path(), "test.py", "import common\nimport unittest");

        let result = build_directory_dependency_graph(temp_dir.path());
        assert!(result.is_ok());

        let graph = result.unwrap();

        // Should have: app, test, common, json, unittest (5 modules)
        assert_eq!(graph.module_count(), 5);
        // Dependencies: app->common, app->json, test->common, test->unittest (4 deps)
        assert_eq!(graph.dependency_count(), 4);
    }

    #[test]
    fn test_build_directory_dependency_graph_nonexistent_directory() {
        let nonexistent_path = Path::new("/nonexistent/directory");
        let result = build_directory_dependency_graph(nonexistent_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_analyze_python_directory_recursive_nested() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let dir_path = temp_dir.path();

        // Create nested structure
        fs::create_dir_all(dir_path.join("package/subpackage")).unwrap();
        create_temp_python_file(dir_path, "main.py", "import os");
        create_temp_python_file(&dir_path.join("package"), "module.py", "import sys");
        create_temp_python_file(
            &dir_path.join("package/subpackage"),
            "deep.py",
            "import json",
        );
        create_temp_python_file(&dir_path.join("package"), "__init__.py", "");

        let result = analyze_python_directory_recursive(dir_path);
        assert!(result.is_ok());
        let files = result.unwrap();

        assert_eq!(files.len(), 4);
        let filenames: Vec<String> = files
            .iter()
            .map(|f| {
                f.strip_prefix(dir_path)
                    .unwrap()
                    .to_string_lossy()
                    .to_string()
            })
            .collect();

        assert!(filenames.contains(&"main.py".to_string()));
        assert!(filenames.contains(&PathBuf::from("package").join("module.py").to_string_lossy().to_string()));
        assert!(filenames.contains(&PathBuf::from("package").join("__init__.py").to_string_lossy().to_string()));
        assert!(filenames.contains(&PathBuf::from("package").join("subpackage").join("deep.py").to_string_lossy().to_string()));
    }

    #[test]
    fn test_analyze_python_file_with_package() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let project_root = temp_dir.path();

        // Create nested structure
        fs::create_dir_all(project_root.join("package")).unwrap();
        let file_path = create_temp_python_file(
            &project_root.join("package"),
            "module.py",
            "import os\nimport sys",
        );

        let result = analyze_python_file_with_package(&file_path, project_root);
        assert!(result.is_ok());

        let (module_id, dependencies) = result.unwrap();
        assert_eq!(module_id.canonical_path, "package.module");
        assert_eq!(module_id.origin, ModuleOrigin::Internal);

        assert_eq!(dependencies.len(), 2);
        let dep_names: Vec<&str> = dependencies
            .iter()
            .map(|d| d.canonical_path.as_str())
            .collect();
        assert!(dep_names.contains(&"os"));
        assert!(dep_names.contains(&"sys"));
    }
}