def download(self): # Emit a HTTP request. try: request = Request(self.remote_url, None, header(self.pivot_time)) response = urlopen(request) except HTTPError as e: response = e # Parse a HTTP response. try: if response.code == 304: # No updates. return False elif response.code == 200: # Found update, download it. call(self.progress, "begin_download") tmp = self.local_cache + ".download" self._save_as(response, tmp) self.clear() os.rename(tmp, self.local_cache) call(self.progress, "end_download") else: raise Exception("unexpected response: %d" % response.code) finally: if response: response.close() return True
def download(self): # Emit a HTTP request. try: request = Request(self.remote_url, None, header(self.pivot_time)) response = urlopen(request) except HTTPError as e: response = e # Parse a HTTP response. try: if response.code == 304: # No updates. return False elif response.code == 200: # Found update, download it. call(self.progress, 'begin_download') tmp = self.local_cache + '.download' self._save_as(response, tmp) self.clear() os.rename(tmp, self.local_cache) call(self.progress, 'end_download') else: raise Exception('unexpected response: %d' % response.code) finally: if response: response.close() return True
def _save_as(self, response, path): # Dig a directory if not exists. (dir, name) = os.path.split(path) if len(dir) > 0 and not os.path.exists(dir): os.makedirs(dir) # Write response body to a file. f = open(path, "wb") try: max = int(response.headers["Content-Length"]) value = 0 while True: chunk = response.read(io.DEFAULT_BUFFER_SIZE) if len(chunk) == 0: break f.write(chunk) value += len(chunk) call(self.progress, "do_download", value, max) finally: f.close()
def _save_as(self, response, path): # Dig a directory if not exists. (dir, name) = os.path.split(path) if len(dir) > 0 and not os.path.exists(dir): os.makedirs(dir) # Write response body to a file. f = open(path, 'wb') try: max = int(response.headers['Content-Length']) value = 0 while True: chunk = response.read(io.DEFAULT_BUFFER_SIZE) if len(chunk) == 0: break f.write(chunk) value += len(chunk) call(self.progress, 'do_download', value, max) finally: f.close()
def extractAll(self): call(self.progress, 'begin_extract') # Open database and check existing files. optimizer = ExtractionOptimizer(self.optimizeFile) optimizer.scanDir(self.unpackDir) # Open and read zip file. zipFile = ZipFile(self.zip, 'r') success = True try: # Register new files. for zipInfo in zipFile.infolist(): if Extractor.__isFile(zipInfo): fileInfo = FileInfo.fromZipInfo(zipInfo, 1) optimizer.registerFile(fileInfo) # Update files. extractor = RawExtractor(self.unpackDir, zipFile) for op in optimizer.operations(): success &= extractor.extract(op) call(self.progress, 'do_extract', optimizer.currentIndex, optimizer.maxSize) # Commit new fileset. if success: optimizer.commit() finally: zipFile.close() call(self.progress, 'end_extract') return success