Skip to content

Commit

Permalink
locker: move export logic into locker for reuse
Browse files Browse the repository at this point in the history
This change, moves common functionality from exporter into the locker
to allow for sharing of logic that can generate `DependencyPackage`
instances given a a list of requirements using the lock data.
  • Loading branch information
abn committed Oct 14, 2020
1 parent 50e0628 commit c2adb32
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
44 changes: 42 additions & 2 deletions poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

from copy import deepcopy
from hashlib import sha256
from typing import Any
from typing import Iterator
from typing import List
from typing import Optional
from typing import Sequence
from typing import Union

from tomlkit import array
from tomlkit import document
Expand All @@ -25,8 +27,10 @@
from poetry.core.semver.version import Version
from poetry.core.toml.file import TOMLFile
from poetry.core.version.markers import parse_marker
from poetry.packages import DependencyPackage
from poetry.utils._compat import OrderedDict
from poetry.utils._compat import Path
from poetry.utils.extras import get_extra_package_names


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -183,7 +187,7 @@ def locked_repository(

def get_project_dependencies(
self, project_requires, pinned_versions=False, with_nested=False, with_dev=False
): # type: (List[Dependency], bool, bool, bool) -> Any
): # type: (List[Dependency], bool, bool, bool) -> List[Dependency]
packages = self.locked_repository(with_dev).packages

# group packages entries by name, this is required because requirement might use
Expand Down Expand Up @@ -263,6 +267,42 @@ def __get_locked_package(
key=lambda x: x.name.lower(),
)

def get_project_dependency_packages(
self, project_requires, dev=False, extras=None
): # type: (List[Dependency], bool, Optional[Union[bool, Sequence[str]]]) -> Iterator[DependencyPackage]
repository = self.locked_repository(with_dev_reqs=dev)

# Build a set of all packages required by our selected extras
extra_package_names = (
None if (isinstance(extras, bool) and extras is True) else ()
)

if extra_package_names is not None:
extra_package_names = set(
get_extra_package_names(
repository.packages, self.lock_data.get("extras", {}), extras or (),
)
)

for dependency in self.get_project_dependencies(
project_requires=project_requires, with_nested=True, with_dev=dev,
):
try:
package = repository.find_packages(dependency=dependency)[0]
except IndexError:
continue

# If a package is optional and we haven't opted in to it, continue
if extra_package_names is not None and (
package.optional and package.name not in extra_package_names
):
continue

for extra in dependency.extras:
package.requires_extras.append(extra)

yield DependencyPackage(dependency=dependency, package=package)

def set_lock_data(self, root, packages): # type: (...) -> bool
files = table()
packages = self._lock_packages(packages)
Expand Down
34 changes: 8 additions & 26 deletions poetry/utils/exporter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Optional
from typing import Sequence
from typing import Union

from clikit.api.io import IO

from poetry.poetry import Poetry
from poetry.utils._compat import Path
from poetry.utils._compat import decode
from poetry.utils.extras import get_extra_package_names


class Exporter(object):
Expand Down Expand Up @@ -51,38 +52,19 @@ def _export_requirements_txt(
dev=False,
extras=None,
with_credentials=False,
): # type: (Path, Union[IO, str], bool, bool, bool) -> None
): # type: (Path, Union[IO, str], bool, bool, Optional[Union[bool, Sequence[str]]], bool) -> None
indexes = set()
content = ""
repository = self._poetry.locker.locked_repository(dev)

# Build a set of all packages required by our selected extras
extra_package_names = set(
get_extra_package_names(
repository.packages,
self._poetry.locker.lock_data.get("extras", {}),
extras or (),
)
)

dependency_lines = set()

for dependency in self._poetry.locker.get_project_dependencies(
project_requires=self._poetry.package.all_requires,
with_nested=True,
with_dev=dev,
for dependency_package in self._poetry.locker.get_project_dependency_packages(
project_requires=self._poetry.package.all_requires, dev=dev, extras=extras
):
try:
package = repository.find_packages(dependency=dependency)[0]
except IndexError:
continue

# If a package is optional and we haven't opted in to it, continue
if package.optional and package.name not in extra_package_names:
continue

line = ""

dependency = dependency_package.dependency
package = dependency_package.package

if package.develop:
line += "-e "

Expand Down

0 comments on commit c2adb32

Please sign in to comment.