def download(download_url, dst_path): '''Download whatever is located at ``download_url`` and store it at ``dst_path``. :param str dst_path: Absolute path to the download destination. The parent directory of the destination file *must* exist. ''' with closing(requests.get(download_url, stream=True)) as r: r.raise_for_status() unused, tempdst = tempfile.mkstemp() with open(tempdst, 'wb') as f: chunk_size = 1024 for chunk in r.iter_content(chunk_size): f.write(chunk) LOG.debug('Downloaded to tempdst: %r.', tempdst) try: shutil.move(tempdst, dst_path) # desired permissions are -rw-r--r perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH os.chmod(dst_path, perms) finally: delete_if_exists(tempdst)
def _save_index_file(self, name, data): LOG.debug('Save index file %r', name) path = self._index_path(name) if data: require_directory(os.path.dirname(path)) with open(path, 'w') as dst: json.dump(data, dst) else: delete_if_exists(path)
def cache_forget(self, namespace, keys=None): '''Remove entries for the given cache keys.''' if keys is None: keys = [k for k in self._cache_iter(namespace)] for key in keys: try: delete_if_exists(self._cache_path(namespace, key)) except Exception: LOG.error('Failed to delete cache %r of %r.', key, self.name)
def delete_subscription(self, name): '''Delete a single subscription.''' path = self._subscription_path(name) LOG.info('Delete subscription at %r.', path) try: os.unlink(path) except FileNotFoundError: pass delete_if_exists(self._index_path(name)) self.cache_forget(name)
def delete_local_files(self): '''Delete the local files for this episode (if they exist).''' while self.files: unused, unused_also, local_file = self.files.pop() if local_file: # filename may be empty or None delete_if_exists(local_file)