uv-migrator 2025.8.1

Tool for converting various python package soltutions to use the uv solution by astral
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
use log::debug;
use std::path::Path;
use toml_edit::{DocumentMut, Item, Table, Value};

/// Updates the build system configuration in pyproject.toml.
/// This function follows PEP 621 guidelines to determine if a project is a package
/// that needs a build system or an application that can use the default.
///
/// # Arguments
///
/// * `doc` - The TOML document to update
/// * `project_dir` - The project directory path
///
/// # Returns
///
/// * `bool` - Whether any changes were made to the document
pub fn update_build_system(doc: &mut DocumentMut, project_dir: &Path) -> Result<bool, String> {
    debug!("Checking if project needs a build system configuration");
    let old_pyproject_path = project_dir.join("old.pyproject.toml");
    if !old_pyproject_path.exists() {
        return Ok(false);
    }

    // Read old pyproject.toml to analyze the project structure
    let old_content = std::fs::read_to_string(&old_pyproject_path)
        .map_err(|e| format!("Failed to read old.pyproject.toml: {}", e))?;

    let old_doc = old_content
        .parse::<DocumentMut>()
        .map_err(|e| format!("Failed to parse old.pyproject.toml: {}", e))?;

    // Check if this is a package project according to PEP 621 and Poetry standards
    let is_package_project = determine_if_package_project(&old_doc, project_dir);

    // If it's not a package project, don't add a build-system section
    if !is_package_project {
        debug!("Project appears to be an application, not setting build system");
        return Ok(false);
    }

    debug!("Project appears to be a package, configuring build system with Hatchling");

    // Create new build-system table
    let mut build_system = Table::new();

    // Add requires array
    let mut requires = toml_edit::Array::new();
    requires.push(Value::String(toml_edit::Formatted::new(
        "hatchling".to_string(),
    )));
    build_system.insert("requires", Item::Value(Value::Array(requires)));

    // Add build-backend string
    build_system.insert(
        "build-backend",
        Item::Value(Value::String(toml_edit::Formatted::new(
            "hatchling.build".to_string(),
        ))),
    );

    // Update the document
    doc.insert("build-system", Item::Table(build_system));

    Ok(true)
}

/// Determines if a project is a package (vs an application) based on various indicators
fn determine_if_package_project(doc: &DocumentMut, project_dir: &Path) -> bool {
    // Check for various indicators that this is a package project:

    // 1. Check for Poetry packages configuration with src directory
    let has_poetry_package_config = doc
        .get("tool")
        .and_then(|t| t.get("poetry"))
        .and_then(|poetry| {
            // First try `packages` key directly in poetry
            if let Some(packages) = poetry.get("packages").and_then(|p| p.as_array()) {
                Some(packages.iter().any(|pkg| {
                    if let Some(table) = pkg.as_inline_table() {
                        (table.get("from").and_then(|f| f.as_str()) == Some("src"))
                            || (table.get("include").and_then(|i| i.as_str()).is_some()
                                && table.get("from").and_then(|f| f.as_str()) == Some("src"))
                    } else {
                        false
                    }
                }))
            }
            // Try packages array within tool.poetry.packages section
            else if let Some(packages_section) = poetry.get("packages") {
                if let Some(packages_array) =
                    packages_section.get("packages").and_then(|p| p.as_array())
                {
                    Some(packages_array.iter().any(|pkg| {
                        if let Some(table) = pkg.as_inline_table() {
                            (table.get("from").and_then(|f| f.as_str()) == Some("src"))
                                || (table.get("include").and_then(|i| i.as_str()).is_some()
                                    && table.get("from").and_then(|f| f.as_str()) == Some("src"))
                        } else {
                            false
                        }
                    }))
                } else {
                    // Just having a packages section is a strong indication it's a package
                    Some(true)
                }
            } else {
                None
            }
        })
        .unwrap_or(false);

    // OR: Check for Poetry packages section in any configuration
    let has_poetry_packages = has_poetry_package_config
        || doc
            .get("tool")
            .and_then(|t| t.get("poetry"))
            .and_then(|poetry| poetry.get("packages"))
            .is_some();

    // Also check for packages configuration in Poetry 2.0 format
    let has_poetry2_packages = doc
        .get("project")
        .and_then(|project| project.get("packages"))
        .is_some();

    if has_poetry_packages || has_poetry2_packages {
        debug!("Project has Poetry package configuration");
        return true;
    }

    // 2. Check for setup.py or setup.cfg which would indicate a package
    if project_dir.join("setup.py").exists() || project_dir.join("setup.cfg").exists() {
        debug!("Project has setup.py or setup.cfg");
        return true;
    }

    // 3. Check for typical package structure (src directory with an __init__.py file)
    let src_dir = project_dir.join("src");
    if src_dir.exists() && src_dir.is_dir() {
        // Check if there are any __init__.py files in the src directory
        if std::fs::read_dir(&src_dir).ok().is_some_and(|entries| {
            entries
                .flatten()
                .any(|entry| entry.path().is_dir() && entry.path().join("__init__.py").exists())
        }) {
            debug!("Project has src directory with __init__.py files");
            return true;
        }
    }

    // 4. Check for PEP 621 package indicators in [project] section
    let has_pep621_package_indicators = doc
        .get("project")
        .map(|project| {
            // Check for typical package indicators in PEP 621 format
            let has_urls = project.get("urls").is_some();
            let has_classifiers = project.get("classifiers").is_some();
            let has_keywords = project.get("keywords").is_some();

            // If it has multiple of these fields, it's likely a package
            (has_urls as u8) + (has_classifiers as u8) + (has_keywords as u8) >= 2
        })
        .unwrap_or(false);

    if has_pep621_package_indicators {
        debug!("Project has PEP 621 package indicators");
        return true;
    }

    // 5. Check for existing build-system in old pyproject.toml
    let has_build_system = doc.get("build-system").is_some();
    if has_build_system {
        debug!("Project already has a build-system section");
        return true;
    }

    debug!("No package indicators found, treating as application");
    false
}

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

    fn setup_test_environment(
        old_content: &str,
        new_content: &str,
        create_setup_py: bool,
        create_src_init: bool,
    ) -> (TempDir, DocumentMut, PathBuf) {
        let temp_dir = TempDir::new().unwrap();
        let project_dir = temp_dir.path().to_path_buf();

        fs::write(project_dir.join("old.pyproject.toml"), old_content).unwrap();
        fs::write(project_dir.join("pyproject.toml"), new_content).unwrap();

        if create_setup_py {
            fs::write(project_dir.join("setup.py"), "# Test setup.py").unwrap();
        }

        if create_src_init {
            let src_dir = project_dir.join("src");
            let pkg_dir = src_dir.join("test_pkg");
            fs::create_dir_all(&pkg_dir).unwrap();
            fs::write(pkg_dir.join("__init__.py"), "# Test init file").unwrap();
        }

        let doc = new_content.parse::<DocumentMut>().unwrap();
        (temp_dir, doc, project_dir)
    }

    #[test]
    fn test_poetry_to_hatchling_conversion_with_existing_build_system() {
        let old_content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, false, false);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(result);

        let build_system = doc.get("build-system").unwrap();
        let requires = build_system.get("requires").unwrap().as_array().unwrap();
        let first_req = requires.get(0).unwrap().as_str().unwrap();
        assert_eq!(first_req, "hatchling");

        let backend = build_system.get("build-backend").unwrap().as_str().unwrap();
        assert_eq!(backend, "hatchling.build");
    }

    #[test]
    fn test_poetry_to_hatchling_with_poetry_package_config() {
        let old_content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"

[tool.poetry.packages]
packages = [
     { from = "src" },
]
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, false, false);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(result);

        let build_system = doc.get("build-system").unwrap();
        let requires = build_system.get("requires").unwrap().as_array().unwrap();
        let first_req = requires.get(0).unwrap().as_str().unwrap();
        assert_eq!(first_req, "hatchling");
    }

    #[test]
    fn test_poetry_to_hatchling_with_setup_py() {
        let old_content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, true, false);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(result);

        let build_system = doc.get("build-system").unwrap();
        let backend = build_system.get("build-backend").unwrap().as_str().unwrap();
        assert_eq!(backend, "hatchling.build");
    }

    #[test]
    fn test_poetry_to_hatchling_with_src_init() {
        let old_content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, false, true);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(result);

        let build_system = doc.get("build-system").unwrap();
        let backend = build_system.get("build-backend").unwrap().as_str().unwrap();
        assert_eq!(backend, "hatchling.build");
    }

    #[test]
    fn test_poetry_to_hatchling_with_pep621_indicators() {
        let old_content = r#"
[project]
name = "test-project"
version = "0.1.0"
description = "A test project"
classifiers = ["Programming Language :: Python"]
keywords = ["test", "project"]
urls = { repository = "https://github.com/user/repo" }
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, false, false);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(result);

        let build_system = doc.get("build-system").unwrap();
        let backend = build_system.get("build-backend").unwrap().as_str().unwrap();
        assert_eq!(backend, "hatchling.build");
    }

    #[test]
    fn test_no_build_system_for_application() {
        let old_content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
description = "A simple application"
"#;

        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let (_temp_dir, mut doc, project_dir) =
            setup_test_environment(old_content, new_content, false, false);

        let result = update_build_system(&mut doc, &project_dir).unwrap();
        assert!(!result);
        assert!(doc.get("build-system").is_none());
    }

    #[test]
    fn test_no_old_pyproject() {
        let new_content = r#"
[project]
name = "test-project"
version = "0.1.0"
"#;

        let temp_dir = TempDir::new().unwrap();
        let mut doc = new_content.parse::<DocumentMut>().unwrap();

        let result = update_build_system(&mut doc, temp_dir.path()).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_determine_if_package_project() {
        // Test with package configuration
        let content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
packages = [
    { include = "src" }
]
"#;
        let doc = content.parse::<DocumentMut>().unwrap();
        let temp_dir = TempDir::new().unwrap();

        let result = determine_if_package_project(&doc, temp_dir.path());
        assert!(
            result,
            "Should detect package project from Poetry packages config"
        );

        // Test with Poetry 2.0 format
        let content2 = r#"
[project]
name = "test-project"
version = "0.1.0"
packages = [
    { include = "src" }
]
"#;
        let doc2 = content2.parse::<DocumentMut>().unwrap();
        let result2 = determine_if_package_project(&doc2, temp_dir.path());
        assert!(
            result2,
            "Should detect package project from Poetry 2.0 packages config"
        );
    }

    #[test]
    fn test_single_package_include() {
        // Test with simple include format
        let content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
packages = [
    { include = "src" }
]
"#;
        let doc = content.parse::<DocumentMut>().unwrap();
        let temp_dir = TempDir::new().unwrap();

        let result = determine_if_package_project(&doc, temp_dir.path());
        assert!(result, "Should detect package from single include format");
    }

    #[test]
    fn test_multiple_package_includes() {
        // Test with multiple includes
        let content = r#"
[tool.poetry]
name = "test-project"
version = "0.1.0"
packages = [
    { include = "src" },
    { include = "lib" }
]
"#;
        let doc = content.parse::<DocumentMut>().unwrap();
        let temp_dir = TempDir::new().unwrap();

        let result = determine_if_package_project(&doc, temp_dir.path());
        assert!(result, "Should detect package from multiple includes");
    }
}