Skip to content

Commit

Permalink
Refactor sources and extras handling on packages and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Sep 5, 2020
1 parent a522953 commit 8baa9e1
Show file tree
Hide file tree
Showing 17 changed files with 872 additions and 294 deletions.
181 changes: 177 additions & 4 deletions poetry/core/factory.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import logging

from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
from warnings import warn

from .json import validate_object
from .packages.dependency import Dependency
Expand All @@ -14,6 +19,9 @@
from .utils._compat import Path


logger = logging.getLogger(__name__)


class Factory(object):
"""
Factory class to create various elements needed by Poetry.
Expand Down Expand Up @@ -71,21 +79,38 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry

if isinstance(constraint, list):
for _constraint in constraint:
package.add_dependency(name, _constraint)
package.add_dependency(
self.create_dependency(
name, _constraint, root_dir=package.root_dir
)
)

continue

package.add_dependency(name, constraint)
package.add_dependency(
self.create_dependency(name, constraint, root_dir=package.root_dir)
)

if "dev-dependencies" in local_config:
for name, constraint in local_config["dev-dependencies"].items():
if isinstance(constraint, list):
for _constraint in constraint:
package.add_dependency(name, _constraint, category="dev")
package.add_dependency(
self.create_dependency(
name,
_constraint,
category="dev",
root_dir=package.root_dir,
)
)

continue

package.add_dependency(name, constraint, category="dev")
package.add_dependency(
self.create_dependency(
name, constraint, category="dev", root_dir=package.root_dir
)
)

extras = local_config.get("extras", {})
for extra_name, requirements in extras.items():
Expand Down Expand Up @@ -134,6 +159,154 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry

return Poetry(poetry_file, local_config, package)

def create_dependency(
self,
name, # type: str
constraint, # type: Union[str, Dict[str, Any]]
category="main", # type: str
root_dir=None, # type: Optional[Path]
): # type: (...) -> Dependency
from .packages.constraints import parse_constraint as parse_generic_constraint
from .packages.directory_dependency import DirectoryDependency
from .packages.file_dependency import FileDependency
from .packages.url_dependency import URLDependency
from .packages.utils.utils import create_nested_marker
from .packages.vcs_dependency import VCSDependency
from .version.markers import AnyMarker
from .version.markers import parse_marker

if constraint is None:
constraint = "*"

if isinstance(constraint, dict):
optional = constraint.get("optional", False)
python_versions = constraint.get("python")
platform = constraint.get("platform")
markers = constraint.get("markers")
if "allows-prereleases" in constraint:
message = (
'The "{}" dependency specifies '
'the "allows-prereleases" property, which is deprecated. '
'Use "allow-prereleases" instead.'.format(name)
)
warn(message, DeprecationWarning)
logger.warning(message)

allows_prereleases = constraint.get(
"allow-prereleases", constraint.get("allows-prereleases", False)
)

if "git" in constraint:
# VCS dependency
dependency = VCSDependency(
name,
"git",
constraint["git"],
branch=constraint.get("branch", None),
tag=constraint.get("tag", None),
rev=constraint.get("rev", None),
category=category,
optional=optional,
develop=constraint.get("develop", True),
extras=constraint.get("extras", []),
)
elif "file" in constraint:
file_path = Path(constraint["file"])

dependency = FileDependency(
name,
file_path,
category=category,
base=root_dir,
extras=constraint.get("extras", []),
)
elif "path" in constraint:
path = Path(constraint["path"])

if root_dir:
is_file = root_dir.joinpath(path).is_file()
else:
is_file = path.is_file()

if is_file:
dependency = FileDependency(
name,
path,
category=category,
optional=optional,
base=root_dir,
extras=constraint.get("extras", []),
)
else:
dependency = DirectoryDependency(
name,
path,
category=category,
optional=optional,
base=root_dir,
develop=constraint.get("develop", True),
extras=constraint.get("extras", []),
)
elif "url" in constraint:
dependency = URLDependency(
name,
constraint["url"],
category=category,
optional=optional,
extras=constraint.get("extras", []),
)
else:
version = constraint["version"]

source_info = {}
if "source" in constraint:
source_info = self.get_source_information(constraint["source"])

dependency = Dependency(
name,
version,
optional=optional,
category=category,
allows_prereleases=allows_prereleases,
extras=constraint.get("extras", []),
**source_info
)

if not markers:
marker = AnyMarker()
if python_versions:
dependency.python_versions = python_versions
marker = marker.intersect(
parse_marker(
create_nested_marker(
"python_version", dependency.python_constraint
)
)
)

if platform:
marker = marker.intersect(
parse_marker(
create_nested_marker(
"sys_platform", parse_generic_constraint(platform)
)
)
)
else:
marker = parse_marker(markers)

if not marker.is_any():
dependency.marker = marker

dependency.source_name = constraint.get("source")
else:
dependency = Dependency(name, constraint, category=category)

return dependency

def get_source_information(self, source_name): # type: (str) -> Dict[str, str]
return {}

@classmethod
def validate(
cls, config, strict=False
Expand Down
29 changes: 15 additions & 14 deletions poetry/core/packages/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re

from typing import List
from typing import Optional
from typing import Union

Expand Down Expand Up @@ -29,8 +30,8 @@


def _make_file_or_dir_dep(
name, path, base=None
): # type: (str, Path, Optional[Path]) -> Optional[Union[FileDependency, DirectoryDependency]]
name, path, base=None, extras=None
): # type: (str, Path, Optional[Path], Optional[List[str]]) -> Optional[Union[FileDependency, DirectoryDependency]]
"""
Helper function to create a file or directoru dependency with the given arguments. If
path is not a file or directory that exists, `None` is returned.
Expand All @@ -41,9 +42,9 @@ def _make_file_or_dir_dep(
_path = Path(base) / path

if _path.is_file():
return FileDependency(name, path, base=base)
return FileDependency(name, path, base=base, extras=extras)
elif _path.is_dir():
return DirectoryDependency(name, path, base=base)
return DirectoryDependency(name, path, base=base, extras=extras)

return None

Expand Down Expand Up @@ -120,26 +121,30 @@ def dependency_from_pep_508(

if link.scheme.startswith("git+"):
url = ParsedUrl.parse(link.url)
dep = VCSDependency(name, "git", url.url, rev=url.rev)
dep = VCSDependency(name, "git", url.url, rev=url.rev, extras=req.extras)
elif link.scheme == "git":
dep = VCSDependency(name, "git", link.url_without_fragment)
dep = VCSDependency(
name, "git", link.url_without_fragment, extras=req.extras
)
elif link.scheme in ["http", "https"]:
dep = URLDependency(name, link.url)
elif is_file_uri:
# handle RFC 8089 references
path = url_to_path(req.url)
dep = _make_file_or_dir_dep(name=name, path=path, base=relative_to)
dep = _make_file_or_dir_dep(
name=name, path=path, base=relative_to, extras=req.extras
)
else:
try:
# this is a local path not using the file URI scheme
dep = _make_file_or_dir_dep(
name=name, path=Path(req.url), base=relative_to
name=name, path=Path(req.url), base=relative_to, extras=req.extras,
)
except ValueError:
pass

if dep is None:
dep = Dependency(name, version or "*")
dep = Dependency(name, version or "*", extras=req.extras)

if version:
dep._constraint = parse_constraint(version)
Expand All @@ -149,7 +154,7 @@ def dependency_from_pep_508(
else:
constraint = "*"

dep = Dependency(name, constraint)
dep = Dependency(name, constraint, extras=req.extras)

if "extra" in markers:
# If we have extras, the dependency is optional
Expand Down Expand Up @@ -213,8 +218,4 @@ def dependency_from_pep_508(
if req.marker:
dep.marker = req.marker

# Extras
for extra in req.extras:
dep.extras.append(extra)

return dep
Loading

0 comments on commit 8baa9e1

Please sign in to comment.