def download_directory(self, target, download_path): """ Downloads the target directory to the given path. The caller should ensure that the target is a directory. """ remote_file_uuid = self.open_tarred_gzipped_directory(target) with closing(RPCFileHandle(remote_file_uuid, self.proxy, finalize_on_close=True)) as fileobj: un_tar_directory(fileobj, download_path, 'gz')
def test_tar_empty(self): dir = tempfile.mkdtemp() self.addCleanup(lambda: remove_path(dir)) temp_dir = tempfile.mkdtemp() self.addCleanup(lambda: remove_path(temp_dir)) output_dir = os.path.join(temp_dir, 'output') un_tar_directory(tar_gzip_directory(dir), output_dir, 'gz') self.assertEquals(os.listdir(output_dir), [])
def download_directory(self, target, download_path): """ Downloads the target directory to the given path. The caller should ensure that the target is a directory. """ remote_file_uuid = self.open_tarred_gzipped_directory(target) with closing( RPCFileHandle(remote_file_uuid, self.proxy, finalize_on_close=True)) as fileobj: un_tar_directory(fileobj, download_path, 'gz')
def test_tar_has_files(self): dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'files') temp_dir = tempfile.mkdtemp() self.addCleanup(lambda: remove_path(temp_dir)) output_dir = os.path.join(temp_dir, 'output') un_tar_directory( tar_gzip_directory(dir, False, ['f2'], ['f1', 'b.txt']), output_dir, 'gz') output_dir_entries = os.listdir(output_dir) self.assertIn('dir1', output_dir_entries) self.assertIn('a.txt', output_dir_entries) self.assertNotIn('b.txt', output_dir_entries) self.assertTrue(os.path.exists(os.path.join(output_dir, 'dir1', 'f1'))) self.assertFalse(os.path.exists(os.path.join(output_dir, 'dir1', 'f2'))) self.assertTrue(os.path.islink(os.path.join(output_dir, 'a-symlink.txt')))
def unpack(ext, source, dest_path): """ Unpack the archive |source| to |dest_path|. Note: |source| can be a file handle or a path. |ext| contains the extension of the archive. """ if ext != '.zip': close_source = False try: if isinstance(source, basestring): source = open(source, 'rb') close_source = True if ext == '.tar.gz' or ext == '.tgz': un_tar_directory(source, dest_path, 'gz') elif ext == '.tar.bz2': un_tar_directory(source, dest_path, 'bz2') elif ext == '.gz': with open(dest_path, 'wb') as f: shutil.copyfileobj(un_gzip_stream(source), f) else: raise UsageError('Not an archive.') except (tarfile.TarError, IOError): raise UsageError('Invalid archive upload.') finally: if close_source: source.close() else: delete_source = False try: # unzip doesn't accept input from standard input, so we have to save # to a temporary file. if not isinstance(source, basestring): temp_path = dest_path + '.zip' with open(temp_path, 'wb') as f: shutil.copyfileobj(source, f) source = temp_path delete_source = True exitcode = subprocess.call( ['unzip', '-q', source, '-d', dest_path]) if exitcode != 0: raise UsageError('Invalid archive upload.') finally: if delete_source: path_util.remove(source)
def unpack(ext, source, dest_path): """ Unpack the archive |source| to |dest_path|. Note: |source| can be a file handle or a path. |ext| contains the extension of the archive. """ if ext != '.zip': close_source = False try: if isinstance(source, basestring): source = open(source, 'rb') close_source = True if ext == '.tar.gz' or ext == '.tgz': un_tar_directory(source, dest_path, 'gz') elif ext == '.tar.bz2': un_tar_directory(source, dest_path, 'bz2') elif ext == '.gz': with open(dest_path, 'wb') as f: shutil.copyfileobj(un_gzip_stream(source), f) else: raise UsageError('Not an archive.') except (tarfile.TarError, IOError): raise UsageError('Invalid archive upload.') finally: if close_source: source.close() else: delete_source = False try: # unzip doesn't accept input from standard input, so we have to save # to a temporary file. if not isinstance(source, basestring): temp_path = dest_path + '.zip' with open(temp_path, 'wb') as f: shutil.copyfileobj(source, f) source = temp_path delete_source = True exitcode = subprocess.call(['unzip', '-q', source, '-d', dest_path]) if exitcode != 0: raise UsageError('Invalid archive upload.') finally: if delete_source: path_util.remove(source)