Skip to content

Commit

Permalink
Fix --requirements-pex.
Browse files Browse the repository at this point in the history
The switch from sha1 to sha256 hashes in pex-tool#1661 did not cover this
feature. Simplify and use the existing machinery to get at a PEX's
distributions.

Fixes pex-tool#1683
  • Loading branch information
jsirois committed Mar 22, 2022
1 parent f69a1c8 commit 47fad6a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
30 changes: 5 additions & 25 deletions pex/pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
atomic_directory,
chmod_plus_x,
is_pyc_temporary_file,
open_zip,
safe_copy,
safe_mkdir,
safe_mkdtemp,
Expand All @@ -25,6 +24,7 @@
from pex.compatibility import to_bytes
from pex.compiler import Compiler
from pex.enum import Enum
from pex.environment import PEXEnvironment
from pex.finders import get_entry_point_from_console_script, get_script_from_distributions
from pex.interpreter import PythonInterpreter
from pex.layout import Layout
Expand Down Expand Up @@ -297,30 +297,10 @@ def add_from_requirements_pex(self, pex):
:param pex: The path to an existing .pex file or unzipped pex directory.
"""
self._ensure_unfrozen("Adding from pex")
pex_info = PexInfo.from_pex(pex)

def add(location, dname, expected_dhash):
dhash = self._add_dist_dir(location, dname)
if dhash != expected_dhash:
raise self.InvalidDistribution(
"Distribution {} at {} had hash {}, expected {}".format(
dname, location, dhash, expected_dhash
)
)
self._pex_info.add_distribution(dname, dhash)

if os.path.isfile(pex):
with open_zip(pex) as zf:
for dist_name, dist_hash in pex_info.distributions.items():
internal_dist_path = "/".join([pex_info.internal_cache, dist_name])
cached_location = os.path.join(pex_info.install_cache, dist_hash, dist_name)
CacheHelper.cache_distribution(zf, internal_dist_path, cached_location)
add(cached_location, dist_name, dist_hash)
else:
for dist_name, dist_hash in pex_info.distributions.items():
add(os.path.join(pex, pex_info.internal_cache, dist_name), dist_name, dist_hash)
for req in pex_info.requirements:
self._pex_info.add_requirement(req)
pex_environment = PEXEnvironment.mount(pex)
for dist in pex_environment.resolve():
self.add_distribution(dist)
self.add_requirement(dist.as_requirement())

def set_executable(self, filename, env_filename=None):
"""Set the executable for this environment.
Expand Down
66 changes: 66 additions & 0 deletions tests/integration/test_issue_1683.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os.path
import subprocess
from textwrap import dedent

from colors import crossed, red

from pex.common import safe_open
from pex.testing import run_pex_command
from pex.typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any


def test_requirements_pex(tmpdir):
# type: (Any) -> None

pex_root = os.path.join(str(tmpdir), "pex_root")
requirements_pex = os.path.join(str(tmpdir), "requirements.pex")
run_pex_command(
args=[
"--pex-root",
pex_root,
"--runtime-pex-root",
pex_root,
"ansicolors==1.1.8",
"-o",
requirements_pex,
]
).assert_success()

src_dir = os.path.join(str(tmpdir), "src")
with safe_open(os.path.join(src_dir, "exe.py"), "w") as fp:
fp.write(
dedent(
"""\
from colors import crossed, red
print(red(crossed("Broken")))
"""
)
)

app_pex = os.path.join(str(tmpdir), "app.pex")
run_pex_command(
args=[
"--pex-root",
pex_root,
"--runtime-pex-root",
pex_root,
"--requirements-pex",
requirements_pex,
"-D",
src_dir,
"-m",
"exe",
"-o",
app_pex,
]
).assert_success()

assert red(crossed("Broken")) == subprocess.check_output(args=[app_pex]).decode("utf-8").strip()

0 comments on commit 47fad6a

Please sign in to comment.