Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MB-6606: Implement extractall function for python 2.4

ZipFile.extractall() only exists since python 2.6. For platforms with
older python version installed, we have our own implementation to
extract all zip content to a working directory.

Change-Id: I67e3cd7bbd540e62d0088444e273788ff7360623
Reviewed-on: http://review.couchbase.org/20765
Tested-by: Bin Cui <bin.cui@gmail.com>
Reviewed-by: Steve Yen <steve.yen@gmail.com>
Reviewed-by: Farshid Ghods <farshid@couchbase.com>
Tested-by: Farshid Ghods <farshid@couchbase.com>
  • Loading branch information...
commit 5626f006609be54b040d697fd5b390cc702b680a 1 parent 2284fc5
@bcui6611 bcui6611 authored Farshid Ghods committed
Showing with 55 additions and 2 deletions.
  1. +55 −2 cbdocloader
View
57 cbdocloader
@@ -15,6 +15,52 @@ from optparse import OptionParser
from couchbase import client
from couchbase.rest_client import RestConnection, RestHelper
+class ZipUtil:
+ def __init__(self, zipobj):
+ self.zipobj = zipobj
+
+ def extractall(self, path=None):
+ if path is None:
+ path = os.getcwd()
+ if (path[-1] in (os.path.sep, os.path.altsep)
+ and len(os.path.splitdrive(path)[1]) > 1):
+ path = path[:-1]
+
+ for member in self.zipobj.namelist():
+ if not isinstance(member, zipfile.ZipInfo):
+ member = self.zipobj.getinfo(member)
+
+ # don't include leading "/" from file name if present
+ if member.filename[0] == '/':
+ targetpath = os.path.join(path, member.filename[1:])
+ else:
+ targetpath = os.path.join(path, member.filename)
+
+ targetpath = os.path.normpath(targetpath)
+
+ # Create all parent directories if necessary.
+ upperdirs = os.path.dirname(targetpath)
+ if upperdirs and not os.path.exists(upperdirs):
+ try:
+ os.makedirs(upperdirs)
+ except:
+ print "Unexpected error:", sys.exc_info()[0]
+ return upperdirs
+
+ if member.filename[-1] == '/':
+ if not os.path.isdir(targetpath):
+ try:
+ os.mkdir(targetpath)
+ except:
+ print "Fail to create directory:", targetpath
+ continue
+
+ target = file(targetpath, "wb")
+ target.write(self.zipobj.read(member.filename))
+ target.close()
+
+ return path
+
class DocLoader:
def parse_args(self):
@@ -122,9 +168,16 @@ class DocLoader:
tmpdir = tempfile.gettempdir()
working_dir = os.path.join(tmpdir, '_working')
if not os.path.exists(working_dir):
- os.makedirs(working_dir)
+ try:
+ os.makedirs(working_dir)
+ except:
+ print "Unexpected error:", sys.exc_info()[0]
+ return
+ if sys.version >= "2.6":
+ zfobj.extractall(working_dir)
+ else:
+ ZipUtil(zfobj).extractall(working_dir)
- zfobj.extractall(working_dir)
self.enumerate_and_save(working_dir)
shutil.rmtree(working_dir)
Please sign in to comment.
Something went wrong with that request. Please try again.