Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sources and extras handling on packages and dependencies #78

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 170 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,147 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry

return Poetry(poetry_file, local_config, package)

@classmethod
def create_dependency(
sdispater marked this conversation as resolved.
Show resolved Hide resolved
cls,
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", False),
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", False),
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"]

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

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)
abn marked this conversation as resolved.
Show resolved Hide resolved

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

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

return dependency

@classmethod
def validate(
cls, config, strict=False
Expand Down
32 changes: 18 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,11 @@


def _make_file_or_dir_dep(
name, path, base=None
): # type: (str, Path, Optional[Path]) -> Optional[Union[FileDependency, DirectoryDependency]]
name, # type: str
path, # type: Path
base=None, # type: Optional[Path]
extras=None, # type: Optional[List[str]]
): # type: (...) -> 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 +45,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 +124,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 +157,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 +221,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