-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_crawler.rs
More file actions
875 lines (777 loc) · 29.5 KB
/
python_crawler.rs
File metadata and controls
875 lines (777 loc) · 29.5 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use super::types::{CrawledPackage, CrawlerOptions};
// ---------------------------------------------------------------------------
// Python command discovery
// ---------------------------------------------------------------------------
/// Find a working Python command on the system.
///
/// Tries `python3`, `python`, and `py` (Windows launcher) in order,
/// returning the first one that responds to `--version`.
pub fn find_python_command() -> Option<&'static str> {
["python3", "python", "py"].into_iter().find(|cmd| {
Command::new(cmd)
.args(["--version"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok()
})
}
/// Default batch size for crawling.
const _DEFAULT_BATCH_SIZE: usize = 100;
// ---------------------------------------------------------------------------
// PEP 503 name canonicalization
// ---------------------------------------------------------------------------
/// Canonicalize a Python package name per PEP 503.
///
/// Lowercases, trims, and replaces runs of `[-_.]` with a single `-`.
pub fn canonicalize_pypi_name(name: &str) -> String {
let trimmed = name.trim().to_lowercase();
let mut result = String::with_capacity(trimmed.len());
let mut in_separator_run = false;
for ch in trimmed.chars() {
if ch == '-' || ch == '_' || ch == '.' {
if !in_separator_run {
result.push('-');
in_separator_run = true;
}
// else: skip consecutive separators
} else {
in_separator_run = false;
result.push(ch);
}
}
result
}
// ---------------------------------------------------------------------------
// Helpers: read Python metadata from dist-info
// ---------------------------------------------------------------------------
/// Read `Name` and `Version` from a `.dist-info/METADATA` file.
pub async fn read_python_metadata(dist_info_path: &Path) -> Option<(String, String)> {
let metadata_path = dist_info_path.join("METADATA");
let content = tokio::fs::read_to_string(&metadata_path).await.ok()?;
let mut name: Option<String> = None;
let mut version: Option<String> = None;
for line in content.lines() {
if name.is_some() && version.is_some() {
break;
}
if let Some(rest) = line.strip_prefix("Name:") {
name = Some(rest.trim().to_string());
} else if let Some(rest) = line.strip_prefix("Version:") {
version = Some(rest.trim().to_string());
}
// Stop at first empty line (end of headers)
if line.trim().is_empty() && (name.is_some() || version.is_some()) {
break;
}
}
match (name, version) {
(Some(n), Some(v)) if !n.is_empty() && !v.is_empty() => Some((n, v)),
_ => None,
}
}
// ---------------------------------------------------------------------------
// Helpers: find Python directories with wildcard matching
// ---------------------------------------------------------------------------
/// Find directories matching a path pattern with wildcard segments.
///
/// Supported wildcards:
/// - `"python3.*"` — matches directory entries starting with `python3.`
/// - `"*"` — matches any directory entry
///
/// All other segments are treated as literal path components.
pub async fn find_python_dirs(base_path: &Path, segments: &[&str]) -> Vec<PathBuf> {
let mut results = Vec::new();
// Check that base_path is a directory
match tokio::fs::metadata(base_path).await {
Ok(m) if m.is_dir() => {}
_ => return results,
}
if segments.is_empty() {
results.push(base_path.to_path_buf());
return results;
}
let first = segments[0];
let rest = &segments[1..];
if first == "python3.*" {
// Wildcard: list directory and match python3.X entries
if let Ok(mut entries) = tokio::fs::read_dir(base_path).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let ft = match entry.file_type().await {
Ok(ft) => ft,
Err(_) => continue,
};
if !ft.is_dir() {
continue;
}
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with("python3.") {
let sub = Box::pin(find_python_dirs(
&base_path.join(entry.file_name()),
rest,
))
.await;
results.extend(sub);
}
}
}
} else if first == "*" {
// Generic wildcard: match any directory entry
if let Ok(mut entries) = tokio::fs::read_dir(base_path).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let ft = match entry.file_type().await {
Ok(ft) => ft,
Err(_) => continue,
};
if !ft.is_dir() {
continue;
}
let sub = Box::pin(find_python_dirs(
&base_path.join(entry.file_name()),
rest,
))
.await;
results.extend(sub);
}
}
} else {
// Literal segment: just check if it exists
let sub =
Box::pin(find_python_dirs(&base_path.join(first), rest)).await;
results.extend(sub);
}
results
}
// ---------------------------------------------------------------------------
// Helpers: site-packages discovery
// ---------------------------------------------------------------------------
/// Find `site-packages` (or `dist-packages`) directories under a base dir.
///
/// Handles both Unix (`lib/python3.X/site-packages`) and macOS/Linux layouts.
pub async fn find_site_packages_under(
base_dir: &Path,
sub_dir_type: &str, // "site-packages" or "dist-packages"
) -> Vec<PathBuf> {
if cfg!(windows) {
find_python_dirs(base_dir, &["Lib", sub_dir_type]).await
} else {
find_python_dirs(base_dir, &["lib", "python3.*", sub_dir_type]).await
}
}
/// Find local virtual environment `site-packages` directories.
///
/// Checks (in order):
/// 1. `VIRTUAL_ENV` environment variable
/// 2. `.venv` directory in `cwd`
/// 3. `venv` directory in `cwd`
pub async fn find_local_venv_site_packages(cwd: &Path) -> Vec<PathBuf> {
let mut results = Vec::new();
// 1. Check VIRTUAL_ENV env var
if let Ok(virtual_env) = std::env::var("VIRTUAL_ENV") {
let venv_path = PathBuf::from(&virtual_env);
let matches = find_site_packages_under(&venv_path, "site-packages").await;
results.extend(matches);
if !results.is_empty() {
return results;
}
}
// 2. Check .venv and venv in cwd
for venv_dir in &[".venv", "venv"] {
let venv_path = cwd.join(venv_dir);
let matches = find_site_packages_under(&venv_path, "site-packages").await;
results.extend(matches);
}
results
}
/// Get global/system Python `site-packages` directories.
///
/// Queries `python3` for site-packages paths, then checks well-known system
/// locations including Homebrew, conda, uv tools, pip --user, etc.
pub async fn get_global_python_site_packages() -> Vec<PathBuf> {
let mut results = Vec::new();
let mut seen = HashSet::new();
let add_path = |p: PathBuf, seen: &mut HashSet<PathBuf>, results: &mut Vec<PathBuf>| {
let resolved = if p.is_absolute() {
p
} else {
std::path::absolute(&p).unwrap_or(p)
};
if seen.insert(resolved.clone()) {
results.push(resolved);
}
};
// 1. Ask Python for site-packages
if let Some(python_cmd) = find_python_command() {
if let Ok(output) = Command::new(python_cmd)
.args([
"-c",
"import site; print('\\n'.join(site.getsitepackages())); print(site.getusersitepackages())",
])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
{
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
let p = line.trim();
if !p.is_empty() {
add_path(PathBuf::from(p), &mut seen, &mut results);
}
}
}
}
}
// 2. Well-known system paths
let home_dir = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.unwrap_or_else(|_| "~".to_string());
// Helper closure to scan base/lib/python3.*/[dist|site]-packages
async fn scan_well_known(
base: &Path,
pkg_type: &str,
seen: &mut HashSet<PathBuf>,
results: &mut Vec<PathBuf>,
) {
let matches = find_python_dirs(base, &["lib", "python3.*", pkg_type]).await;
for m in matches {
let resolved = if m.is_absolute() {
m
} else {
std::path::absolute(&m).unwrap_or(m)
};
if seen.insert(resolved.clone()) {
results.push(resolved);
}
}
}
if !cfg!(windows) {
// Debian/Ubuntu
scan_well_known(Path::new("/usr"), "dist-packages", &mut seen, &mut results).await;
scan_well_known(Path::new("/usr"), "site-packages", &mut seen, &mut results).await;
// Debian pip / most distros / macOS
scan_well_known(
Path::new("/usr/local"),
"dist-packages",
&mut seen,
&mut results,
)
.await;
scan_well_known(
Path::new("/usr/local"),
"site-packages",
&mut seen,
&mut results,
)
.await;
// pip --user on Unix
let user_local = PathBuf::from(&home_dir).join(".local");
scan_well_known(&user_local, "site-packages", &mut seen, &mut results).await;
}
// macOS-specific
if cfg!(target_os = "macos") {
scan_well_known(
Path::new("/opt/homebrew"),
"site-packages",
&mut seen,
&mut results,
)
.await;
// Python.org framework
let fw_matches = find_python_dirs(
Path::new("/Library/Frameworks/Python.framework/Versions"),
&["python3.*", "lib", "python3.*", "site-packages"],
)
.await;
for m in fw_matches {
add_path(m, &mut seen, &mut results);
}
let fw_matches2 = find_python_dirs(
Path::new("/Library/Frameworks/Python.framework"),
&["Versions", "*", "lib", "python3.*", "site-packages"],
)
.await;
for m in fw_matches2 {
add_path(m, &mut seen, &mut results);
}
}
// Windows-specific
if cfg!(windows) {
// pip --user on Windows: %APPDATA%\Python\PythonXY\site-packages
if let Ok(appdata) = std::env::var("APPDATA") {
let appdata_python = PathBuf::from(&appdata).join("Python");
if let Ok(mut entries) = tokio::fs::read_dir(&appdata_python).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let p = appdata_python.join(entry.file_name()).join("site-packages");
if tokio::fs::metadata(&p).await.is_ok() {
add_path(p, &mut seen, &mut results);
}
}
}
}
// Common Windows Python install locations
for base in &["C:\\Python", "C:\\Program Files\\Python"] {
if let Ok(mut entries) = tokio::fs::read_dir(base).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let sp = PathBuf::from(base)
.join(entry.file_name())
.join("Lib")
.join("site-packages");
if tokio::fs::metadata(&sp).await.is_ok() {
add_path(sp, &mut seen, &mut results);
}
}
}
}
// Microsoft Store / python.org via LocalAppData
if let Ok(local) = std::env::var("LOCALAPPDATA") {
let programs_python = PathBuf::from(&local).join("Programs").join("Python");
if let Ok(mut entries) = tokio::fs::read_dir(&programs_python).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let sp = programs_python
.join(entry.file_name())
.join("Lib")
.join("site-packages");
if tokio::fs::metadata(&sp).await.is_ok() {
add_path(sp, &mut seen, &mut results);
}
}
}
}
}
// pyenv (works on macOS and Linux)
if !cfg!(windows) {
let pyenv_root = std::env::var("PYENV_ROOT")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from(&home_dir).join(".pyenv"));
let pyenv_versions = pyenv_root.join("versions");
let pyenv_matches = find_python_dirs(
&pyenv_versions,
&["*", "lib", "python3.*", "site-packages"],
)
.await;
for m in pyenv_matches {
add_path(m, &mut seen, &mut results);
}
}
// Conda
let anaconda = PathBuf::from(&home_dir).join("anaconda3");
scan_well_known(&anaconda, "site-packages", &mut seen, &mut results).await;
let miniconda = PathBuf::from(&home_dir).join("miniconda3");
scan_well_known(&miniconda, "site-packages", &mut seen, &mut results).await;
// uv tools
if cfg!(target_os = "macos") {
let uv_base = PathBuf::from(&home_dir)
.join("Library")
.join("Application Support")
.join("uv")
.join("tools");
let uv_matches =
find_python_dirs(&uv_base, &["*", "lib", "python3.*", "site-packages"]).await;
for m in uv_matches {
add_path(m, &mut seen, &mut results);
}
} else if cfg!(windows) {
// %LOCALAPPDATA%\uv\tools
if let Ok(local) = std::env::var("LOCALAPPDATA") {
let uv_base = PathBuf::from(local).join("uv").join("tools");
let uv_matches =
find_python_dirs(&uv_base, &["*", "Lib", "site-packages"]).await;
for m in uv_matches {
add_path(m, &mut seen, &mut results);
}
}
} else {
let uv_base = PathBuf::from(&home_dir)
.join(".local")
.join("share")
.join("uv")
.join("tools");
let uv_matches =
find_python_dirs(&uv_base, &["*", "lib", "python3.*", "site-packages"]).await;
for m in uv_matches {
add_path(m, &mut seen, &mut results);
}
}
results
}
// ---------------------------------------------------------------------------
// PythonCrawler
// ---------------------------------------------------------------------------
/// Python ecosystem crawler for discovering packages in `site-packages`.
pub struct PythonCrawler;
impl PythonCrawler {
/// Create a new `PythonCrawler`.
pub fn new() -> Self {
Self
}
/// Get `site-packages` paths based on options.
pub async fn get_site_packages_paths(&self, options: &CrawlerOptions) -> Result<Vec<PathBuf>, std::io::Error> {
if options.global || options.global_prefix.is_some() {
if let Some(ref custom) = options.global_prefix {
return Ok(vec![custom.clone()]);
}
return Ok(get_global_python_site_packages().await);
}
Ok(find_local_venv_site_packages(&options.cwd).await)
}
/// Crawl all discovered `site-packages` and return every package found.
pub async fn crawl_all(&self, options: &CrawlerOptions) -> Vec<CrawledPackage> {
let mut packages = Vec::new();
let mut seen = HashSet::new();
let sp_paths = self.get_site_packages_paths(options).await.unwrap_or_default();
for sp_path in &sp_paths {
let found = self.scan_site_packages(sp_path, &mut seen).await;
packages.extend(found);
}
packages
}
/// Find specific packages by PURL.
///
/// Accepts base PURLs (no qualifiers) — the caller should strip qualifiers
/// before calling.
pub async fn find_by_purls(
&self,
site_packages_path: &Path,
purls: &[String],
) -> Result<HashMap<String, CrawledPackage>, std::io::Error> {
let mut result = HashMap::new();
// Build lookup: canonicalized-name@version -> purl
let mut purl_lookup: HashMap<String, &str> = HashMap::new();
for purl in purls {
if let Some((name, version)) = Self::parse_pypi_purl(purl) {
let key = format!("{}@{}", canonicalize_pypi_name(&name), version);
purl_lookup.insert(key, purl.as_str());
}
}
if purl_lookup.is_empty() {
return Ok(result);
}
// Scan all .dist-info dirs
let entries = match tokio::fs::read_dir(site_packages_path).await {
Ok(rd) => {
let mut entries = rd;
let mut v = Vec::new();
while let Ok(Some(entry)) = entries.next_entry().await {
v.push(entry);
}
v
}
Err(_) => return Ok(result),
};
for entry in entries {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if !name_str.ends_with(".dist-info") {
continue;
}
let dist_info_path = site_packages_path.join(&*name_str);
if let Some((raw_name, version)) = read_python_metadata(&dist_info_path).await {
let canon_name = canonicalize_pypi_name(&raw_name);
let key = format!("{canon_name}@{version}");
if let Some(&matched_purl) = purl_lookup.get(&key) {
result.insert(
matched_purl.to_string(),
CrawledPackage {
name: canon_name,
version,
namespace: None,
purl: matched_purl.to_string(),
path: site_packages_path.to_path_buf(),
},
);
}
}
}
Ok(result)
}
// ------------------------------------------------------------------
// Private helpers
// ------------------------------------------------------------------
/// Scan a `site-packages` directory for `.dist-info` directories.
async fn scan_site_packages(
&self,
site_packages_path: &Path,
seen: &mut HashSet<String>,
) -> Vec<CrawledPackage> {
let mut results = Vec::new();
let entries = match tokio::fs::read_dir(site_packages_path).await {
Ok(rd) => {
let mut entries = rd;
let mut v = Vec::new();
while let Ok(Some(entry)) = entries.next_entry().await {
v.push(entry);
}
v
}
Err(_) => return results,
};
for entry in entries {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if !name_str.ends_with(".dist-info") {
continue;
}
let dist_info_path = site_packages_path.join(&*name_str);
if let Some((raw_name, version)) = read_python_metadata(&dist_info_path).await {
let canon_name = canonicalize_pypi_name(&raw_name);
let purl = format!("pkg:pypi/{canon_name}@{version}");
if seen.contains(&purl) {
continue;
}
seen.insert(purl.clone());
results.push(CrawledPackage {
name: canon_name,
version,
namespace: None,
purl,
path: site_packages_path.to_path_buf(),
});
}
}
results
}
/// Parse a PyPI PURL string to extract name and version.
/// Strips qualifiers before parsing.
fn parse_pypi_purl(purl: &str) -> Option<(String, String)> {
// Strip qualifiers
let base = match purl.find('?') {
Some(idx) => &purl[..idx],
None => purl,
};
let rest = base.strip_prefix("pkg:pypi/")?;
let at_idx = rest.rfind('@')?;
let name = &rest[..at_idx];
let version = &rest[at_idx + 1..];
if name.is_empty() || version.is_empty() {
return None;
}
Some((name.to_string(), version.to_string()))
}
}
impl Default for PythonCrawler {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_canonicalize_pypi_name_basic() {
assert_eq!(canonicalize_pypi_name("Requests"), "requests");
assert_eq!(canonicalize_pypi_name("my_package"), "my-package");
assert_eq!(canonicalize_pypi_name("My.Package"), "my-package");
assert_eq!(canonicalize_pypi_name("My-._Package"), "my-package");
}
#[test]
fn test_canonicalize_pypi_name_runs() {
// Runs of separators collapse to single -
assert_eq!(canonicalize_pypi_name("a__b"), "a-b");
assert_eq!(canonicalize_pypi_name("a-.-b"), "a-b");
assert_eq!(canonicalize_pypi_name("a_._-b"), "a-b");
}
#[test]
fn test_canonicalize_pypi_name_trim() {
assert_eq!(canonicalize_pypi_name(" requests "), "requests");
}
#[test]
fn test_parse_pypi_purl() {
let (name, ver) = PythonCrawler::parse_pypi_purl("pkg:pypi/requests@2.28.0").unwrap();
assert_eq!(name, "requests");
assert_eq!(ver, "2.28.0");
}
#[test]
fn test_parse_pypi_purl_with_qualifiers() {
let (name, ver) =
PythonCrawler::parse_pypi_purl("pkg:pypi/requests@2.28.0?artifact_id=abc").unwrap();
assert_eq!(name, "requests");
assert_eq!(ver, "2.28.0");
}
#[test]
fn test_parse_pypi_purl_invalid() {
assert!(PythonCrawler::parse_pypi_purl("pkg:npm/lodash@4.17.21").is_none());
assert!(PythonCrawler::parse_pypi_purl("not-a-purl").is_none());
}
#[tokio::test]
async fn test_read_python_metadata_valid() {
let dir = tempfile::tempdir().unwrap();
let dist_info = dir.path().join("requests-2.28.0.dist-info");
tokio::fs::create_dir_all(&dist_info).await.unwrap();
tokio::fs::write(
dist_info.join("METADATA"),
"Metadata-Version: 2.1\nName: Requests\nVersion: 2.28.0\n\nSome description",
)
.await
.unwrap();
let result = read_python_metadata(&dist_info).await;
assert!(result.is_some());
let (name, version) = result.unwrap();
assert_eq!(name, "Requests");
assert_eq!(version, "2.28.0");
}
#[tokio::test]
async fn test_read_python_metadata_missing() {
let dir = tempfile::tempdir().unwrap();
let dist_info = dir.path().join("nonexistent.dist-info");
assert!(read_python_metadata(&dist_info).await.is_none());
}
#[tokio::test]
async fn test_find_python_dirs_literal() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("lib").join("python3.11").join("site-packages");
tokio::fs::create_dir_all(&target).await.unwrap();
let results =
find_python_dirs(dir.path(), &["lib", "python3.*", "site-packages"]).await;
assert_eq!(results.len(), 1);
assert_eq!(results[0], target);
}
#[tokio::test]
async fn test_find_python_dirs_wildcard() {
let dir = tempfile::tempdir().unwrap();
let sp1 = dir.path().join("lib").join("python3.10").join("site-packages");
let sp2 = dir.path().join("lib").join("python3.11").join("site-packages");
tokio::fs::create_dir_all(&sp1).await.unwrap();
tokio::fs::create_dir_all(&sp2).await.unwrap();
// Also create a non-matching dir
let non_match = dir.path().join("lib").join("ruby3.0").join("site-packages");
tokio::fs::create_dir_all(&non_match).await.unwrap();
let results =
find_python_dirs(dir.path(), &["lib", "python3.*", "site-packages"]).await;
assert_eq!(results.len(), 2);
}
#[tokio::test]
async fn test_find_python_dirs_star_wildcard() {
let dir = tempfile::tempdir().unwrap();
let sp1 = dir
.path()
.join("tools")
.join("mytool")
.join("lib")
.join("python3.11")
.join("site-packages");
tokio::fs::create_dir_all(&sp1).await.unwrap();
let results = find_python_dirs(
dir.path(),
&["tools", "*", "lib", "python3.*", "site-packages"],
)
.await;
assert_eq!(results.len(), 1);
assert_eq!(results[0], sp1);
}
#[tokio::test]
async fn test_find_python_dirs_pyenv_layout() {
// Create a pyenv-like layout: versions/3.11.5/lib/python3.11/site-packages
let dir = tempfile::tempdir().unwrap();
let sp1 = dir
.path()
.join("versions")
.join("3.11.5")
.join("lib")
.join("python3.11")
.join("site-packages");
let sp2 = dir
.path()
.join("versions")
.join("3.12.0")
.join("lib")
.join("python3.12")
.join("site-packages");
tokio::fs::create_dir_all(&sp1).await.unwrap();
tokio::fs::create_dir_all(&sp2).await.unwrap();
let results = find_python_dirs(
&dir.path().join("versions"),
&["*", "lib", "python3.*", "site-packages"],
)
.await;
assert_eq!(results.len(), 2);
assert!(results.contains(&sp1));
assert!(results.contains(&sp2));
}
#[tokio::test]
async fn test_crawl_all_python() {
let dir = tempfile::tempdir().unwrap();
let venv = dir.path().join(".venv");
let sp = if cfg!(windows) {
venv.join("Lib").join("site-packages")
} else {
venv.join("lib").join("python3.11").join("site-packages")
};
tokio::fs::create_dir_all(&sp).await.unwrap();
// Create a dist-info dir with METADATA
let dist_info = sp.join("requests-2.28.0.dist-info");
tokio::fs::create_dir_all(&dist_info).await.unwrap();
tokio::fs::write(
dist_info.join("METADATA"),
"Metadata-Version: 2.1\nName: Requests\nVersion: 2.28.0\n",
)
.await
.unwrap();
let crawler = PythonCrawler::new();
let options = CrawlerOptions {
cwd: dir.path().to_path_buf(),
global: false,
global_prefix: None,
batch_size: 100,
};
let packages = crawler.crawl_all(&options).await;
assert_eq!(packages.len(), 1);
assert_eq!(packages[0].name, "requests");
assert_eq!(packages[0].version, "2.28.0");
assert_eq!(packages[0].purl, "pkg:pypi/requests@2.28.0");
assert!(packages[0].namespace.is_none());
}
#[test]
fn test_find_python_command() {
// On any platform with Python installed, this should return Some
// In CI environments, Python is typically available
let cmd = find_python_command();
// We don't assert Some because Python may not be installed,
// but if it is, the command should be valid
if let Some(c) = cmd {
assert!(
["python3", "python", "py"].contains(&c),
"unexpected command: {c}"
);
}
}
#[test]
fn test_home_dir_detection() {
// Verify the fallback chain works: HOME -> USERPROFILE -> "~"
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.unwrap_or_else(|_| "~".to_string());
// On any CI or dev machine, we should get a real path, not "~"
assert_ne!(home, "~", "expected a real home directory");
assert!(!home.is_empty());
}
#[tokio::test]
async fn test_find_by_purls_python() {
let dir = tempfile::tempdir().unwrap();
let sp = dir.path().to_path_buf();
// Create dist-info
let dist_info = sp.join("requests-2.28.0.dist-info");
tokio::fs::create_dir_all(&dist_info).await.unwrap();
tokio::fs::write(
dist_info.join("METADATA"),
"Metadata-Version: 2.1\nName: Requests\nVersion: 2.28.0\n",
)
.await
.unwrap();
let crawler = PythonCrawler::new();
let purls = vec![
"pkg:pypi/requests@2.28.0".to_string(),
"pkg:pypi/flask@3.0.0".to_string(),
];
let result = crawler.find_by_purls(&sp, &purls).await.unwrap();
assert_eq!(result.len(), 1);
assert!(result.contains_key("pkg:pypi/requests@2.28.0"));
assert!(!result.contains_key("pkg:pypi/flask@3.0.0"));
}
}