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
//! Common utilities for environment discovery
//!
//! This module contains shared functions used by all discovery implementations
//! to avoid code duplication (DRY principle).
use ;
use cratepaths;
use EnvironmentStatus;
/// EOL Python minor version threshold (3.8 and below are EOL as of 2024)
const EOL_PYTHON_MINOR: u32 = 8;
/// Calculate directory size in bytes.
///
/// # Performance Note
///
/// This function traverses the entire directory tree and calls `stat()` on every file.
/// For large environments with thousands of files, this can be expensive.
/// Consider using `Option<u64>` in `SourceEnvironment` and calculating size lazily.
///
/// # Examples
///
/// ```
/// use std::path::Path;
/// use scoop_uv::core::migrate::common::dir_size;
///
/// # // Create temp directory with test files
/// # let temp = tempfile::tempdir().unwrap();
/// # std::fs::write(temp.path().join("file1.txt"), "hello").unwrap();
/// # std::fs::write(temp.path().join("file2.txt"), "world").unwrap();
/// # let venv_path = temp.path();
/// let size = dir_size(venv_path);
/// # assert_eq!(size, 10); // "hello" + "world" = 10 bytes
/// let size_mb = size as f64 / 1_048_576.0;
/// println!("Environment size: {:.1} MB", size_mb);
/// ```
/// Check if environment name conflicts with existing scoop environment.
///
/// Returns `Some(path)` if a scoop environment with the same name exists,
/// `None` otherwise.
///
/// # Examples
///
/// ```
/// use scoop_uv::core::migrate::common::check_name_conflict;
///
/// # // Setup: create temp SCOOP_HOME with an existing environment
/// # let temp = tempfile::tempdir().unwrap();
/// # let venvs_dir = temp.path().join("virtualenvs");
/// # std::fs::create_dir_all(&venvs_dir).unwrap();
/// # std::fs::create_dir(venvs_dir.join("existing-env")).unwrap();
/// # // SAFETY: Single-threaded doctest, no concurrent access
/// # unsafe { std::env::set_var("SCOOP_HOME", temp.path()); }
/// #
/// // Check for existing environment
/// if let Some(existing_path) = check_name_conflict("existing-env") {
/// println!("Conflict: {} already exists", existing_path.display());
/// }
/// # assert!(check_name_conflict("existing-env").is_some());
///
/// // Check for non-existing environment
/// assert!(check_name_conflict("new-project").is_none());
/// # // SAFETY: Restoring original environment
/// # unsafe { std::env::remove_var("SCOOP_HOME"); }
/// ```
/// Determine environment status based on name conflicts and Python version.
///
/// # Status Priority
///
/// 1. Name conflict (existing scoop environment)
/// 2. Python EOL (3.8 and below, or Python 2.x)
/// 3. Ready to migrate
///
/// # Examples
///
/// ```
/// use scoop_uv::core::migrate::common::determine_status;
/// use scoop_uv::core::migrate::EnvironmentStatus;
///
/// # // Setup: isolated SCOOP_HOME to avoid real conflicts
/// # let temp = tempfile::tempdir().unwrap();
/// # std::fs::create_dir_all(temp.path().join("virtualenvs")).unwrap();
/// # // SAFETY: Single-threaded doctest, no concurrent access
/// # unsafe { std::env::set_var("SCOOP_HOME", temp.path()); }
/// #
/// // Modern Python, no conflict
/// let status = determine_status("new_env", "3.12.0");
/// assert!(matches!(status, EnvironmentStatus::Ready));
///
/// // EOL Python version
/// let status = determine_status("old_env", "3.7.0");
/// assert!(matches!(status, EnvironmentStatus::PythonEol { .. }));
///
/// // Python 2.x is definitely EOL
/// let status = determine_status("ancient_env", "2.7.18");
/// assert!(matches!(status, EnvironmentStatus::PythonEol { .. }));
/// # // SAFETY: Restoring original environment
/// # unsafe { std::env::remove_var("SCOOP_HOME"); }
/// ```