Skip to content

Commit 5c36c6e

Browse files
Merge pull request #30 from xenserver-next/remove-always-broken-_XZProxy
Fix pytype and pylint errors from xcp.cpiofile.CpioFile._XZProxy()
2 parents 123db8f + e776886 commit 5c36c6e

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

tests/test_cpio.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import print_function
22
from hashlib import md5
3+
import lzma
34
import os
45
import sys
56
import shutil
67
import subprocess
78
import unittest
89
import warnings
910

11+
import xcp.cpiofile
1012
from xcp.cpiofile import CpioFile, CpioFileCompat, CPIO_PLAIN, CPIO_GZIPPED
1113

1214
def writeRandomFile(fn, size, start=b'', add=b'a'):
@@ -113,9 +115,9 @@ def doArchive(self, fn, fmt=None):
113115
self.archiveExtract(fn)
114116
fn2 = "archive2" + fn[len("archive"):]
115117
print("creating %s" % fn2)
116-
self.archiveCreate(fn2, fmt is None and 'w' or 'w|%s' % fmt)
118+
self.archiveCreate(fn2, fmt is None and "w" or "w:%s" % fmt)
117119
if fmt is not None:
118-
self.archiveExtract(fn2, 'r|%s' % fmt)
120+
self.archiveExtract(fn2, "r:%s" % fmt)
119121

120122
def test_plain(self):
121123
self.doArchive('archive.cpio')
@@ -132,6 +134,13 @@ def test_xz(self):
132134
print('Running test for XZ')
133135
self.doArchive('archive.cpio.xz', 'xz')
134136

137+
def test_cover_xzopen_pass_fileobj(self):
138+
"""Cover CpioFile.xzopen() not supporting receiving a fileobj argument"""
139+
class MockCpioFile(CpioFile):
140+
def __init__(self): # pylint: disable=super-init-not-called
141+
pass
142+
with self.assertRaises(xcp.cpiofile.CompressionError):
143+
MockCpioFile.xzopen(name="", mode="r", fileobj=lzma.LZMAFile("/dev/null"))
135144
# CpioFileCompat testing
136145

137146
def archiveExtractCompat(self, fn, comp):

tox.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ setenv =
7878
{[cov]setenv}
7979
commands =
8080
{cov,covcp,covcombine,check,fox,lint,test,pytype,mdreport}: {[test]commands}
81-
{cov,covcp,covcombine}: {[cov]commands}
81+
# covcombine shall not call [cov]commands: diff-cover shall check the combined cov:
82+
{cov,covcp}: {[cov]commands}
8283
{py27-test}: pylint --py3k --disable=no-absolute-import xcp/
8384
covcp: cp -av {envlogdir}/coverage.xml {env:UPLOAD_DIR:.}
8485
covcombine: {[covcombine]commands}

xcp/cpiofile.py

+8-27
Original file line numberDiff line numberDiff line change
@@ -572,27 +572,6 @@ def init(self):
572572

573573
# class _BZ2Proxy
574574

575-
class _XZProxy(_CMPProxy):
576-
"""Small proxy class that enables external file object
577-
support for "r:xz" and "w:xz" modes.
578-
"""
579-
580-
def __init__(self, fileobj, mode):
581-
_CMPProxy.__init__(self, fileobj, mode)
582-
self.init()
583-
584-
def init(self):
585-
import lzma
586-
self.pos = 0
587-
if self.mode == "r":
588-
self.cmpobj = lzma.BZ2Decompressor()
589-
self.fileobj.seek(0)
590-
self.buf = b""
591-
else:
592-
self.cmpobj = lzma.BZ2Compressor()
593-
594-
# class _XZProxy
595-
596575

597576
#------------------------
598577
# Extraction file object
@@ -1162,11 +1141,13 @@ def xzopen(cls, name, mode="r", fileobj=None, compresslevel=6):
11621141
raise CompressionError("lzma module is not available")
11631142

11641143
if fileobj is not None:
1165-
fileobj = _XZProxy(fileobj, mode)
1166-
else:
1167-
# FIXME: not compatible with python3 API
1168-
fileobj = lzma.LZMAFile(name, mode, options={'level': compresslevel, 'dict_size': 20 })
1169-
1144+
raise CompressionError("passing fileobj not implemented for LZMA")
1145+
kwargs = {}
1146+
if sys.version_info < (3, 0):
1147+
kwargs["options"] = {"level": compresslevel}
1148+
elif "w" in mode:
1149+
kwargs["preset"] = compresslevel
1150+
fileobj = lzma.LZMAFile(name, mode, **kwargs)
11701151
try:
11711152
t = cls.cpioopen(name, mode, fileobj)
11721153
except IOError:
@@ -1179,7 +1160,7 @@ def xzopen(cls, name, mode="r", fileobj=None, compresslevel=6):
11791160
"cpio": "cpioopen", # uncompressed cpio
11801161
"gz": "gzopen", # gzip compressed cpio
11811162
"bz2": "bz2open", # bzip2 compressed cpio
1182-
"xz": "xzopen " # xz compressed cpio
1163+
"xz": "xzopen", # xz compressed cpio
11831164
}
11841165

11851166
#--------------------------------------------------------------------------

0 commit comments

Comments
 (0)