2012-05-11 21:30:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2012 Bastian Kleineidam
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-05-12 07:00:55 +00:00
|
|
|
"""Archive commands for the zipfile Python module."""
|
2012-05-25 20:15:29 +00:00
|
|
|
from .. import util
|
2012-05-11 21:30:46 +00:00
|
|
|
import zipfile
|
2012-05-23 18:21:53 +00:00
|
|
|
import os
|
2012-05-11 21:30:46 +00:00
|
|
|
|
|
|
|
READ_SIZE_BYTES = 1024*1024
|
|
|
|
|
|
|
|
|
2012-05-12 07:31:27 +00:00
|
|
|
def list_zip (archive, compression, cmd, **kwargs):
|
2012-05-12 07:00:55 +00:00
|
|
|
"""List member of a ZIP archive with the zipfile Python module."""
|
2012-05-11 21:30:46 +00:00
|
|
|
verbose = kwargs['verbose']
|
|
|
|
if verbose:
|
|
|
|
util.log_info('listing %s...' % archive)
|
|
|
|
zfile = zipfile.ZipFile(archive, "r")
|
|
|
|
try:
|
|
|
|
for name in zfile.namelist():
|
|
|
|
util.log_info('member %s' % name)
|
|
|
|
finally:
|
|
|
|
zfile.close()
|
|
|
|
return None
|
|
|
|
|
|
|
|
test_zip = list_zip
|
|
|
|
|
2012-05-12 07:31:27 +00:00
|
|
|
def extract_zip (archive, compression, cmd, **kwargs):
|
2012-05-11 21:30:46 +00:00
|
|
|
"""Extract a ZIP archive with the zipfile Python module."""
|
|
|
|
verbose = kwargs['verbose']
|
|
|
|
outdir = kwargs['outdir']
|
|
|
|
if verbose:
|
|
|
|
util.log_info('extracting %s...' % archive)
|
|
|
|
zfile = zipfile.ZipFile(archive)
|
|
|
|
try:
|
2012-05-23 17:55:42 +00:00
|
|
|
zfile.extractall(outdir)
|
2012-05-11 21:30:46 +00:00
|
|
|
finally:
|
|
|
|
zfile.close()
|
|
|
|
if verbose:
|
|
|
|
util.log_info('... extracted to %s' % outdir)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-05-12 07:31:27 +00:00
|
|
|
def create_zip (archive, compression, cmd, *args, **kwargs):
|
2012-05-11 21:30:46 +00:00
|
|
|
"""Create a ZIP archive with the zipfile Python module."""
|
|
|
|
verbose = kwargs['verbose']
|
|
|
|
if verbose:
|
|
|
|
util.log_info('creating %s...' % archive)
|
|
|
|
zfile = zipfile.ZipFile(archive, 'w')
|
|
|
|
try:
|
|
|
|
for filename in args:
|
2012-05-23 18:21:53 +00:00
|
|
|
if os.path.isdir(filename):
|
|
|
|
write_directory(zfile, filename)
|
|
|
|
else:
|
|
|
|
zfile.write(filename)
|
2012-05-11 21:30:46 +00:00
|
|
|
finally:
|
|
|
|
zfile.close()
|
|
|
|
return None
|
2012-05-23 18:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_directory (zfile, directory):
|
|
|
|
"""Write recursively all directories and filenames to zipfile instance."""
|
|
|
|
for dirpath, dirnames, filenames in os.walk(directory):
|
|
|
|
zfile.write(dirpath)
|
|
|
|
for filename in filenames:
|
|
|
|
zfile.write(os.path.join(dirpath, filename))
|