[svn] Fix a small bug that would crash the program if an archive was empty. trunk

Sun, 29 Oct 2006 20:03:12 -0500

author
brett
date
Sun, 29 Oct 2006 20:03:12 -0500
branch
trunk
changeset 2
1570351bf863
parent 1
a86a0cb0dd57
child 3
5172456c3588

[svn] Fix a small bug that would crash the program if an archive was empty.

Make basename calculation more robust.

Go ahead and run .exe files through the ZipExtractor. Need to cope more
gracefully when it doesn't work, but I've lost momentum for tonight.

scripts/x file | annotate | diff | comparison | revisions
--- a/scripts/x	Sun Oct 29 19:34:46 2006 -0500
+++ b/scripts/x	Sun Oct 29 20:03:12 2006 -0500
@@ -116,7 +116,7 @@
         filenames = self.get_filenames()
         try:
             first_part = filenames.next().split('/', 1)[0] + '/'
-        except IndexError:
+        except StopIteration:
             filenames.stop()
             return EMPTY
         for filename in filenames:
@@ -130,7 +130,13 @@
 
     def basename(self):
         pieces = self.filename.split('.')
-        if mimetypes.encodings_map.has_key('.' + pieces.pop()):
+        extension = '.' + pieces[-1]
+        if mimetypes.encodings_map.has_key(extension):
+            pieces.pop()
+            extension = '.' + pieces[-1]
+        if (mimetypes.types_map.has_key(extension) or
+            mimetypes.common_types.has_key(extension) or
+            mimetypes.suffix_map.has_key(extension)):
             pieces.pop()
         return '.'.join(pieces)
 
@@ -177,7 +183,17 @@
         self.pipe(['rpm2cpio', '-'], "rpm2cpio")
 
     def basename(self):
-        return self.filename.rsplit('.', 2)[0]
+        pieces = self.filename.rsplit('.', 2)
+        if len(pieces) == 1:
+            return pieces[0]
+        elif pieces[-1] != 'rpm':
+            return BaseExtractor.basename(self)
+        pieces.pop()
+        if len(pieces) == 1:
+            return pieces[0]
+        elif len(pieces[-1]) < 6:
+            pieces.pop()
+        return '.'.join(pieces)
 
     def check_contents(self):
         return BOMB
@@ -191,7 +207,12 @@
         self.pipe(['zcat'], "data.tar.gz decompression")
 
     def basename(self):
-        return self.filename.rsplit('_', 1)[0]
+        pieces = self.filename.rsplit('_', 1)
+        if len(pieces) == 1:
+            return pieces[0]
+        elif (len(pieces[-1]) > 10) or (not pieces[-1].endswith('.deb')):
+            return BaseExtractor.basename(self)
+        return pieces[0]
 
     def check_contents(self):
         return BOMB
@@ -199,6 +220,7 @@
 
 extractor_map = {'application/x-tar': TarExtractor,
                  'application/zip': ZipExtractor,
+                 'application/x-msdos-program': ZipExtractor,
                  'application/x-debian-package': DebExtractor,
                  'application/x-redhat-package-manager': RPMExtractor,
                  'application/x-shar': None,

mercurial