-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
746c4c8
commit cdb1c2b
Showing
2 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import toml | ||
|
||
if TYPE_CHECKING: | ||
from os import PathLike | ||
|
||
__all__ = [] | ||
|
||
|
||
def combine_dev_dependencies( | ||
pyproject: str | PathLike[str], destination: str | PathLike[str] | ||
) -> tuple[str, Path]: | ||
pyproject = Path(pyproject) | ||
destination = Path(destination) | ||
|
||
pyproject_obj = read_pyproject(pyproject) | ||
key, new_pyproject = dev_dependencies_to_dependencies(pyproject_obj) | ||
write_pyproject(new_pyproject, destination) | ||
|
||
return key, destination | ||
|
||
|
||
def read_pyproject(pyproject: str | PathLike[str]) -> dict[str, Any]: | ||
pyproject = Path(pyproject) | ||
with pyproject.open() as f: | ||
return toml.load(f) | ||
|
||
|
||
def write_pyproject( | ||
pyproject: dict[str, Any], pyproject_path: str | PathLike[str] | ||
) -> Path: | ||
pyproject_path = Path(pyproject_path) | ||
with pyproject_path.open("w") as f: | ||
toml.dump(pyproject, f) | ||
return pyproject_path | ||
|
||
|
||
def dev_dependencies_to_dependencies( | ||
pyproject: str | PathLike[str] | dict[str, Any], | ||
) -> tuple[str, dict[str, Any]]: | ||
if not isinstance(pyproject, dict): | ||
pyproject = read_pyproject(pyproject) | ||
|
||
key = "dev_dependencies" | ||
project: dict[str, Any] = pyproject["project"] | ||
|
||
optional_dependencies: dict[str, list[str]] = project.setdefault( | ||
"optional-dependencies", {} | ||
) | ||
if key in optional_dependencies: | ||
key = f"new_{key}" | ||
|
||
uv_config: dict[str, Any] = pyproject.setdefault("uv", {}) | ||
dev_dependencies: list[str] = uv_config.setdefault("dev-dependencies", []) | ||
|
||
optional_dependencies[key] = dev_dependencies | ||
pyproject["project"]["optional-dependencies"] = optional_dependencies | ||
|
||
return key, pyproject |