From b6a813ac0f5f670ae27e37a7af2a21cbdb37a6f2 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Tue, 16 Sep 2014 19:23:48 -0700 Subject: [PATCH 01/11] Adding classmethod to access files within a zipped pex --- pex/environment.py | 30 +++++++++++++++++++++++++++++- tests/test_environment.py | 10 ++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pex/environment.py b/pex/environment.py index 489744c3f..c7b29e78e 100644 --- a/pex/environment.py +++ b/pex/environment.py @@ -16,7 +16,7 @@ WorkingSet ) -from .common import open_zip, safe_mkdir, safe_rmtree +from .common import open_zip, safe_mkdir, safe_mkdtemp, safe_rmtree from .interpreter import PythonInterpreter from .package import distribution_compatible from .pex_builder import PEXBuilder @@ -98,6 +98,34 @@ def load_internal_cache(cls, pex, pex_info): for dist in cls.write_zipped_internal_cache(pex, pex_info): yield dist + @classmethod + def access_zipped_assets(cls, static_module_name, static_path, asset_path, dir_location='.'): + """ + Create a copy of static resource files as we can't serve + them from within the pex file. + + :param static_module_name: Module name containing module to cache in a tempdir + :type static_module_name: string in the form of 'twitter.common.zookeeper' + :param static_path: Module name of the form 'serverset' + :param asset_path: Initially a module name that's the same as the static_path, but will be changed to walk + the directory tree + :param dir_location: directory to create a new temporary directory in + """ + temp_dir = safe_mkdtemp(dir=dir_location) + # ['__init__.py', 'rando_module.py'] + for asset in pkg_resources.resource_listdir(static_module_name, asset_path): + print("Asset: %s" % asset) + asset_target = os.path.join(os.path.relpath(asset_path, static_path), asset) + print("Asset target: %s" % asset_target) + if pkg_resources.resource_isdir(static_module, os.path.join(asset_path, asset)): + safe_mkdir(os.path.join(temp_dir, asset_target)) + access_zipped_file(static_module_name, static_path, os.path.join(asset_path, asset)) + else: + with open(os.path.join(temp_dir, asset_target), 'wb') as fp: + fp.write(pkg_resources.resource_string( + static_module_name, os.path.join(static_path, asset_target))) + + def __init__(self, pex, pex_info, interpreter=None, **kw): self._internal_cache = os.path.join(pex, pex_info.internal_cache) self._pex = pex diff --git a/tests/test_environment.py b/tests/test_environment.py index 7a60ab9a9..d9fbc79c1 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -93,3 +93,13 @@ def test_load_internal_cache_unzipped(): assert len(dists) == 1 assert normalize(dists[0].location).startswith( normalize(os.path.join(pb.path(), pb.info.internal_cache))) + +def test_access_zipped_assets(): + with nested(yield_pex_builder(zip_safe=False), temporary_dir()) as (pb, pex_root): + print("pb: %s" % pb) + pex = pb.path() + print("pex: %s" % pex) + pb.info.pex_root = pex_root + print("pex_root: %s" % pex_root) + pb.freeze() + import subprocess; subprocess.check_call('%s/*.pex' % pex) From 78b5be96daaf334c65abe81087a65fcb08f380ac Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Wed, 17 Sep 2014 13:55:57 -0700 Subject: [PATCH 02/11] Improving testing by using a built pex --- tests/test_environment.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/test_environment.py b/tests/test_environment.py index d9fbc79c1..bda84fbca 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -3,6 +3,7 @@ import os from contextlib import contextmanager +from textwrap import dedent from twitter.common.contextutil import temporary_dir, temporary_file @@ -10,7 +11,7 @@ from pex.environment import PEXEnvironment from pex.pex_builder import PEXBuilder from pex.pex_info import PexInfo -from pex.testing import make_bdist +from pex.testing import make_bdist, run_simple_pex_test @contextmanager @@ -95,11 +96,13 @@ def test_load_internal_cache_unzipped(): normalize(os.path.join(pb.path(), pb.info.internal_cache))) def test_access_zipped_assets(): - with nested(yield_pex_builder(zip_safe=False), temporary_dir()) as (pb, pex_root): - print("pb: %s" % pb) - pex = pb.path() - print("pex: %s" % pex) - pb.info.pex_root = pex_root - print("pex_root: %s" % pex_root) - pb.freeze() - import subprocess; subprocess.check_call('%s/*.pex' % pex) + body = dedent(''' + import _pex.environment + #assert dir(_pex.environment) == 'gimmeh' + #from _pex.environment import access_zipped_assets + with open('/Users/jsmith/test.txt', 'w') as fp: + fp.write('jiem')#access_zipped_assets) + print(dir(_pex.environment)) + print('pex_return_value') + ''') + assert run_simple_pex_test(body, coverage=True) == ('pex_return_value\n', 0) From 656f1d19390d337dddb120ede785fbc512ca3ba1 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Wed, 17 Sep 2014 16:50:23 -0700 Subject: [PATCH 03/11] testing progress --- pex/environment.py | 16 ++++++++++++---- pex/testing.py | 5 ++++- tests/test_environment.py | 19 +++++++++---------- tox.ini | 4 ++-- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pex/environment.py b/pex/environment.py index c7b29e78e..ce1e0be11 100644 --- a/pex/environment.py +++ b/pex/environment.py @@ -13,6 +13,9 @@ Environment, find_distributions, Requirement, + resource_isdir, + resource_listdir, + resource_string, WorkingSet ) @@ -113,17 +116,22 @@ def access_zipped_assets(cls, static_module_name, static_path, asset_path, dir_l """ temp_dir = safe_mkdtemp(dir=dir_location) # ['__init__.py', 'rando_module.py'] - for asset in pkg_resources.resource_listdir(static_module_name, asset_path): + for asset in resource_listdir(static_module_name, asset_path): print("Asset: %s" % asset) asset_target = os.path.join(os.path.relpath(asset_path, static_path), asset) print("Asset target: %s" % asset_target) - if pkg_resources.resource_isdir(static_module, os.path.join(asset_path, asset)): + if resource_isdir(static_module_name, os.path.join(asset_path, asset)): + print("Not writing to: %s" % os.path.join(asset_path, asset)) safe_mkdir(os.path.join(temp_dir, asset_target)) access_zipped_file(static_module_name, static_path, os.path.join(asset_path, asset)) else: with open(os.path.join(temp_dir, asset_target), 'wb') as fp: - fp.write(pkg_resources.resource_string( - static_module_name, os.path.join(static_path, asset_target))) + print("writing to %s" % fp.name) + path = os.path.join(static_path, asset_target) + print("static_module_name: %s" % static_module_name) + print("Path to be written: %s" % path) + fp.write(resource_string( + static_module_name, path)) def __init__(self, pex, pex_info, interpreter=None, **kw): diff --git a/pex/testing.py b/pex/testing.py index 7276a5faf..f01e9cd2b 100644 --- a/pex/testing.py +++ b/pex/testing.py @@ -71,7 +71,7 @@ def write_zipfile(directory, dest, reverse=False): name=%(project_name)r, version='0.0.0', zip_safe=%(zip_safe)r, - packages=['my_package'], + packages=['my_package', 'my_package.submodule'], package_data={'my_package': ['package_data/*.dat']}, ) '''), @@ -80,6 +80,8 @@ def write_zipfile(directory, dest, reverse=False): '''), 'my_package/__init__.py': 0, 'my_package/my_module.py': 'def do_something():\n print("hello world!")\n', + 'my_package/submodule/__init__.py': 0, + 'my_package/submodule/another_module.py': 'print("accessed")\n', 'my_package/package_data/resource1.dat': 1000, 'my_package/package_data/resource2.dat': 1000, } @@ -151,6 +153,7 @@ def run_simple_pex(pex, args=(), env=None): def run_simple_pex_test(body, args=(), env=None, dists=None, coverage=False): with nested(temporary_dir(), temporary_dir()) as (td1, td2): + td2 = '/Users/jsmith' pb = write_simple_pex(td1, body, dists=dists, coverage=coverage) pex = os.path.join(td2, 'app.pex') pb.build(pex) diff --git a/tests/test_environment.py b/tests/test_environment.py index bda84fbca..4d0d033d4 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -96,13 +96,12 @@ def test_load_internal_cache_unzipped(): normalize(os.path.join(pb.path(), pb.info.internal_cache))) def test_access_zipped_assets(): - body = dedent(''' - import _pex.environment - #assert dir(_pex.environment) == 'gimmeh' - #from _pex.environment import access_zipped_assets - with open('/Users/jsmith/test.txt', 'w') as fp: - fp.write('jiem')#access_zipped_assets) - print(dir(_pex.environment)) - print('pex_return_value') - ''') - assert run_simple_pex_test(body, coverage=True) == ('pex_return_value\n', 0) + with nested(make_bdist()) as p1: + body = dedent(''' + from _pex.environment import PEXEnvironment + PEXEnvironment.access_zipped_assets('my_package', 'submodule', 'submodule') + import os + with open('/Users/jsmith/test.txt', 'w') as fp: + fp.write('jiem') + ''') + assert run_simple_pex_test(body, dists=p1, coverage=False) == ('accessed\n', 0) diff --git a/tox.ini b/tox.ini index 66f12d15d..6a038f300 100644 --- a/tox.ini +++ b/tox.ini @@ -47,7 +47,7 @@ deps = lockfile [testenv] -commands = py.test --basetemp={envtmpdir} -n 4 {posargs:} +commands = py.test -vv --basetemp={envtmpdir} -n 4 {posargs:} changedir = tests install_command = pip install {opts} {packages} deps = @@ -79,7 +79,7 @@ basepython = python3.4 [testenv:pypy] # Run pypy with only one thread because it opens tons of files. -commands = py.test --basetemp={envtmpdir} -n 1 {posargs:} +commands = py.test -vv --basetemp={envtmpdir} -n 1 {posargs:} basepython = pypy deps = {[basedeps]deps} From 4e9d25433841da5346fb4f192b1ab9787e65e327 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 00:00:26 -0700 Subject: [PATCH 04/11] We actually want to extract from within the pex as opposed to within an egg --- pex/testing.py | 13 +++++++--- tests/test_environment.py | 52 ++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/pex/testing.py b/pex/testing.py index f01e9cd2b..e995421c2 100644 --- a/pex/testing.py +++ b/pex/testing.py @@ -71,7 +71,7 @@ def write_zipfile(directory, dest, reverse=False): name=%(project_name)r, version='0.0.0', zip_safe=%(zip_safe)r, - packages=['my_package', 'my_package.submodule'], + packages=['my_package'], package_data={'my_package': ['package_data/*.dat']}, ) '''), @@ -80,8 +80,6 @@ def write_zipfile(directory, dest, reverse=False): '''), 'my_package/__init__.py': 0, 'my_package/my_module.py': 'def do_something():\n print("hello world!")\n', - 'my_package/submodule/__init__.py': 0, - 'my_package/submodule/another_module.py': 'print("accessed")\n', 'my_package/package_data/resource1.dat': 1000, 'my_package/package_data/resource2.dat': 1000, } @@ -124,6 +122,14 @@ def make_bdist(name='my_project', installer_impl=EggInstaller, zipped=False, zip def write_simple_pex(td, exe_contents, dists=None, coverage=False): + """Write a pex file that contains an executable entry point + + :param td: temporary directory path + :param exe_contents: entry point python file + :type exe_contents: string + :param dists: distributions to include, typically sdists or bdists + :param coverage: include coverage header + """ dists = dists or [] with open(os.path.join(td, 'exe.py'), 'w') as fp: @@ -153,7 +159,6 @@ def run_simple_pex(pex, args=(), env=None): def run_simple_pex_test(body, args=(), env=None, dists=None, coverage=False): with nested(temporary_dir(), temporary_dir()) as (td1, td2): - td2 = '/Users/jsmith' pb = write_simple_pex(td1, body, dists=dists, coverage=coverage) pex = os.path.join(td2, 'app.pex') pb.build(pex) diff --git a/tests/test_environment.py b/tests/test_environment.py index 4d0d033d4..0d2123386 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -3,10 +3,12 @@ import os from contextlib import contextmanager +import subprocess from textwrap import dedent from twitter.common.contextutil import temporary_dir, temporary_file +from pex.common import safe_mkdir from pex.compatibility import nested from pex.environment import PEXEnvironment from pex.pex_builder import PEXBuilder @@ -96,12 +98,44 @@ def test_load_internal_cache_unzipped(): normalize(os.path.join(pb.path(), pb.info.internal_cache))) def test_access_zipped_assets(): - with nested(make_bdist()) as p1: - body = dedent(''' - from _pex.environment import PEXEnvironment - PEXEnvironment.access_zipped_assets('my_package', 'submodule', 'submodule') - import os - with open('/Users/jsmith/test.txt', 'w') as fp: - fp.write('jiem') - ''') - assert run_simple_pex_test(body, dists=p1, coverage=False) == ('accessed\n', 0) + test_executable = dedent(''' + from _pex.environment import PEXEnvironment + PEXEnvironment.access_zipped_assets('my_package', 'submodule', 'submodule') + import os + with open(os.path.join('my_package', 'submodule', 'mod.py'), 'r') as fp: + for line in fp: + print(line) + ''') + with nested(temporary_dir(), temporary_dir()) as (td1, td2): + td2 = '/Users/joe' + + pb = PEXBuilder(path=td1) + with open(os.path.join(td1, 'exe.py'), 'w') as fp: + fp.write(test_executable) + pb.set_executable(fp.name) + + submodule = os.path.join(td1, 'my_package', 'submodule') + safe_mkdir(submodule) + with open(os.path.join(td1, 'my_package', '__init__.py'), 'w') as fp: + fp.write('') + pb.add_source(fp.name, 'my_package/__init__.py') + with open(os.path.join(submodule, '__init__.py'), 'w') as fp: + fp.write('') + pb.add_source(fp.name, 'my_package/submodule/__init__.py') + with open(os.path.join(submodule, 'mod.py'), 'w') as fp: + fp.write('accessed') + pb.add_source(fp.name, 'my_package/submodule/mod.py') + + with open(submodule + '/mod.py') as fp: + for line in fp: + print(line) + + pex = os.path.join(td2, 'app.pex') + pb.build(pex) + + po = subprocess.Popen( + [pex], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + po.wait() + assert ('jimmeh', 0) == po.stdout.read(), po.returncode From d9a2240016f59483a6321fd717e456f1d1ea6bd0 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 01:10:03 -0700 Subject: [PATCH 05/11] Working integration test for access cached-assets within a pex --- pex/environment.py | 14 ++++---------- tests/test_environment.py | 13 +++++-------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/pex/environment.py b/pex/environment.py index ce1e0be11..2de486683 100644 --- a/pex/environment.py +++ b/pex/environment.py @@ -115,23 +115,17 @@ def access_zipped_assets(cls, static_module_name, static_path, asset_path, dir_l :param dir_location: directory to create a new temporary directory in """ temp_dir = safe_mkdtemp(dir=dir_location) - # ['__init__.py', 'rando_module.py'] for asset in resource_listdir(static_module_name, asset_path): - print("Asset: %s" % asset) - asset_target = os.path.join(os.path.relpath(asset_path, static_path), asset) - print("Asset target: %s" % asset_target) + asset_target = os.path.join(os.path.relpath(asset_path, static_path), asset)[2:] if resource_isdir(static_module_name, os.path.join(asset_path, asset)): - print("Not writing to: %s" % os.path.join(asset_path, asset)) safe_mkdir(os.path.join(temp_dir, asset_target)) access_zipped_file(static_module_name, static_path, os.path.join(asset_path, asset)) else: with open(os.path.join(temp_dir, asset_target), 'wb') as fp: - print("writing to %s" % fp.name) path = os.path.join(static_path, asset_target) - print("static_module_name: %s" % static_module_name) - print("Path to be written: %s" % path) - fp.write(resource_string( - static_module_name, path)) + file_data = resource_string(static_module_name, path) + fp.write(file_data) + return temp_dir def __init__(self, pex, pex_info, interpreter=None, **kw): diff --git a/tests/test_environment.py b/tests/test_environment.py index 0d2123386..5fb8d37d3 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -99,10 +99,10 @@ def test_load_internal_cache_unzipped(): def test_access_zipped_assets(): test_executable = dedent(''' - from _pex.environment import PEXEnvironment - PEXEnvironment.access_zipped_assets('my_package', 'submodule', 'submodule') import os - with open(os.path.join('my_package', 'submodule', 'mod.py'), 'r') as fp: + from _pex.environment import PEXEnvironment + temp_dir = PEXEnvironment.access_zipped_assets('my_package', 'submodule', 'submodule') + with open(os.path.join(temp_dir, 'mod.py'), 'r') as fp: for line in fp: print(line) ''') @@ -126,10 +126,6 @@ def test_access_zipped_assets(): fp.write('accessed') pb.add_source(fp.name, 'my_package/submodule/mod.py') - with open(submodule + '/mod.py') as fp: - for line in fp: - print(line) - pex = os.path.join(td2, 'app.pex') pb.build(pex) @@ -138,4 +134,5 @@ def test_access_zipped_assets(): stdout=subprocess.PIPE, stderr=subprocess.STDOUT) po.wait() - assert ('jimmeh', 0) == po.stdout.read(), po.returncode + assert po.stdout.read() == 'accessed\n' + assert po.returncode is 0 From 7d36f9057cc039dc1c81f2a99e04f4a9d6a7d82d Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 01:10:56 -0700 Subject: [PATCH 06/11] Decreasing test-scope --- tests/test_environment.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/test_environment.py b/tests/test_environment.py index 5fb8d37d3..9a1a3f941 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -116,15 +116,9 @@ def test_access_zipped_assets(): submodule = os.path.join(td1, 'my_package', 'submodule') safe_mkdir(submodule) - with open(os.path.join(td1, 'my_package', '__init__.py'), 'w') as fp: - fp.write('') - pb.add_source(fp.name, 'my_package/__init__.py') - with open(os.path.join(submodule, '__init__.py'), 'w') as fp: - fp.write('') - pb.add_source(fp.name, 'my_package/submodule/__init__.py') with open(os.path.join(submodule, 'mod.py'), 'w') as fp: fp.write('accessed') - pb.add_source(fp.name, 'my_package/submodule/mod.py') + pb.add_source(fp.name, 'my_package/jimmeh/mod.py') pex = os.path.join(td2, 'app.pex') pb.build(pex) From 63549441891923cfed0bb9d7e5d180e1e5da1c1f Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 02:11:37 -0700 Subject: [PATCH 07/11] Unit test coverage --- pex/environment.py | 2 +- tests/test_environment.py | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pex/environment.py b/pex/environment.py index 2de486683..1ed62d8f2 100644 --- a/pex/environment.py +++ b/pex/environment.py @@ -119,7 +119,7 @@ def access_zipped_assets(cls, static_module_name, static_path, asset_path, dir_l asset_target = os.path.join(os.path.relpath(asset_path, static_path), asset)[2:] if resource_isdir(static_module_name, os.path.join(asset_path, asset)): safe_mkdir(os.path.join(temp_dir, asset_target)) - access_zipped_file(static_module_name, static_path, os.path.join(asset_path, asset)) + cls.access_zipped_assets(static_module_name, static_path, os.path.join(asset_path, asset)) else: with open(os.path.join(temp_dir, asset_target), 'wb') as fp: path = os.path.join(static_path, asset_target) diff --git a/tests/test_environment.py b/tests/test_environment.py index 9a1a3f941..67cdbccf0 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -6,9 +6,11 @@ import subprocess from textwrap import dedent +import mock +import pkg_resources from twitter.common.contextutil import temporary_dir, temporary_file -from pex.common import safe_mkdir +from pex.common import safe_mkdir, safe_mkdtemp from pex.compatibility import nested from pex.environment import PEXEnvironment from pex.pex_builder import PEXBuilder @@ -97,7 +99,23 @@ def test_load_internal_cache_unzipped(): assert normalize(dists[0].location).startswith( normalize(os.path.join(pb.path(), pb.info.internal_cache))) -def test_access_zipped_assets(): +@mock.patch('__builtin__.open') +@mock.patch('pex.environment.resource_string', spec=pkg_resources.resource_string) +@mock.patch('pex.environment.resource_isdir', spec=pkg_resources.resource_isdir) +@mock.patch('pex.environment.resource_listdir', spec=pkg_resources.resource_listdir) +def test_access_zipped_assets(mock_resource_listdir, mock_resource_isdir, mock_resource_string, mock_open): + mock_resource_listdir.side_effect = [['./__init__.py', './directory/'], ['file.py']] + mock_resource_isdir.side_effect = [False, True, False] + mock_resource_string.return_value = 'testing' + + PEXEnvironment.access_zipped_assets('twitter.common', 'dirutil', 'dirutil') + + assert mock_resource_listdir.call_count == 2 + assert mock_open.call_count == 2 + file_handle = mock_open.return_value.__enter__.return_value + assert file_handle.write.call_count == 2 + +def test_access_zipped_assets_integration(): test_executable = dedent(''' import os from _pex.environment import PEXEnvironment @@ -116,9 +134,10 @@ def test_access_zipped_assets(): submodule = os.path.join(td1, 'my_package', 'submodule') safe_mkdir(submodule) - with open(os.path.join(submodule, 'mod.py'), 'w') as fp: + mod_path = os.path.join(submodule, 'mod.py') + with open(mod_path, 'w') as fp: fp.write('accessed') - pb.add_source(fp.name, 'my_package/jimmeh/mod.py') + pb.add_source(fp.name, 'my_package/submodule/mod.py') pex = os.path.join(td2, 'app.pex') pb.build(pex) @@ -129,4 +148,4 @@ def test_access_zipped_assets(): stderr=subprocess.STDOUT) po.wait() assert po.stdout.read() == 'accessed\n' - assert po.returncode is 0 + assert po.returncode == 0 From 6079aa50e8a287593c5717ba7b6a2ebf6e9c9266 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 02:15:30 -0700 Subject: [PATCH 08/11] Remove verbose testing output --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index 6a038f300..66f12d15d 100644 --- a/tox.ini +++ b/tox.ini @@ -47,7 +47,7 @@ deps = lockfile [testenv] -commands = py.test -vv --basetemp={envtmpdir} -n 4 {posargs:} +commands = py.test --basetemp={envtmpdir} -n 4 {posargs:} changedir = tests install_command = pip install {opts} {packages} deps = @@ -79,7 +79,7 @@ basepython = python3.4 [testenv:pypy] # Run pypy with only one thread because it opens tons of files. -commands = py.test -vv --basetemp={envtmpdir} -n 1 {posargs:} +commands = py.test --basetemp={envtmpdir} -n 1 {posargs:} basepython = pypy deps = {[basedeps]deps} From 4e5d43e4aab871f31d2fd651710b88b5eedc5929 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 10:23:47 -0700 Subject: [PATCH 09/11] Remove debugging directory --- tests/test_environment.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_environment.py b/tests/test_environment.py index 67cdbccf0..2beb08745 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -125,8 +125,6 @@ def test_access_zipped_assets_integration(): print(line) ''') with nested(temporary_dir(), temporary_dir()) as (td1, td2): - td2 = '/Users/joe' - pb = PEXBuilder(path=td1) with open(os.path.join(td1, 'exe.py'), 'w') as fp: fp.write(test_executable) From 4388270797fffe07a785bd82f3d669b04f320cb8 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 10:59:31 -0700 Subject: [PATCH 10/11] py2 and py3 mock compatibility --- tests/test_environment.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_environment.py b/tests/test_environment.py index 2beb08745..d17d6928d 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -6,7 +6,10 @@ import subprocess from textwrap import dedent -import mock +try: + import mock +except ImportError: + from unittest import mock import pkg_resources from twitter.common.contextutil import temporary_dir, temporary_file From 3ec2123bdfff7aeca4244f186f9bd52d8572a96f Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Thu, 18 Sep 2014 13:08:13 -0700 Subject: [PATCH 11/11] builtin path and file bytes for py2 and py3 compatibility --- pex/common.py | 5 ++++- tests/test_environment.py | 40 ++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pex/common.py b/pex/common.py index d70bbe2f2..7b74564af 100644 --- a/pex/common.py +++ b/pex/common.py @@ -279,7 +279,10 @@ def write(self, data, dst, label=None, mode='wb'): self._tag(dst, label) self._mkdir_for(dst) with open(os.path.join(self.chroot, dst), mode) as wp: - wp.write(data) + try: + wp.write(data) + except TypeError: + wp.write(bytes(data, 'UTF-8')) def touch(self, dst, label=None): """Perform 'touch' on {chroot}/dest with optional label. diff --git a/tests/test_environment.py b/tests/test_environment.py index d17d6928d..2f72e8add 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -102,21 +102,30 @@ def test_load_internal_cache_unzipped(): assert normalize(dists[0].location).startswith( normalize(os.path.join(pb.path(), pb.info.internal_cache))) -@mock.patch('__builtin__.open') @mock.patch('pex.environment.resource_string', spec=pkg_resources.resource_string) @mock.patch('pex.environment.resource_isdir', spec=pkg_resources.resource_isdir) @mock.patch('pex.environment.resource_listdir', spec=pkg_resources.resource_listdir) -def test_access_zipped_assets(mock_resource_listdir, mock_resource_isdir, mock_resource_string, mock_open): - mock_resource_listdir.side_effect = [['./__init__.py', './directory/'], ['file.py']] - mock_resource_isdir.side_effect = [False, True, False] - mock_resource_string.return_value = 'testing' - - PEXEnvironment.access_zipped_assets('twitter.common', 'dirutil', 'dirutil') - - assert mock_resource_listdir.call_count == 2 - assert mock_open.call_count == 2 - file_handle = mock_open.return_value.__enter__.return_value - assert file_handle.write.call_count == 2 +def test_access_zipped_assets(mock_resource_listdir, mock_resource_isdir, mock_resource_string): + try: + import __builtin__ + builtin_path = '__builtin__' + except ImportError: + # Python3 + import builtins + builtin_path = 'builtins' + + mock_open = mock.mock_open() + with mock.patch('%s.open' % builtin_path, mock_open, create=True): + mock_resource_listdir.side_effect = [['./__init__.py', './directory/'], ['file.py']] + mock_resource_isdir.side_effect = [False, True, False] + mock_resource_string.return_value = 'testing' + + PEXEnvironment.access_zipped_assets('twitter.common', 'dirutil', 'dirutil') + + assert mock_resource_listdir.call_count == 2 + assert mock_open.call_count == 2 + file_handle = mock_open.return_value.__enter__.return_value + assert file_handle.write.call_count == 2 def test_access_zipped_assets_integration(): test_executable = dedent(''' @@ -148,5 +157,10 @@ def test_access_zipped_assets_integration(): stdout=subprocess.PIPE, stderr=subprocess.STDOUT) po.wait() - assert po.stdout.read() == 'accessed\n' + output = po.stdout.read() + try: + output = output.decode('UTF-8') + except: + pass + assert output == 'accessed\n' assert po.returncode == 0