def _download_tarball(self, tarball_url, filename): log.debug("Downloading from %s" % (tarball_url,)) response = urllib2.urlopen(tarball_url) blob = response.read() h = blob_hash(blob) self.dircache.store(blob, filename) return FileId.construct(self, self.dircache.get_file_path(filename), hash_ = h)
def _common_get(self, url, filename): ret = ServiceResult() with self.get_lock(filename): if self.dircache.is_available(filename): ret.result = FileId.construct(self, self.dircache.get_file_path(filename)) elif remote_exists(url): log.debug("Downloading from %s" % (url,)) response = urllib2.urlopen(url) blob = response.read() h = blob_hash(blob) self.dircache.store(blob, filename) ret.result = FileId.construct(self, self.dircache.get_file_path(filename), hash_ = h) else: raise KeyError("Desired file '%s' does not exist ( %s )" % (filename, url)) ret.meta['origin'] = url return ret
def upload(self, blob): ''' Upload file to the system @param blob: a file content to be store @return: file id ''' res = ServiceResult() log.info("uploading") h = blob_hash(blob) dst = os.path.join(self.upload_dir, h) with self.get_lock(): with open(dst, 'wb') as f: f.write(blob) creation_time = datetime_parse(time.ctime(os.path.getctime(dst))) # TODO: remove, use dircache instead valid_until = creation_time + self.file_lifetime res.result = FileId.construct(self, dst, h) return res