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

Fix Pex emitting warnings about its Pip PEX venv. #1351

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion pex/third_party/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ def isolated():

module = "pex"

# These files are only used for running `tox -evendor` and should not pollute either the
# PEX_ROOT or built PEXs.
vendor_lockfiles = tuple(
os.path.join(os.path.relpath(vendor_spec.relpath, module), "constraints.txt")
for vendor_spec in vendor.iter_vendor_specs()
)

# TODO(John Sirois): Unify with `pex.util.DistributionHelper.access_zipped_assets`.
def recursive_copy(srcdir, dstdir):
os.mkdir(dstdir)
Expand All @@ -370,7 +377,11 @@ def recursive_copy(srcdir, dstdir):
if os.path.basename(src_entry) == "__pycache__":
continue
recursive_copy(src_entry, dst_entry)
elif not entry_name.endswith(".pyc") and not is_pyc_temporary_file(entry_name):
elif (
not entry_name.endswith(".pyc")
and not is_pyc_temporary_file(entry_name)
and src_entry not in vendor_lockfiles
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both new tests fail with this line commented out.

):
with open(dst_entry, "wb") as fp:
with closing(resource_stream(module, src_entry)) as resource:
shutil.copyfileobj(resource, fp)
Expand Down
6 changes: 5 additions & 1 deletion pex/vendor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from pex.common import filter_pyc_dirs, filter_pyc_files, touch
from pex.compatibility import urlparse
from pex.tracer import TRACER
from pex.typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Iterator

_PACKAGE_COMPONENTS = __name__.split(".")

Expand Down Expand Up @@ -100,10 +104,10 @@ def create_packages(self):


def iter_vendor_specs():
# type: () -> Iterator[VendorSpec]
"""Iterate specifications for code vendored by pex.

:return: An iterator over specs of all vendored code.
:rtype: :class:`collection.Iterator` of :class:`VendorSpec`
"""
# We use this for a better @dataclass that is also Python2.7 and PyPy compatible.
# N.B.: The `[testenv:typecheck]` section in `tox.ini` should have its deps list updated to
Expand Down
35 changes: 35 additions & 0 deletions tests/test_pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import

import os
import warnings

from pex.interpreter import PythonInterpreter
from pex.pip import Pip
from pex.typing import TYPE_CHECKING
from pex.variables import ENV

if TYPE_CHECKING:
from typing import Any


def test_no_duplicate_constraints_pex_warnings(tmpdir):
# type: (Any) -> None
pex_root = os.path.join(str(tmpdir), "pex_root")
pip_root = os.path.join(str(tmpdir), "pip_root")
interpreter = PythonInterpreter.get()
platform = interpreter.platform

with ENV.patch(PEX_ROOT=pex_root), warnings.catch_warnings(record=True) as events:
pip = Pip.create(path=pip_root, interpreter=interpreter)

pip.spawn_debug(
platform=platform.platform, impl=platform.impl, version=platform.version, abi=platform.abi
).wait()

assert 0 == len([event for event in events if "constraints.txt" in str(event)]), (
"Expected no duplicate constraints warnings to be emitted when creating a Pip venv but "
"found\n{}".format("\n".join(map(str, events)))
)
12 changes: 12 additions & 0 deletions tests/test_third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def test_isolated_pex_root():
assert pex_root == os.path.commonprefix([pex_root, devendored_chroot])


def test_isolated_vendoring_constraints_omitted():
# type: () -> None
with temporary_pex_root() as (pex_root, _):
devendored_chroot = os.path.realpath(third_party.isolated().chroot_path)
assert [] == [
os.path.join(root, file)
for root, _, files in os.walk(devendored_chroot)
for file in files
if file == "constraints.txt"
]


def test_isolated_idempotent_inprocess():
# type: () -> None
with temporary_pex_root():
Expand Down