|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | +import tomlkit |
| 4 | +from tomlkit.toml_document import TOMLDocument |
| 5 | + |
| 6 | +LOGGER = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +def convert_dependencies(poetry_deps: dict) -> list[str]: |
| 10 | + """ |
| 11 | + Converts Poetry-style dependencies to PEP 621-style dependencies. |
| 12 | +
|
| 13 | + Args: |
| 14 | + poetry_deps (dict): Dependencies in Poetry format. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + list[str]: Dependencies in PEP 621 format. |
| 18 | + """ |
| 19 | + pep621_deps = [] |
| 20 | + for package, version_spec in poetry_deps.items(): |
| 21 | + if isinstance(version_spec, dict): |
| 22 | + extras = version_spec.get("extras", []) |
| 23 | + markers = version_spec.get("markers", "") |
| 24 | + version = version_spec.get("version", "") |
| 25 | + |
| 26 | + dep = package |
| 27 | + if extras: |
| 28 | + dep += f"[{','.join(extras)}]" |
| 29 | + if version: |
| 30 | + dep += f" {version}" |
| 31 | + if markers: |
| 32 | + dep += f"; {markers}" |
| 33 | + pep621_deps.append(dep) |
| 34 | + else: |
| 35 | + if version_spec == "*": |
| 36 | + version_spec = "" |
| 37 | + else: |
| 38 | + version_spec = f" {version_spec}" |
| 39 | + pep621_deps.append(f"{package}{version_spec}") |
| 40 | + return pep621_deps |
| 41 | + |
| 42 | + |
| 43 | +def extract_dependencies(input_path: Path, output_path: Path) -> None: |
| 44 | + """ |
| 45 | + Extracts dependencies from a pyproject.toml file and writes them to a new file in PEP 621 format. |
| 46 | +
|
| 47 | + Args: |
| 48 | + input_path (Path): The path to the input pyproject.toml file. |
| 49 | + output_path (Path): The path to the output pyproject_621.toml file. |
| 50 | + """ |
| 51 | + LOGGER.info("Reading pyproject.toml from %s", input_path) |
| 52 | + with input_path.open("r", encoding="utf-8") as file: |
| 53 | + pyproject_data = tomlkit.parse(file.read()) |
| 54 | + |
| 55 | + LOGGER.info("Extracting dependencies") |
| 56 | + dependencies_section = TOMLDocument() |
| 57 | + dependencies_section["project"] = {"dependencies": [], "optional-dependencies": {}} |
| 58 | + |
| 59 | + # Extract the dependencies sections if they exist |
| 60 | + if "tool" in pyproject_data and "poetry" in pyproject_data["tool"]: |
| 61 | + poetry_section = pyproject_data["tool"]["poetry"] |
| 62 | + if "dependencies" in poetry_section: |
| 63 | + dependencies_section["project"]["dependencies"] = convert_dependencies(poetry_section["dependencies"]) |
| 64 | + if "dev-dependencies" in poetry_section: |
| 65 | + dependencies_section["project"]["optional-dependencies"]["dev"] = convert_dependencies( |
| 66 | + poetry_section["dev-dependencies"]) |
| 67 | + |
| 68 | + LOGGER.info("Writing extracted dependencies to %s", output_path) |
| 69 | + with output_path.open("w", encoding="utf-8") as file: |
| 70 | + file.write(tomlkit.dumps(dependencies_section)) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + logging.basicConfig(level=logging.INFO) |
| 75 | + input_path = Path("pyproject.toml") |
| 76 | + output_path = Path("pyproject_621.toml") |
| 77 | + extract_dependencies(input_path, output_path) |
0 commit comments