Skip to content

Commit bc0edf4

Browse files
committed
Fix pex-tool#139: PEX_SCRIPT fails for scripts from not-zip-safe eggs.
h/t @palexander for doing the heavy lifting to figure out what was going on here.
1 parent 49aec66 commit bc0edf4

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ CHANGES
1111
the `PythonInterpreter` class.
1212
Fixes `#144 <https://github.com/pantsbuild/pex/issues/144>`_.
1313

14+
* Bug fix: PEX_SCRIPT failed when the script was from a not-zip-safe egg.
15+
Original PR `#139 <https://github.com/pantsbuild/pex/pull/139>`_.
16+
17+
* Bug fix: `sys.exit` called without arguments would cause `None` to be printed on stderr since pex 1.0.1.
18+
`#143 <https://github.com/pantsbuild/pex/pull/143>`_.
19+
1420
-----
1521
1.0.2
1622
-----

pex/finders.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,19 @@ def get_script_from_whl(name, dist):
262262

263263

264264
def get_script_from_distribution(name, dist):
265-
if isinstance(dist._provider, FixedEggMetadata):
265+
# PathMetadata: exploded distribution on disk.
266+
if isinstance(dist._provider, pkg_resources.PathMetadata):
267+
if dist.egg_info.endswith('EGG-INFO'):
268+
return get_script_from_egg(name, dist)
269+
elif dist.egg_info.endswith('.dist-info'):
270+
return get_script_from_whl(name, dist)
271+
else:
272+
return None, None
273+
# FixedEggMetadata: Zipped egg
274+
elif isinstance(dist._provider, FixedEggMetadata):
266275
return get_script_from_egg(name, dist)
267-
elif isinstance(dist._provider, (WheelMetadata, pkg_resources.PathMetadata)):
276+
# WheelMetadata: Zipped whl (in theory should not experience this at runtime.)
277+
elif isinstance(dist._provider, WheelMetadata):
268278
return get_script_from_whl(name, dist)
269279
return None, None
270280

pex/pex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def execute_content(cls, name, content, argv0=None):
392392
try:
393393
ast = compile(content, name, 'exec', flags=0, dont_inherit=1)
394394
except SyntaxError:
395-
die('Unable to parse %s. PEX script support only supports Python scripts.')
395+
die('Unable to parse %s. PEX script support only supports Python scripts.' % name)
396396
old_name, old_file = globals().get('__name__'), globals().get('__file__')
397397
try:
398398
old_argv0, sys.argv[0] = sys.argv[0], argv0

tests/test_pex.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,19 @@ def test_minimum_sys_modules():
120120
assert tainted_module.__path__ == ['good_path']
121121

122122

123+
@pytest.mark.parametrize('zip_safe', (False, True))
123124
@pytest.mark.parametrize('project_name', ('my_project', 'my-project'))
124125
@pytest.mark.parametrize('installer_impl', (EggInstaller, WheelInstaller))
125-
def test_pex_script(installer_impl, project_name):
126-
with make_installer(name=project_name, installer_impl=installer_impl) as installer:
126+
def test_pex_script(installer_impl, project_name, zip_safe):
127+
kw = dict(name=project_name, installer_impl=installer_impl, zip_safe=zip_safe)
128+
with make_installer(**kw) as installer:
127129
bdist = DistributionHelper.distribution_from_path(installer.bdist())
128130

129131
env_copy = os.environ.copy()
130132
env_copy['PEX_SCRIPT'] = 'hello_world'
131133
so, rc = run_simple_pex_test('', env=env_copy)
132134
assert rc == 1, so.decode('utf-8')
133-
assert b'Could not find' in so
135+
assert b'Could not find script hello_world' in so
134136

135137
so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist])
136138
assert rc == 0, so.decode('utf-8')

0 commit comments

Comments
 (0)