Skip to content

Commit

Permalink
Leverage pex.common.open_zip. (#409)
Browse files Browse the repository at this point in the history
Also use in one spot in the `pex.testing` module where we weren't
before.
  • Loading branch information
jsirois authored Sep 1, 2017
1 parent 6ad94b6 commit 616dc9c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# "machines".
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false
# The pypy test shard fails with "too many open files" under Trusty so we pin to Precise.
dist: precise

# TRAVIS_PYTHON_VERSION

Expand Down
2 changes: 1 addition & 1 deletion pex/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,6 @@ def delete(self):
shutil.rmtree(self.chroot)

def zip(self, filename, mode='wb'):
with contextlib.closing(zipfile.ZipFile(filename, mode)) as zf:
with open_zip(filename, mode) as zf:
for f in sorted(self.files()):
zf.write(os.path.join(self.chroot, f), arcname=f, compress_type=zipfile.ZIP_DEFLATED)
6 changes: 3 additions & 3 deletions pex/pex_bootstrapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import contextlib
import os
import sys
import zipfile

from .common import open_zip

__all__ = ('bootstrap_pex',)

Expand All @@ -24,7 +24,7 @@ def read_pexinfo_from_directory(entry_point):


def read_pexinfo_from_zip(entry_point):
with contextlib.closing(zipfile.ZipFile(entry_point)) as zf:
with open_zip(entry_point) as zf:
return zf.read('PEX-INFO')


Expand Down
9 changes: 4 additions & 5 deletions pex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import random
import sys
import tempfile
import zipfile
from collections import namedtuple
from textwrap import dedent

from .bin.pex import log, main
from .common import safe_mkdir, safe_rmtree
from .common import open_zip, safe_mkdir, safe_rmtree
from .compatibility import nested
from .executor import Executor
from .installer import EggInstaller, Packager
Expand Down Expand Up @@ -48,7 +47,7 @@ def random_bytes(length):

def get_dep_dist_names_from_pex(pex_path, match_prefix=''):
"""Given an on-disk pex, extract all of the unique first-level paths under `.deps`."""
with zipfile.ZipFile(pex_path) as pex_zip:
with open_zip(pex_path) as pex_zip:
dep_gen = (f.split(os.sep)[1] for f in pex_zip.namelist() if f.startswith('.deps/'))
return set(item for item in dep_gen if item.startswith(match_prefix))

Expand Down Expand Up @@ -80,7 +79,7 @@ def yield_files(directory):


def write_zipfile(directory, dest, reverse=False):
with contextlib.closing(zipfile.ZipFile(dest, 'w')) as zf:
with open_zip(dest, 'w') as zf:
for filename, rel_filename in sorted(yield_files(directory), reverse=reverse):
zf.write(filename, arcname=rel_filename)
return dest
Expand Down Expand Up @@ -152,7 +151,7 @@ def make_bdist(name='my_project', version='0.0.0', installer_impl=EggInstaller,
else:
with temporary_dir() as td:
extract_path = os.path.join(td, os.path.basename(dist_location))
with contextlib.closing(zipfile.ZipFile(dist_location)) as zf:
with open_zip(dist_location) as zf:
zf.extractall(extract_path)
yield DistributionHelper.distribution_from_path(extract_path)

Expand Down
5 changes: 2 additions & 3 deletions tests/test_pex_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
import zipfile
from contextlib import closing

from twitter.common.contextutil import temporary_dir

from pex.common import open_zip
from pex.pex_bootstrapper import get_pex_info
from pex.testing import write_simple_pex

Expand All @@ -21,7 +20,7 @@ def test_get_pex_info():
pex_info = get_pex_info(pex_path)

with temporary_dir() as pex_td:
with closing(zipfile.ZipFile(pex_path, 'r')) as zf:
with open_zip(pex_path, 'r') as zf:
zf.extractall(pex_td)

# from dir
Expand Down
5 changes: 2 additions & 3 deletions tests/test_pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import os
import stat
import sys
import zipfile
from contextlib import closing

import pytest
from twitter.common.contextutil import temporary_dir
from twitter.common.dirutil import safe_mkdir

from pex.common import open_zip
from pex.compatibility import WINDOWS, nested
from pex.pex import PEX
from pex.pex_builder import PEXBuilder
Expand Down Expand Up @@ -55,7 +54,7 @@ def test_pex_builder():
td1, td2, p1):
target_egg_dir = os.path.join(td2, os.path.basename(p1.location))
safe_mkdir(target_egg_dir)
with closing(zipfile.ZipFile(p1.location, 'r')) as zf:
with open_zip(p1.location, 'r') as zf:
zf.extractall(target_egg_dir)
p1 = DistributionHelper.distribution_from_path(target_egg_dir)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import contextlib
import functools
import os
import zipfile
from hashlib import sha1
from textwrap import dedent

from twitter.common.contextutil import temporary_dir

from pex.common import safe_mkdir
from pex.common import open_zip, safe_mkdir
from pex.compatibility import nested, to_bytes
from pex.installer import EggInstaller, WheelInstaller
from pex.pex_builder import PEXBuilder
Expand Down Expand Up @@ -60,7 +58,7 @@ def test_hash_consistency():
dir_hash = CacheHelper.dir_hash(td)
with named_temporary_file() as tf:
write_zipfile(td, tf.name, reverse=reverse)
with contextlib.closing(zipfile.ZipFile(tf.name, 'r')) as zf:
with open_zip(tf.name, 'r') as zf:
zip_hash = CacheHelper.zip_hash(zf)
assert zip_hash == dir_hash
assert zip_hash != sha1().hexdigest() # make sure it's not an empty hash
Expand Down

0 comments on commit 616dc9c

Please sign in to comment.