Skip to content

Commit

Permalink
Fix exploding zip-unsafe pexes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterebden committed Nov 19, 2024
1 parent ea37e5f commit f5833f7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
8 changes: 8 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ python_test(
"//third_party/python:six",
],
)

python_test(
name = "zip_unsafe_test",
srcs = ["zip_unsafe_test.py"],
deps = [
"//third_party/python:confluent-kafka",
],
)
9 changes: 9 additions & 0 deletions test/zip_unsafe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import unittest


class ZipUnsafeTest(unittest.TestCase):
"""Test importing something that requires zip-unsafety and exploding the pex."""

def test_can_import(self):
from third_party.python.confluent_kafka import cimpl
self.assertIsNotNone(cimpl)
8 changes: 8 additions & 0 deletions third_party/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,11 @@ python_wheel(
version = "0.4.0",
deps = [],
)

pip_library(
name = "confluent-kafka",
licences = ["Apache-2.0"],
version = "2.6.1",
test_only = True,
zip_safe = False,
)
11 changes: 4 additions & 7 deletions tools/please_pex/pex/pex_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@

def add_module_dir_to_sys_path(dirname, zip_safe=True):
"""Adds the given dirname to sys.path if it's nonempty."""
# Add .bootstrap dir to path, after the initial pex entry
sys.path = sys.path[:1] + [os.path.join(sys.path[0], '.bootstrap')] + sys.path[1:]
# Now we have .bootstrap on the path, we can import our own hooks.
import plz
import plz # this needs to be imported after paths are set up
if dirname:
sys.path = sys.path[:1] + [os.path.join(sys.path[0], dirname)] + sys.path[1:]
sys.path.insert(1, os.path.join(sys.path[0], dirname))
sys.meta_path.insert(0, plz.ModuleDirImport(dirname))
if zip_safe:
sys.meta_path.append(plz.SoImport(MODULE_DIR))
Expand Down Expand Up @@ -82,10 +79,10 @@ def _explode_zip():
os.makedirs(basepath, exist_ok=True)
with pex_lockfile(basepath, uniquedir) as lockfile:
if len(lockfile.read()) == 0:
import compileall, zipfile
import compileall, zipfile, plz

os.makedirs(PEX_PATH, exist_ok=True)
with ZipFileWithPermissions(PEX, "r") as zf:
with plz.ZipFileWithPermissions(PEX, "r") as zf:
zf.extractall(PEX_PATH)

if not no_cache: # Don't bother optimizing; we're deleting this when we're done.
Expand Down
2 changes: 2 additions & 0 deletions tools/please_pex/pex/pex_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def run(explode=False):
# Add .bootstrap dir to path, after the initial pex entry
sys.path.insert(1, os.path.join(sys.path[0], '.bootstrap'))
if explode or not ZIP_SAFE:
with explode_zip()():
add_module_dir_to_sys_path(MODULE_DIR, zip_safe=False)
Expand Down

0 comments on commit f5833f7

Please sign in to comment.