Skip to content
Browse files

Archive files can be open regardless of their extension

  • Loading branch information...
1 parent 3e1d8da commit 4dc33cf4d9bd43d70311d69a3f2f996015fb70aa @dvarrazzo committed
Showing with 92 additions and 11 deletions.
  1. +15 −9 pgxnclient/archive.py
  2. +3 −0 pgxnclient/tar.py
  3. +67 −0 pgxnclient/tests/test_archives.py
  4. +5 −2 pgxnclient/zip.py
  5. +1 −0 testdata/tar.ext
  6. +1 −0 testdata/zip.ext
View
24 pgxnclient/archive.py
@@ -21,15 +21,17 @@ def from_spec(spec):
def from_file(filename):
"""Return an `Archive` instance to handle the file *filename*
"""
- # Get the metadata from an archive file
- if filename.endswith('.zip'):
- from pgxnclient.zip import ZipArchive
- return ZipArchive(filename)
- else:
- # Tar files have many naming variants. Let's not
- # guess them.
- from pgxnclient.tar import TarArchive
- return TarArchive(filename)
+ from pgxnclient.zip import ZipArchive
+ from pgxnclient.tar import TarArchive
+
+ for cls in (ZipArchive, TarArchive):
+ a = cls(filename)
+ if a.can_open():
+ return a
+
+ raise PgxnClientException(
+ _("can't open archive '%s': file type not recognized")
+ % filename)
class Archive(object):
@@ -37,6 +39,10 @@ class Archive(object):
def __init__(self, filename):
self.filename = filename
+ def can_open(self):
+ """Return `!True` if the `!filename` can be opened by the obect."""
+ raise NotImplementedError
+
def open(self):
"""Open the archive for usage.
View
3 pgxnclient/tar.py
@@ -21,6 +21,9 @@ class TarArchive(Archive):
"""Handle .tar archives"""
_file = None
+ def can_open(self):
+ return tarfile.is_tarfile(self.filename)
+
def open(self):
assert not self._file, "archive already open"
try:
View
67 pgxnclient/tests/test_archives.py
@@ -0,0 +1,67 @@
+from pgxnclient import tar
+from pgxnclient import zip
+from pgxnclient import archive
+
+from pgxnclient.tests import unittest
+from pgxnclient.errors import PgxnClientException
+from pgxnclient.tests.testutils import get_test_filename
+
+class TestArchive(unittest.TestCase):
+ def test_from_file_zip(self):
+ fn = get_test_filename('foobar-0.42.1.zip')
+ a = archive.from_file(fn)
+ self.assert_(isinstance(a, zip.ZipArchive))
+ self.assertEqual(a.filename, fn)
+
+ def test_from_file_tar(self):
+ fn = get_test_filename('foobar-0.42.1.tar.gz')
+ a = archive.from_file(fn)
+ self.assert_(isinstance(a, tar.TarArchive))
+ self.assertEqual(a.filename, fn)
+
+ def test_from_file_unknown(self):
+ fn = get_test_filename('META-manyext.json')
+ self.assertRaises(PgxnClientException(archive.from_file, fn))
+
+
+class TestZipArchive(unittest.TestCase):
+ def test_can_open(self):
+ fn = get_test_filename('foobar-0.42.1.zip')
+ a = zip.ZipArchive(fn)
+ self.assert_(a.can_open())
+ a.open()
+ a.close()
+
+ def test_can_open_noext(self):
+ fn = get_test_filename('zip.ext')
+ a = zip.ZipArchive(fn)
+ self.assert_(a.can_open())
+ a.open()
+ a.close()
+
+ def test_cannot_open(self):
+ fn = get_test_filename('foobar-0.42.1.tar.gz')
+ a = zip.ZipArchive(fn)
+ self.assert_(not a.can_open())
+
+
+class TestTarArchive(unittest.TestCase):
+ def test_can_open(self):
+ fn = get_test_filename('foobar-0.42.1.tar.gz')
+ a = tar.TarArchive(fn)
+ self.assert_(a.can_open())
+ a.open()
+ a.close()
+
+ def test_can_open_noext(self):
+ fn = get_test_filename('tar.ext')
+ a = tar.TarArchive(fn)
+ self.assert_(a.can_open())
+ a.open()
+ a.close()
+
+ def test_cannot_open(self):
+ fn = get_test_filename('foobar-0.42.1.zip')
+ a = tar.TarArchive(fn)
+ self.assert_(not a.can_open())
+
View
7 pgxnclient/zip.py
@@ -8,7 +8,7 @@
import os
import stat
-from zipfile import ZipFile
+import zipfile
from pgxnclient.utils import b
from pgxnclient.i18n import _
@@ -24,10 +24,13 @@ class ZipArchive(Archive):
_file = None
+ def can_open(self):
+ return zipfile.is_zipfile(self.filename)
+
def open(self):
assert not self._file, "archive already open"
try:
- self._file = ZipFile(self.filename, 'r')
+ self._file = zipfile.ZipFile(self.filename, 'r')
except Exception, e:
raise PgxnClientException(
_("cannot open archive '%s': %s") % (self.filename, e))
View
1 testdata/tar.ext
View
1 testdata/zip.ext

0 comments on commit 4dc33cf

Please sign in to comment.
Something went wrong with that request. Please try again.