|
8 | 8 |
|
9 | 9 | from usethis._config import usethis_config |
10 | 10 | from usethis._console import info_print, instruct_print, tick_print |
11 | | -from usethis._deps import get_project_deps |
12 | | -from usethis._init import ensure_dep_declaration_file |
| 11 | +from usethis._init import ensure_dep_declaration_file, write_simple_requirements_txt |
13 | 12 | from usethis._integrations.backend.dispatch import get_backend |
14 | 13 | from usethis._integrations.backend.uv.call import call_uv_subprocess |
| 14 | +from usethis._integrations.backend.uv.lockfile import ensure_uv_lock |
15 | 15 | from usethis._integrations.ci.bitbucket.used import is_bitbucket_used |
16 | 16 | from usethis._integrations.file.pyproject_toml.valid import ensure_pyproject_validity |
17 | 17 | from usethis._integrations.mkdocs.core import add_docs_dir |
@@ -367,53 +367,46 @@ def use_requirements_txt(*, remove: bool = False, how: bool = False) -> None: |
367 | 367 | path = usethis_config.cpd() / "requirements.txt" |
368 | 368 |
|
369 | 369 | if not remove: |
370 | | - ensure_dep_declaration_file() |
371 | | - |
372 | | - is_pre_commit = PreCommitTool().is_used() |
| 370 | + backend = get_backend() |
373 | 371 |
|
374 | | - if is_pre_commit: |
| 372 | + if PreCommitTool().is_used(): |
375 | 373 | tool.add_pre_commit_config() |
376 | 374 |
|
377 | | - if not path.exists(): |
378 | | - backend = get_backend() |
379 | | - if backend is BackendEnum.uv: |
380 | | - if ( |
381 | | - not (usethis_config.cpd() / "uv.lock").exists() |
382 | | - and not usethis_config.frozen |
383 | | - ): |
384 | | - tick_print("Writing 'uv.lock'.") |
385 | | - call_uv_subprocess(["lock"], change_toml=False) |
386 | | - |
387 | | - if not usethis_config.frozen: |
388 | | - tick_print("Writing 'requirements.txt'.") |
389 | | - call_uv_subprocess( |
390 | | - [ |
391 | | - "export", |
392 | | - "--frozen", |
393 | | - "--output-file=requirements.txt", |
394 | | - ], |
395 | | - change_toml=False, |
396 | | - ) |
397 | | - elif backend is BackendEnum.none: |
398 | | - # Simply dump the dependencies list to requirements.txt |
399 | | - if usethis_config.backend is BackendEnum.auto: |
400 | | - info_print( |
401 | | - "Generating 'requirements.txt' with un-pinned, abstract dependencies." |
402 | | - ) |
403 | | - info_print( |
404 | | - "Consider installing 'uv' for pinned, cross-platform, full requirements files." |
405 | | - ) |
| 375 | + if path.exists(): |
| 376 | + # requirements file already exists - short circuit; only need to explain how |
| 377 | + # how to re-generate it. |
| 378 | + tool.print_how_to_use() |
| 379 | + return |
| 380 | + |
| 381 | + if backend is BackendEnum.uv: |
| 382 | + if not (usethis_config.cpd() / "pyproject.toml").exists(): |
| 383 | + write_simple_requirements_txt() |
| 384 | + elif not usethis_config.frozen: |
| 385 | + ensure_uv_lock() |
406 | 386 | tick_print("Writing 'requirements.txt'.") |
407 | | - with open(path, "w", encoding="utf-8") as f: |
408 | | - f.write("-e .\n") |
409 | | - f.writelines( |
410 | | - dep.to_requirement_string() + "\n" for dep in get_project_deps() |
411 | | - ) |
412 | | - else: |
413 | | - assert_never(backend) |
| 387 | + call_uv_subprocess( |
| 388 | + [ |
| 389 | + "export", |
| 390 | + "--frozen", |
| 391 | + "--output-file=requirements.txt", |
| 392 | + ], |
| 393 | + change_toml=False, |
| 394 | + ) |
| 395 | + elif backend is BackendEnum.none: |
| 396 | + # Simply dump the dependencies list to requirements.txt |
| 397 | + if usethis_config.backend is BackendEnum.auto: |
| 398 | + info_print( |
| 399 | + "Generating 'requirements.txt' with un-pinned, abstract dependencies." |
| 400 | + ) |
| 401 | + info_print( |
| 402 | + "Consider installing 'uv' for pinned, cross-platform, full requirements files." |
| 403 | + ) |
| 404 | + |
| 405 | + write_simple_requirements_txt() |
| 406 | + else: |
| 407 | + assert_never(backend) |
414 | 408 |
|
415 | 409 | tool.print_how_to_use() |
416 | | - |
417 | 410 | else: |
418 | 411 | tool.remove_pre_commit_repo_configs() |
419 | 412 | tool.remove_managed_files() |
|
0 commit comments