diff --git a/libarchive/__init__.py b/libarchive/__init__.py index 4ef2866..8ad5653 100644 --- a/libarchive/__init__.py +++ b/libarchive/__init__.py @@ -29,12 +29,12 @@ import sys import time import warnings -#from ctypes import cdll, c_char_p +# from ctypes import cdll, c_char_p from libarchive import _libarchive from io import StringIO -#PY3 = sys.version_info[0] == 3 +# PY3 = sys.version_info[0] == 3 PY3 = True # Suggested block size for libarchive. Libarchive may adjust it. @@ -91,6 +91,7 @@ FILTER_EXTENSIONS = { class EOF(Exception): '''Raised by ArchiveInfo.from_archive() when unable to read the next archive header.''' + pass @@ -145,7 +146,7 @@ def is_archive_name(filename, formats=None): return format -def is_archive(f, formats=(None, ), filters=(None, )): +def is_archive(f, formats=(None,), filters=(None,)): '''Check to see if the given file is actually an archive. The format parameter can be used to specify which archive format is acceptable. If ommitted, all supported archive formats will be checked. It opens the file using libarchive. If no error is @@ -158,7 +159,7 @@ def is_archive(f, formats=(None, ), filters=(None, )): This function will return True if the file can be opened as an archive using the given format(s)/filter(s).''' - need_close : bool = False + need_close: bool = False if isinstance(f, str): f = open(f, 'rb') need_close = True @@ -188,6 +189,7 @@ def is_archive(f, formats=(None, ), filters=(None, )): class EntryReadStream(object): '''A file-like object for reading an entry from the archive.''' + def __init__(self, archive, size): self.archive = archive self.closed = False @@ -247,6 +249,7 @@ class EntryWriteStream(object): If the size is known ahead of time and provided, then the file contents are not buffered but flushed directly to the archive. If size is omitted, then the file contents are buffered and flushed in the close() method.''' + def __init__(self, archive, pathname, size=None): self.archive = archive self.entry = Entry(pathname=pathname, mtime=time.time(), mode=stat.S_IFREG) @@ -301,9 +304,10 @@ class EntryWriteStream(object): class Entry(object): '''An entry within an archive. Represents the header data and it's location within the archive.''' + def __init__(self, pathname=None, size=None, mtime=None, mode=None, hpos=None, encoding=ENCODING): - #, symlink=None + # , symlink=None self.pathname = pathname self.size = size self.mtime = mtime @@ -313,9 +317,9 @@ class Entry(object): self.symlink = "" - #if self.issym() and symlink: + # if self.issym() and symlink: # self.symlink = symlink - #else: + # else: # self.symlink = None @property @@ -402,8 +406,8 @@ class Entry(object): call_and_check(_libarchive.archive_write_header, archive._a, archive._a, e) - #todo - #self.hpos = archive.header_position + # todo + # self.hpos = archive.header_position finally: _libarchive.archive_entry_free(e) @@ -429,8 +433,18 @@ class Entry(object): class Archive(object): '''A low-level archive reader which provides forward-only iteration. Consider this a light-weight pythonic libarchive wrapper.''' - def __init__(self, f, mode='r', format=None, filter=None, entry_class=Entry, - encoding=ENCODING, blocksize=BLOCK_SIZE, password=None): + + def __init__( + self, + f, + mode='r', + format=None, + filter=None, + entry_class=Entry, + encoding=ENCODING, + blocksize=BLOCK_SIZE, + password=None, + ): assert mode in ('r', 'w', 'wb', 'a'), 'Mode should be "r", "w", "wb", or "a".' self._stream = None self.encoding = encoding @@ -550,7 +564,7 @@ class Archive(object): if hasattr(self.f, "mode") and self.f.mode != 'r' and self.f.mode != 'rb': if hasattr(self.f, "flush"): self.f.flush() - if hasattr(self.f, "fileno"): + if hasattr(self.f, "fileno"): os.fsync(self.f.fileno()) # and then close it, if we opened it... if getattr(self, '_close', None): @@ -637,7 +651,7 @@ class Archive(object): def add_passphrase(self, password): '''Adds a password to the archive.''' _libarchive.archive_read_add_passphrase(self._a, password) - + def set_passphrase(self, password): '''Sets a password for the archive.''' _libarchive.archive_write_set_passphrase(self._a, password) @@ -649,6 +663,7 @@ class SeekableArchive(Archive): occur when reading archive entries in the order in which they appear in the archive. Reading out of order will cause the archive to be closed and opened each time a reverse seek is needed.''' + def __init__(self, f, **kwargs): self._stream = None # Convert file to open file. We need this to reopen the archive. diff --git a/libarchive/_libarchive.py b/libarchive/_libarchive.py index 9ddcf02..f9fe9dd 100644 --- a/libarchive/_libarchive.py +++ b/libarchive/_libarchive.py @@ -5,6 +5,7 @@ # the SWIG interface file instead. from sys import version_info as _swig_python_version_info + if _swig_python_version_info < (2, 7, 0): raise RuntimeError("Python 2.7 or later required") @@ -19,12 +20,17 @@ try: except ImportError: import __builtin__ + def _swig_repr(self): try: strthis = "proxy of " + self.this.__repr__() except __builtin__.Exception: strthis = "" - return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,) + return "<%s.%s; %s >" % ( + self.__class__.__module__, + self.__class__.__name__, + strthis, + ) def _swig_setattr_nondynamic_instance_variable(set): @@ -37,6 +43,7 @@ def _swig_setattr_nondynamic_instance_variable(set): set(self, name, value) else: raise AttributeError("You cannot add instance attributes to %s" % self) + return set_instance_attr @@ -46,465 +53,617 @@ def _swig_setattr_nondynamic_class_variable(set): set(cls, name, value) else: raise AttributeError("You cannot add class attributes to %s" % cls) + return set_class_attr def _swig_add_metaclass(metaclass): """Class decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclass""" + def wrapper(cls): return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy()) + return wrapper class _SwigNonDynamicMeta(type): """Meta class to enforce nondynamic attributes (no new attributes) for a class""" - __setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__) + __setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__) def archive_read_new(): return __libarchive.archive_read_new() + def archive_read_free(arg1): return __libarchive.archive_read_free(arg1) + def archive_read_open_filename(arg1, _filename, _block_size): return __libarchive.archive_read_open_filename(arg1, _filename, _block_size) + def archive_read_open_memory(arg1, buff, size): return __libarchive.archive_read_open_memory(arg1, buff, size) + def archive_read_open_memory2(a, buff, size, read_size): return __libarchive.archive_read_open_memory2(a, buff, size, read_size) + def archive_read_open_fd(arg1, _fd, _block_size): return __libarchive.archive_read_open_fd(arg1, _fd, _block_size) + def archive_read_close(arg1): return __libarchive.archive_read_close(arg1) + def archive_format(arg1): return __libarchive.archive_format(arg1) + def archive_read_next_header2(arg1, arg2): return __libarchive.archive_read_next_header2(arg1, arg2) + def archive_entry_stat(arg1): return __libarchive.archive_entry_stat(arg1) + def archive_read_header_position(arg1): return __libarchive.archive_read_header_position(arg1) + def archive_read_set_format_option(_a, m, o, v): return __libarchive.archive_read_set_format_option(_a, m, o, v) + def archive_read_set_filter_option(_a, m, o, v): return __libarchive.archive_read_set_filter_option(_a, m, o, v) + def archive_read_set_option(_a, m, o, v): return __libarchive.archive_read_set_option(_a, m, o, v) + def archive_read_set_options(_a, opts): return __libarchive.archive_read_set_options(_a, opts) + def archive_read_add_passphrase(arg1, arg2): return __libarchive.archive_read_add_passphrase(arg1, arg2) + def archive_read_data_skip(arg1): return __libarchive.archive_read_data_skip(arg1) + def archive_read_data_into_fd(arg1, fd): return __libarchive.archive_read_data_into_fd(arg1, fd) + def archive_read_support_compression_all(arg1): return __libarchive.archive_read_support_compression_all(arg1) + def archive_read_support_compression_bzip2(arg1): return __libarchive.archive_read_support_compression_bzip2(arg1) + def archive_read_support_compression_compress(arg1): return __libarchive.archive_read_support_compression_compress(arg1) + def archive_read_support_compression_gzip(arg1): return __libarchive.archive_read_support_compression_gzip(arg1) + def archive_read_support_compression_lzip(arg1): return __libarchive.archive_read_support_compression_lzip(arg1) + def archive_read_support_compression_lzma(arg1): return __libarchive.archive_read_support_compression_lzma(arg1) + def archive_read_support_compression_none(arg1): return __libarchive.archive_read_support_compression_none(arg1) + def archive_read_support_compression_program(arg1, command): return __libarchive.archive_read_support_compression_program(arg1, command) + def archive_read_support_compression_program_signature(arg1, arg2, arg3, arg4): return __libarchive.archive_read_support_compression_program_signature(arg1, arg2, arg3, arg4) + def archive_read_support_compression_rpm(arg1): return __libarchive.archive_read_support_compression_rpm(arg1) + def archive_read_support_compression_uu(arg1): return __libarchive.archive_read_support_compression_uu(arg1) + def archive_read_support_compression_xz(arg1): return __libarchive.archive_read_support_compression_xz(arg1) + def archive_read_support_filter_all(arg1): return __libarchive.archive_read_support_filter_all(arg1) + def archive_read_support_filter_bzip2(arg1): return __libarchive.archive_read_support_filter_bzip2(arg1) + def archive_read_support_filter_compress(arg1): return __libarchive.archive_read_support_filter_compress(arg1) + def archive_read_support_filter_gzip(arg1): return __libarchive.archive_read_support_filter_gzip(arg1) + def archive_read_support_filter_grzip(arg1): return __libarchive.archive_read_support_filter_grzip(arg1) + def archive_read_support_filter_lrzip(arg1): return __libarchive.archive_read_support_filter_lrzip(arg1) + def archive_read_support_filter_lz4(arg1): return __libarchive.archive_read_support_filter_lz4(arg1) + def archive_read_support_filter_lzip(arg1): return __libarchive.archive_read_support_filter_lzip(arg1) + def archive_read_support_filter_lzma(arg1): return __libarchive.archive_read_support_filter_lzma(arg1) + def archive_read_support_filter_lzop(arg1): return __libarchive.archive_read_support_filter_lzop(arg1) + def archive_read_support_filter_none(arg1): return __libarchive.archive_read_support_filter_none(arg1) + def archive_read_support_filter_program(arg1, command): return __libarchive.archive_read_support_filter_program(arg1, command) + def archive_read_support_filter_program_signature(arg1, arg2, arg3, arg4): return __libarchive.archive_read_support_filter_program_signature(arg1, arg2, arg3, arg4) + def archive_read_support_filter_rpm(arg1): return __libarchive.archive_read_support_filter_rpm(arg1) + def archive_read_support_filter_uu(arg1): return __libarchive.archive_read_support_filter_uu(arg1) + def archive_read_support_filter_xz(arg1): return __libarchive.archive_read_support_filter_xz(arg1) + def archive_read_support_format_7zip(arg1): return __libarchive.archive_read_support_format_7zip(arg1) + def archive_read_support_format_all(arg1): return __libarchive.archive_read_support_format_all(arg1) + def archive_read_support_format_ar(arg1): return __libarchive.archive_read_support_format_ar(arg1) + def archive_read_support_format_by_code(arg1, arg2): return __libarchive.archive_read_support_format_by_code(arg1, arg2) + def archive_read_support_format_cab(arg1): return __libarchive.archive_read_support_format_cab(arg1) + def archive_read_support_format_cpio(arg1): return __libarchive.archive_read_support_format_cpio(arg1) + def archive_read_support_format_empty(arg1): return __libarchive.archive_read_support_format_empty(arg1) + def archive_read_support_format_gnutar(arg1): return __libarchive.archive_read_support_format_gnutar(arg1) + def archive_read_support_format_iso9660(arg1): return __libarchive.archive_read_support_format_iso9660(arg1) + def archive_read_support_format_lha(arg1): return __libarchive.archive_read_support_format_lha(arg1) + def archive_read_support_format_rar(arg1): return __libarchive.archive_read_support_format_rar(arg1) + def archive_read_support_format_raw(arg1): return __libarchive.archive_read_support_format_raw(arg1) + def archive_read_support_format_tar(arg1): return __libarchive.archive_read_support_format_tar(arg1) + def archive_read_support_format_warc(arg1): return __libarchive.archive_read_support_format_warc(arg1) + def archive_read_support_format_xar(arg1): return __libarchive.archive_read_support_format_xar(arg1) + def archive_read_support_format_zip(arg1): return __libarchive.archive_read_support_format_zip(arg1) + def archive_read_support_format_zip_streamable(arg1): return __libarchive.archive_read_support_format_zip_streamable(arg1) + def archive_read_support_format_zip_seekable(arg1): return __libarchive.archive_read_support_format_zip_seekable(arg1) + def archive_read_set_format(arg1, arg2): return __libarchive.archive_read_set_format(arg1, arg2) + def archive_read_append_filter(arg1, arg2): return __libarchive.archive_read_append_filter(arg1, arg2) + def archive_read_append_filter_program(arg1, arg2): return __libarchive.archive_read_append_filter_program(arg1, arg2) + def archive_read_append_filter_program_signature(arg1, arg2, arg3, arg4): return __libarchive.archive_read_append_filter_program_signature(arg1, arg2, arg3, arg4) + def archive_write_new(): return __libarchive.archive_write_new() + def archive_write_free(arg1): return __libarchive.archive_write_free(arg1) + def archive_write_open(arg1, arg2, arg3, arg4, arg5): return __libarchive.archive_write_open(arg1, arg2, arg3, arg4, arg5) + def archive_write_open_fd(arg1, _fd): return __libarchive.archive_write_open_fd(arg1, _fd) + def archive_write_open_filename(arg1, _file): return __libarchive.archive_write_open_filename(arg1, _file) + def archive_write_open_filename_w(arg1, _file): return __libarchive.archive_write_open_filename_w(arg1, _file) + def archive_write_open_memory(arg1, _buffer, _buffSize, _used): return __libarchive.archive_write_open_memory(arg1, _buffer, _buffSize, _used) + def archive_write_close(arg1): return __libarchive.archive_write_close(arg1) + def archive_write_header(arg1, arg2): return __libarchive.archive_write_header(arg1, arg2) + def archive_write_set_format_option(_a, m, o, v): return __libarchive.archive_write_set_format_option(_a, m, o, v) + def archive_write_set_filter_option(_a, m, o, v): return __libarchive.archive_write_set_filter_option(_a, m, o, v) + def archive_write_set_option(_a, m, o, v): return __libarchive.archive_write_set_option(_a, m, o, v) + def archive_write_set_options(_a, opts): return __libarchive.archive_write_set_options(_a, opts) + def archive_write_set_passphrase(_a, p): return __libarchive.archive_write_set_passphrase(_a, p) + def archive_write_finish_entry(arg1): return __libarchive.archive_write_finish_entry(arg1) + def archive_write_add_filter(arg1, filter_code): return __libarchive.archive_write_add_filter(arg1, filter_code) + def archive_write_add_filter_by_name(arg1, name): return __libarchive.archive_write_add_filter_by_name(arg1, name) + def archive_write_add_filter_b64encode(arg1): return __libarchive.archive_write_add_filter_b64encode(arg1) + def archive_write_add_filter_bzip2(arg1): return __libarchive.archive_write_add_filter_bzip2(arg1) + def archive_write_add_filter_compress(arg1): return __libarchive.archive_write_add_filter_compress(arg1) + def archive_write_add_filter_grzip(arg1): return __libarchive.archive_write_add_filter_grzip(arg1) + def archive_write_add_filter_gzip(arg1): return __libarchive.archive_write_add_filter_gzip(arg1) + def archive_write_add_filter_lrzip(arg1): return __libarchive.archive_write_add_filter_lrzip(arg1) + def archive_write_add_filter_lz4(arg1): return __libarchive.archive_write_add_filter_lz4(arg1) + def archive_write_add_filter_lzip(arg1): return __libarchive.archive_write_add_filter_lzip(arg1) + def archive_write_add_filter_lzma(arg1): return __libarchive.archive_write_add_filter_lzma(arg1) + def archive_write_add_filter_lzop(arg1): return __libarchive.archive_write_add_filter_lzop(arg1) + def archive_write_add_filter_none(arg1): return __libarchive.archive_write_add_filter_none(arg1) + def archive_write_add_filter_program(arg1, cmd): return __libarchive.archive_write_add_filter_program(arg1, cmd) + def archive_write_add_filter_uuencode(arg1): return __libarchive.archive_write_add_filter_uuencode(arg1) + def archive_write_add_filter_xz(arg1): return __libarchive.archive_write_add_filter_xz(arg1) + def archive_write_set_format(arg1, format_code): return __libarchive.archive_write_set_format(arg1, format_code) + def archive_write_set_format_by_name(arg1, name): return __libarchive.archive_write_set_format_by_name(arg1, name) + def archive_write_set_format_7zip(arg1): return __libarchive.archive_write_set_format_7zip(arg1) + def archive_write_set_format_ar_bsd(arg1): return __libarchive.archive_write_set_format_ar_bsd(arg1) + def archive_write_set_format_ar_svr4(arg1): return __libarchive.archive_write_set_format_ar_svr4(arg1) + def archive_write_set_format_cpio(arg1): return __libarchive.archive_write_set_format_cpio(arg1) + def archive_write_set_format_cpio_newc(arg1): return __libarchive.archive_write_set_format_cpio_newc(arg1) + def archive_write_set_format_gnutar(arg1): return __libarchive.archive_write_set_format_gnutar(arg1) + def archive_write_set_format_iso9660(arg1): return __libarchive.archive_write_set_format_iso9660(arg1) + def archive_write_set_format_mtree(arg1): return __libarchive.archive_write_set_format_mtree(arg1) + def archive_write_set_format_mtree_classic(arg1): return __libarchive.archive_write_set_format_mtree_classic(arg1) + def archive_write_set_format_pax(arg1): return __libarchive.archive_write_set_format_pax(arg1) + def archive_write_set_format_pax_restricted(arg1): return __libarchive.archive_write_set_format_pax_restricted(arg1) + def archive_write_set_format_raw(arg1): return __libarchive.archive_write_set_format_raw(arg1) + def archive_write_set_format_shar(arg1): return __libarchive.archive_write_set_format_shar(arg1) + def archive_write_set_format_shar_dump(arg1): return __libarchive.archive_write_set_format_shar_dump(arg1) + def archive_write_set_format_ustar(arg1): return __libarchive.archive_write_set_format_ustar(arg1) + def archive_write_set_format_v7tar(arg1): return __libarchive.archive_write_set_format_v7tar(arg1) + def archive_write_set_format_warc(arg1): return __libarchive.archive_write_set_format_warc(arg1) + def archive_write_set_format_xar(arg1): return __libarchive.archive_write_set_format_xar(arg1) + def archive_write_set_format_zip(arg1): return __libarchive.archive_write_set_format_zip(arg1) + def archive_write_set_format_filter_by_ext(a, filename): return __libarchive.archive_write_set_format_filter_by_ext(a, filename) + def archive_write_set_format_filter_by_ext_def(a, filename, def_ext): return __libarchive.archive_write_set_format_filter_by_ext_def(a, filename, def_ext) + def archive_write_zip_set_compression_deflate(arg1): return __libarchive.archive_write_zip_set_compression_deflate(arg1) + def archive_write_zip_set_compression_store(arg1): return __libarchive.archive_write_zip_set_compression_store(arg1) + def archive_entry_new(): return __libarchive.archive_entry_new() + def archive_entry_free(arg1): return __libarchive.archive_entry_free(arg1) + def archive_entry_pathname(arg1): return __libarchive.archive_entry_pathname(arg1) + def archive_entry_pathname_w(arg1): return __libarchive.archive_entry_pathname_w(arg1) + def archive_entry_size(arg1): return __libarchive.archive_entry_size(arg1) + def archive_entry_mtime(arg1): return __libarchive.archive_entry_mtime(arg1) + def archive_entry_filetype(arg1): return __libarchive.archive_entry_filetype(arg1) + def archive_entry_perm(arg1): return __libarchive.archive_entry_perm(arg1) + def archive_entry_symlink(arg1): return __libarchive.archive_entry_symlink(arg1) + def archive_entry_set_link(arg1, arg2): return __libarchive.archive_entry_set_link(arg1, arg2) + def archive_entry_symlink_w(arg1): return __libarchive.archive_entry_symlink_w(arg1) + def archive_read_disk_set_symlink_logical(arg1): return __libarchive.archive_read_disk_set_symlink_logical(arg1) + def archive_read_disk_set_symlink_physical(arg1): return __libarchive.archive_read_disk_set_symlink_physical(arg1) + def archive_read_disk_set_symlink_hybrid(arg1): return __libarchive.archive_read_disk_set_symlink_hybrid(arg1) + def archive_entry_set_symlink(arg1, arg2): return __libarchive.archive_entry_set_symlink(arg1, arg2) + def archive_entry_copy_symlink(arg1, arg2): return __libarchive.archive_entry_copy_symlink(arg1, arg2) + def archive_entry_copy_symlink_w(arg1, arg2): return __libarchive.archive_entry_copy_symlink_w(arg1, arg2) + def archive_entry_set_pathname(arg1, arg2): return __libarchive.archive_entry_set_pathname(arg1, arg2) + def archive_entry_set_size(arg1, arg2): return __libarchive.archive_entry_set_size(arg1, arg2) + def archive_entry_set_mtime(arg1, arg2, arg3): return __libarchive.archive_entry_set_mtime(arg1, arg2, arg3) + def archive_entry_set_filetype(arg1, arg2): return __libarchive.archive_entry_set_filetype(arg1, arg2) + def archive_entry_set_perm(arg1, arg2): return __libarchive.archive_entry_set_perm(arg1, arg2) + def archive_errno(arg1): return __libarchive.archive_errno(arg1) + def archive_error_string(arg1): return __libarchive.archive_error_string(arg1) + + ARCHIVE_VERSION_NUMBER = __libarchive.ARCHIVE_VERSION_NUMBER ARCHIVE_VERSION_STRING = __libarchive.ARCHIVE_VERSION_STRING ARCHIVE_EOF = __libarchive.ARCHIVE_EOF @@ -577,10 +736,10 @@ ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED = __libarchive.ARCHIVE_EXTRACT_HFS_COMPRE ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS = __libarchive.ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS = __libarchive.ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS + def archive_read_data_into_str(archive, len): return __libarchive.archive_read_data_into_str(archive, len) + def archive_write_data_from_str(archive, str): return __libarchive.archive_write_data_from_str(archive, str) - - diff --git a/libarchive/tar.py b/libarchive/tar.py index 773234f..4f1826a 100644 --- a/libarchive/tar.py +++ b/libarchive/tar.py @@ -1,13 +1,12 @@ import os from libarchive import is_archive, Entry, SeekableArchive -from tarfile import DEFAULT_FORMAT, USTAR_FORMAT, GNU_FORMAT, PAX_FORMAT, \ - ENCODING +from tarfile import DEFAULT_FORMAT, USTAR_FORMAT, GNU_FORMAT, PAX_FORMAT, ENCODING from tarfile import REGTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CHRTYPE, BLKTYPE FORMAT_CONVERSION = { - USTAR_FORMAT: 'tar', - GNU_FORMAT: 'gnu', - PAX_FORMAT: 'pax', + USTAR_FORMAT: 'tar', + GNU_FORMAT: 'gnu', + PAX_FORMAT: 'pax', } @@ -36,9 +35,13 @@ class TarInfo(Entry): @property def get_type(self): for attr, type in ( - ('isdir', DIRTYPE), ('isfile', REGTYPE), ('issym', SYMTYPE), - ('isfifo', FIFOTYPE), ('ischr', CHRTYPE), ('isblk', BLKTYPE), - ): + ('isdir', DIRTYPE), + ('isfile', REGTYPE), + ('issym', SYMTYPE), + ('isfifo', FIFOTYPE), + ('ischr', CHRTYPE), + ('isblk', BLKTYPE), + ): if getattr(self, attr)(): return type @@ -52,13 +55,12 @@ class TarInfo(Entry): class TarFile(SeekableArchive): - getmember = SeekableArchive.getentry - list = SeekableArchive.printlist - extract = SeekableArchive.readpath + getmember = SeekableArchive.getentry + list = SeekableArchive.printlist + extract = SeekableArchive.readpath extractfile = SeekableArchive.readstream - def __init__(self, name=None, mode='r', fileobj=None, - format=DEFAULT_FORMAT, tarinfo=TarInfo, encoding=ENCODING): + def __init__(self, name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, encoding=ENCODING): if name: f = name elif fileobj: @@ -67,8 +69,7 @@ class TarFile(SeekableArchive): format = FORMAT_CONVERSION.get(format) except KeyError: raise Exception('Invalid tar format: %s' % format) - super(TarFile, self).__init__(f, mode=mode, format=format, - entry_class=tarinfo, encoding=encoding) + super(TarFile, self).__init__(f, mode=mode, format=format, entry_class=tarinfo, encoding=encoding) def getmembers(self): return list(self) @@ -78,7 +79,7 @@ class TarFile(SeekableArchive): def __next__(self): raise NotImplementedError - pass # TODO: how to do this? + pass # TODO: how to do this? def extract(self, member, path=None): if path is None: @@ -90,7 +91,7 @@ class TarFile(SeekableArchive): return self.readpath(member, f) def add(self, name, arcname, recursive=True, exclude=None, filter=None): - pass # TODO: implement this. + pass # TODO: implement this. def addfile(self, tarinfo, fileobj): return self.writepath(fileobj, tarinfo) diff --git a/libarchive/zip.py b/libarchive/zip.py index 2995aea..9e5d433 100644 --- a/libarchive/zip.py +++ b/libarchive/zip.py @@ -4,7 +4,7 @@ from zipfile import ZIP_STORED, ZIP_DEFLATED def is_zipfile(filename): - return is_archive(filename, formats=('zip', )) + return is_archive(filename, formats=('zip',)) class ZipEntry(Entry): @@ -63,13 +63,15 @@ class ZipEntry(Entry): class ZipFile(SeekableArchive): def __init__(self, f, mode='r', compression=ZIP_DEFLATED, allowZip64=False, password=None): - super(ZipFile, self).__init__(f, mode=mode, format='zip', entry_class=ZipEntry, encoding='CP437', password=password) + super(ZipFile, self).__init__( + f, mode=mode, format='zip', entry_class=ZipEntry, encoding='CP437', password=password + ) if mode == 'w' and compression == ZIP_STORED: # Disable compression for writing. _libarchive.archive_write_set_format_option(self.archive._a, "zip", "compression", "store") self.compression = compression - getinfo = SeekableArchive.getentry + getinfo = SeekableArchive.getentry def namelist(self): return list(self.iterpaths()) diff --git a/tests.py b/tests.py index 8bc5894..bf6cb61 100644 --- a/tests.py +++ b/tests.py @@ -33,7 +33,7 @@ import io from libarchive import Archive, is_archive_name, is_archive from libarchive.zip import is_zipfile, ZipFile, ZipEntry -#PY3 = sys.version_info[0] == 3 +# PY3 = sys.version_info[0] == 3 PY3 = True TMPDIR = tempfile.mkdtemp(suffix='.python-libarchive') @@ -81,8 +81,8 @@ class TestIsArchiveZip(unittest.TestCase): def test_zip(self): self.assertEqual(is_archive(ZIPPATH), True) - self.assertEqual(is_archive(ZIPPATH, formats=('zip', )), True) - self.assertEqual(is_archive(ZIPPATH, formats=('tar', )), False) + self.assertEqual(is_archive(ZIPPATH, formats=('zip',)), True) + self.assertEqual(is_archive(ZIPPATH, formats=('tar',)), False) class TestIsArchiveTar(unittest.TestCase): @@ -112,7 +112,7 @@ class TestZipRead(unittest.TestCase): self.assertEqual(count, len(FILENAMES), 'Did not enumerate correct number of items in archive.') def test_deferred_close_by_archive(self): - """ Test archive deferred close without a stream. """ + """Test archive deferred close without a stream.""" z = ZipFile(self.f, 'r') self.assertIsNotNone(z._a) self.assertIsNone(z._stream) @@ -120,7 +120,7 @@ class TestZipRead(unittest.TestCase): self.assertIsNone(z._a) def test_deferred_close_by_stream(self): - """ Ensure archive closes self if stream is closed first. """ + """Ensure archive closes self if stream is closed first.""" z = ZipFile(self.f, 'r') stream = z.readstream(FILENAMES[0]) stream.close() @@ -132,8 +132,8 @@ class TestZipRead(unittest.TestCase): self.assertTrue(stream.closed) def test_close_stream_first(self): - """ Ensure that archive stays open after being closed if a stream is - open. Further, ensure closing the stream closes the archive. """ + """Ensure that archive stays open after being closed if a stream is + open. Further, ensure closing the stream closes the archive.""" z = ZipFile(self.f, 'r') stream = z.readstream(FILENAMES[0]) z.close() @@ -154,8 +154,8 @@ class TestZipRead(unittest.TestCase): names.append(e.filename) self.assertEqual(names, FILENAMES, 'File names differ in archive.') - #~ def test_non_ascii(self): - #~ pass + # ~ def test_non_ascii(self): + # ~ pass def test_extract_str(self): pass @@ -184,7 +184,7 @@ class TestZipWrite(unittest.TestCase): z.close() def test_writepath_directory(self): - """ Test writing a directory. """ + """Test writing a directory.""" z = ZipFile(self.f, 'w') z.writepath(None, pathname='/testdir', folder=True) z.writepath(None, pathname='/testdir/testinside', folder=True) @@ -238,7 +238,7 @@ class TestZipWrite(unittest.TestCase): z.close() def test_deferred_close_by_archive(self): - """ Test archive deferred close without a stream. """ + """Test archive deferred close without a stream.""" z = ZipFile(self.f, 'w') o = z.writestream(FILENAMES[0]) z.close() @@ -259,7 +259,7 @@ class TestHighLevelAPI(unittest.TestCase): make_temp_archive() def _test_listing_content(self, f): - """ Test helper capturing file paths while iterating the archive. """ + """Test helper capturing file paths while iterating the archive.""" found = [] with Archive(f) as a: for entry in a: @@ -268,16 +268,16 @@ class TestHighLevelAPI(unittest.TestCase): self.assertEqual(set(found), set(FILENAMES)) def test_open_by_name(self): - """ Test an archive opened directly by name. """ + """Test an archive opened directly by name.""" self._test_listing_content(ZIPPATH) def test_open_by_named_fobj(self): - """ Test an archive using a file-like object opened by name. """ + """Test an archive using a file-like object opened by name.""" with open(ZIPPATH, 'rb') as f: self._test_listing_content(f) def test_open_by_unnamed_fobj(self): - """ Test an archive using file-like object opened by fileno(). """ + """Test an archive using file-like object opened by fileno().""" with open(ZIPPATH, 'rb') as zf: with io.FileIO(zf.fileno(), mode='r', closefd=False) as f: self._test_listing_content(f)