Skip to content

Commit

Permalink
Replaces 'unzip' executable with zipfile
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Jun 2, 2014
1 parent 39c0727 commit 2cea41e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
44 changes: 20 additions & 24 deletions vistrails/core/modules/basic_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import pickle
import re
import shutil
#import zipfile
import zipfile
import urllib

try:
Expand Down Expand Up @@ -1058,23 +1058,23 @@ def compute(self):
##############################################################################

def zip_extract_file(archive, filename_in_archive, output_filename):
return os.system(
"%s > %s" % (
vistrails.core.system.list2cmdline([
vistrails.core.system.get_executable_path('unzip'),
'-q', '-o',
'-p', archive,
filename_in_archive]),
vistrails.core.system.list2cmdline([output_filename])))
z = zipfile.ZipFile(archive)
try:
fileinfo = z.getinfo(filename_in_archive) # Might raise KeyError
output_dirname, output_filename = os.path.split(output_filename)
fileinfo.filename = output_filename
z.extract(fileinfo, output_dirname)
finally:
z.close()


def zip_extract_all_files(archive, output_path):
return os.system(
vistrails.core.system.list2cmdline([
vistrails.core.system.get_executable_path('unzip'),
'-q', '-o',
archive,
'-d', output_path]))
z = zipfile.ZipFile(archive)
try:
z.extractall(output_path)
finally:
z.close()


class Unzip(Module):
"""Unzip extracts a file from a ZIP archive."""
Expand All @@ -1091,11 +1091,9 @@ def compute(self):
raise ModuleError(self, "archive file does not exist")
suffix = self.interpreter.filePool.guess_suffix(filename_in_archive)
output = self.interpreter.filePool.create_file(suffix=suffix)
s = zip_extract_file(archive_file.name,
filename_in_archive,
output.name)
if s != 0:
raise ModuleError(self, "unzip command failed with status %d" % s)
zip_extract_file(archive_file.name,
filename_in_archive,
output.name)
self.set_output("file", output)


Expand All @@ -1110,10 +1108,8 @@ def compute(self):
if not os.path.isfile(archive_file.name):
raise ModuleError(self, "archive file does not exist")
output = self.interpreter.filePool.create_directory()
s = zip_extract_all_files(archive_file.name,
output.name)
if s != 0:
raise ModuleError(self, "unzip command failed with status %d" % s)
zip_extract_all_files(archive_file.name,
output.name)
self.set_output("directory", output)

##############################################################################
Expand Down
15 changes: 6 additions & 9 deletions vistrails/db/services/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import shutil
import tempfile
import copy
import zipfile

from vistrails.db import VistrailsDBException
from vistrails.db.domain import DBVistrail, DBWorkflow, DBLog, DBAbstraction, DBGroup, \
Expand Down Expand Up @@ -710,17 +711,13 @@ def open_vistrail_bundle_from_zip_xml(filename):
and thumbnails inside archive are '.png' files in 'thumbs' dir
"""
unzipcmd = get_executable_path('unzip')
if unzipcmd is None:
raise VistrailsInternalError("unzip command is not available")

vt_save_dir = tempfile.mkdtemp(prefix='vt_save')
output = []
cmdline = [unzipcmd, '-q','-o','-d', vt_save_dir, filename]
result = execute_cmdline(cmdline, output)

if result != 0 and len(output) != 0:
raise VistrailsDBException("Unzip of '%s' failed" % filename)
z = zipfile.ZipFile(filename)
try:
z.extractall(vt_save_dir)
finally:
z.close()

vistrail = None
log = None
Expand Down

0 comments on commit 2cea41e

Please sign in to comment.