Commit bb305300 by Arun Babu Neelicattu Committed by Bjorn Neergaard

factory: reuse dependency specification utils

parent 58db4247
...@@ -9,7 +9,6 @@ from typing import cast ...@@ -9,7 +9,6 @@ from typing import cast
from cleo.io.null_io import NullIO from cleo.io.null_io import NullIO
from poetry.core.factory import Factory as BaseFactory from poetry.core.factory import Factory as BaseFactory
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.toml.file import TOMLFile from poetry.core.toml.file import TOMLFile
from tomlkit.toml_document import TOMLDocument from tomlkit.toml_document import TOMLDocument
...@@ -21,6 +20,7 @@ from poetry.packages.project_package import ProjectPackage ...@@ -21,6 +20,7 @@ from poetry.packages.project_package import ProjectPackage
from poetry.plugins.plugin import Plugin from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry from poetry.poetry import Poetry
from poetry.utils.dependency_specification import dependency_to_specification
try: try:
...@@ -36,6 +36,7 @@ if TYPE_CHECKING: ...@@ -36,6 +36,7 @@ if TYPE_CHECKING:
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.repositories.legacy_repository import LegacyRepository from poetry.repositories.legacy_repository import LegacyRepository
from poetry.utils.dependency_specification import DependencySpec
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -277,29 +278,18 @@ class Factory(BaseFactory): ...@@ -277,29 +278,18 @@ class Factory(BaseFactory):
dependency_section["python"] = package.python_versions dependency_section["python"] = package.python_versions
for dep in package.all_requires: for dep in package.all_requires:
constraint: dict[str, Any] = tomlkit.inline_table() constraint: DependencySpec | str = dependency_to_specification(
if dep.is_vcs(): dep, tomlkit.inline_table()
dep = cast(VCSDependency, dep) )
constraint[dep.vcs] = dep.source_url
if dep.reference:
constraint["rev"] = dep.reference
elif dep.is_file() or dep.is_directory():
constraint["path"] = dep.source_url
else:
constraint["version"] = dep.pretty_constraint
if not dep.marker.is_any():
constraint["markers"] = str(dep.marker)
if dep.extras:
constraint["extras"] = sorted(dep.extras)
if not isinstance(constraint, str):
if dep.name in optional_dependencies: if dep.name in optional_dependencies:
constraint["optional"] = True constraint["optional"] = True
if len(constraint) == 1 and "version" in constraint: if len(constraint) == 1 and "version" in constraint:
constraint = constraint["version"] constraint = cast(str, constraint["version"])
elif not constraint:
constraint = "*"
for group in dep.groups: for group in dep.groups:
if group == MAIN_GROUP: if group == MAIN_GROUP:
......
...@@ -9,11 +9,13 @@ from pathlib import Path ...@@ -9,11 +9,13 @@ from pathlib import Path
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import TypeVar
from typing import Union from typing import Union
from typing import cast from typing import cast
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.vcs_dependency import VCSDependency from poetry.core.packages.vcs_dependency import VCSDependency
from tomlkit.items import InlineTable
from poetry.puzzle.provider import Provider from poetry.puzzle.provider import Provider
...@@ -22,7 +24,7 @@ if TYPE_CHECKING: ...@@ -22,7 +24,7 @@ if TYPE_CHECKING:
from poetry.utils.env import Env from poetry.utils.env import Env
DependencySpec = Dict[str, Union[str, Dict[str, Union[str, bool]], List[str]]] DependencySpec = Dict[str, Union[str, bool, Dict[str, Union[str, bool]], List[str]]]
def _parse_dependency_specification_git_url( def _parse_dependency_specification_git_url(
...@@ -136,9 +138,12 @@ def _parse_dependency_specification_simple( ...@@ -136,9 +138,12 @@ def _parse_dependency_specification_simple(
return require return require
def dependency_to_specification(dependency: Dependency) -> DependencySpec: BaseSpec = TypeVar("BaseSpec", DependencySpec, InlineTable)
specification: DependencySpec = {}
def dependency_to_specification(
dependency: Dependency, specification: BaseSpec
) -> BaseSpec:
if dependency.is_vcs(): if dependency.is_vcs():
dependency = cast(VCSDependency, dependency) dependency = cast(VCSDependency, dependency)
specification[dependency.vcs] = cast(str, dependency.source_url) specification[dependency.vcs] = cast(str, dependency.source_url)
...@@ -167,7 +172,8 @@ def pep508_to_dependency_specification(requirement: str) -> DependencySpec | Non ...@@ -167,7 +172,8 @@ def pep508_to_dependency_specification(requirement: str) -> DependencySpec | Non
with contextlib.suppress(ValueError): with contextlib.suppress(ValueError):
dependency = Dependency.create_from_pep_508(requirement) dependency = Dependency.create_from_pep_508(requirement)
specification = dependency_to_specification(dependency) specification: DependencySpec = {}
specification = dependency_to_specification(dependency, specification)
if specification: if specification:
specification["name"] = dependency.name specification["name"] = dependency.name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment