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
//! Stdlib os.path method converters
//!
//! DEPYLER-REFACTOR: Extracted from expr_gen/mod.rs
//!
//! Contains converters for Python os.path module:
//! - `try_convert_os_path_method` — Maps os.path calls to std::path + std::fs
use super::ExpressionConverter;
use crate::hir::*;
use crate::rust_gen::context::ToRustExpr;
use anyhow::{bail, Result};
use syn::parse_quote;
impl<'a, 'b> ExpressionConverter<'a, 'b> {
/// Try to convert os.path module method calls
/// DEPYLER-STDLIB-OSPATH: Path manipulation and file system operations
///
/// Maps Python os.path module to Rust std::path + std::fs:
/// - os.path.join() → PathBuf::new().join()
/// - os.path.basename() → Path::file_name()
/// - os.path.exists() → Path::exists()
///
/// # Complexity
/// 10 (match with 10 primary branches - split into helper methods as needed)
#[inline]
pub(crate) fn try_convert_os_path_method(
&mut self,
method: &str,
args: &[HirExpr],
) -> Result<Option<syn::Expr>> {
// Convert arguments first
let arg_exprs: Vec<syn::Expr> = args
.iter()
.map(|arg| arg.to_rust_expr(self.ctx))
.collect::<Result<Vec<_>>>()?;
// DEPYLER-0594: Removed maybe_borrow closure - always use explicit & for Path::new()
// Path::new() requires &S, and subcommand field bindings create owned Strings
// Using & consistently is simpler and works for both owned and borrowed values
let result = match method {
// Path construction
"join" => {
if arg_exprs.is_empty() {
bail!("os.path.join() requires at least 1 argument");
}
// os.path.join(a, b, c, ...) → PathBuf::from(a).join(b).join(c)...
let first = &arg_exprs[0];
if arg_exprs.len() == 1 {
parse_quote! { std::path::PathBuf::from(#first) }
} else {
let mut result: syn::Expr = parse_quote! { std::path::PathBuf::from(#first) };
for part in &arg_exprs[1..] {
result = parse_quote! { #result.join(#part) };
}
parse_quote! { #result.to_string_lossy().to_string() }
}
}
// Path decomposition
"basename" => {
if arg_exprs.len() != 1 {
bail!("os.path.basename() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
// Path::new() requires &S where S: AsRef<OsStr>
// Subcommand field bindings create owned Strings that need borrowing
let path = &arg_exprs[0];
// os.path.basename(path) → Path::new(&path).file_name()
parse_quote! {
std::path::Path::new(&#path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_string()
}
}
"dirname" => {
if arg_exprs.len() != 1 {
bail!("os.path.dirname() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.dirname(path) → Path::new(&path).parent()
parse_quote! {
std::path::Path::new(&#path)
.parent()
.and_then(|p| p.to_str())
.unwrap_or("")
.to_string()
}
}
"split" => {
if arg_exprs.len() != 1 {
bail!("os.path.split() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.split(path) → (dirname, basename) tuple
parse_quote! {
{
let p = std::path::Path::new(&#path);
let dirname = p.parent().and_then(|p| p.to_str()).unwrap_or("").to_string();
let basename = p.file_name().and_then(|n| n.to_str()).unwrap_or("").to_string();
(dirname, basename)
}
}
}
"splitext" => {
if arg_exprs.len() != 1 {
bail!("os.path.splitext() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.splitext(path) → (stem, extension) tuple
parse_quote! {
{
let p = std::path::Path::new(&#path);
let stem = p.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_string();
let ext = p.extension().and_then(|e| e.to_str()).map(|e| format!(".{}", e)).unwrap_or_default();
(stem, ext)
}
}
}
// Path predicates
"exists" => {
if arg_exprs.len() != 1 {
bail!("os.path.exists() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.exists(path) → Path::new(&path).exists()
parse_quote! { std::path::Path::new(&#path).exists() }
}
"isfile" => {
if arg_exprs.len() != 1 {
bail!("os.path.isfile() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.isfile(path) → Path::new(&path).is_file()
parse_quote! { std::path::Path::new(&#path).is_file() }
}
"isdir" => {
if arg_exprs.len() != 1 {
bail!("os.path.isdir() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.isdir(path) → Path::new(&path).is_dir()
parse_quote! { std::path::Path::new(&#path).is_dir() }
}
"isabs" => {
if arg_exprs.len() != 1 {
bail!("os.path.isabs() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.isabs(path) → Path::new(&path).is_absolute()
parse_quote! { std::path::Path::new(&#path).is_absolute() }
}
// Path normalization
"abspath" => {
if arg_exprs.len() != 1 {
bail!("os.path.abspath() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for fs::canonicalize and PathBuf::from
let path = &arg_exprs[0];
// os.path.abspath(path) → std::fs::canonicalize() or manual absolute path
// Using canonicalize (resolves symlinks too, like realpath)
parse_quote! {
std::fs::canonicalize(&#path)
.unwrap_or_else(|_| std::path::PathBuf::from(&#path))
.to_string_lossy()
.to_string()
}
}
"normpath" => {
if arg_exprs.len() != 1 {
bail!("os.path.normpath() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for Path::new()
let path = &arg_exprs[0];
// os.path.normpath(path) → normalize path components
// Rust Path doesn't have direct normpath, but we can use PathBuf operations
parse_quote! {
{
let p = std::path::Path::new(&#path);
let mut components = Vec::new();
for component in p.components() {
match component {
std::path::Component::CurDir => {},
std::path::Component::ParentDir => {
components.pop();
}
_ => components.push(component),
}
}
components.iter()
.map(|c| c.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join(std::path::MAIN_SEPARATOR_STR)
}
}
}
"realpath" => {
if arg_exprs.len() != 1 {
bail!("os.path.realpath() requires exactly 1 argument");
}
let path = &arg_exprs[0];
// os.path.realpath(path) → std::fs::canonicalize()
parse_quote! {
std::fs::canonicalize(#path)
.unwrap_or_else(|_| std::path::PathBuf::from(#path))
.to_string_lossy()
.to_string()
}
}
// Path properties
"getsize" => {
if arg_exprs.len() != 1 {
bail!("os.path.getsize() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for std::fs::metadata()
let path = &arg_exprs[0];
// os.path.getsize(path) → std::fs::metadata().len()
parse_quote! {
std::fs::metadata(&#path).expect("operation failed").len() as i64
}
}
"getmtime" => {
if arg_exprs.len() != 1 {
bail!("os.path.getmtime() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for std::fs::metadata()
let path = &arg_exprs[0];
// os.path.getmtime(path) → std::fs::metadata().modified()
parse_quote! {
std::fs::metadata(&#path)
.expect("operation failed")
.modified()
.expect("operation failed")
.duration_since(std::time::UNIX_EPOCH)
.expect("operation failed")
.as_secs_f64()
}
}
"getctime" => {
if arg_exprs.len() != 1 {
bail!("os.path.getctime() requires exactly 1 argument");
}
// DEPYLER-0594: Always use reference for std::fs::metadata()
let path = &arg_exprs[0];
// os.path.getctime(path) → std::fs::metadata().created()
// Note: On Unix, this is ctime (change time), but Rust only has created()
parse_quote! {
std::fs::metadata(&#path)
.expect("operation failed")
.created()
.expect("operation failed")
.duration_since(std::time::UNIX_EPOCH)
.expect("operation failed")
.as_secs_f64()
}
}
// Path expansion
"expanduser" => {
if arg_exprs.len() != 1 {
bail!("os.path.expanduser() requires exactly 1 argument");
}
let path = &arg_exprs[0];
// os.path.expanduser(path) → expand ~ to home directory
parse_quote! {
{
let p = #path;
if p.starts_with("~") {
if let Some(home) = std::env::var_os("HOME") {
format!("{}{}", home.to_string_lossy(), &p[1..])
} else {
p.to_string()
}
} else {
p.to_string()
}
}
}
}
"expandvars" => {
if arg_exprs.len() != 1 {
bail!("os.path.expandvars() requires exactly 1 argument");
}
let path = &arg_exprs[0];
// os.path.expandvars(path) → expand environment variables
// Simplified: just return path as-is for now (full implementation complex)
parse_quote! { #path.to_string() }
}
// DEPYLER-STDLIB-OSPATH: relpath() - compute relative path
"relpath" => {
if arg_exprs.len() != 2 {
bail!("os.path.relpath() requires exactly 2 arguments");
}
let path = &arg_exprs[0];
let start = &arg_exprs[1];
// os.path.relpath(path, start) → compute relative path from start to path
parse_quote! {
{
let path_obj = std::path::Path::new(#path);
let start_obj = std::path::Path::new(#start);
path_obj
.strip_prefix(start_obj)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|_| #path.to_string())
}
}
}
_ => {
// For functions not yet implemented, return None to allow fallback
return Ok(None);
}
};
Ok(Some(result))
}
}