def GetPackageWriter(output_dir=None, output_file=None, output_format='zip'): """Get an output writer for a package.""" if not (output_dir or output_file): raise ValueError( 'GetPackageWriter requires either output_dir or output_file') if output_dir and output_file: raise ValueError( 'GetPackageWriter requires only one of output_dir or output_file') if output_dir: package_writer = filesystem_library_package.FilesystemLibraryPackage( output_dir) else: out = open(output_file, 'w') if output_format == 'tgz': package_writer = tar_library_package.TarLibraryPackage(out) elif output_format == 'tar': package_writer = tar_library_package.TarLibraryPackage( out, compress=False) elif output_format == 'txt': package_writer = single_file_library_package.SingleFileLibraryPackage( out) else: package_writer = zip_library_package.ZipLibraryPackage(out) return package_writer
def testFileProperties(self): self.assertEquals('tgz', self._package.FileExtension()) self.assertEquals('application/x-gtar-compressed', self._package.MimeType()) uncompressed = tar_library_package.TarLibraryPackage( BytesIO(), compress=False) self.assertEquals('tar', uncompressed.FileExtension()) self.assertEquals('application/x-gtar', uncompressed.MimeType())
def testBasicWriteFileUncompressed(self): output_stream = BytesIO() package = tar_library_package.TarLibraryPackage( output_stream, compress=False) stream = package.StartFile(self._FILE_NAME) stream.write(self._FILE_CONTENTS) package.EndFile() package.DoneWritingArchive() # read it back and verify archive = tarfile.open(fileobj=BytesIO(output_stream.getvalue()), mode='r') info_list = archive.getmembers() self.assertEquals(1, len(info_list)) self.assertEquals(self._FILE_NAME, info_list[0].name) self.assertEquals(len(self._FILE_CONTENTS.encode('utf-8')), info_list[0].size)
def setUp(self): self._output_stream = BytesIO() self._package = tar_library_package.TarLibraryPackage( self._output_stream)