def _create_checksum_file(self, checksum, filename): compressed_blocks = None compress = Compress(self.source_filename) if compress.get_format(): compressed_blocks = self._block_list( os.path.getsize(self.source_filename) ) compress.uncompress(temporary=True) blocks = self._block_list( os.path.getsize(compress.uncompressed_filename) ) checksum = self._calculate_hash_hexdigest( hashlib.md5(), compress.uncompressed_filename ) else: blocks = self._block_list( os.path.getsize(self.source_filename) ) with open(filename, 'w') as checksum_file: if compressed_blocks: checksum_file.write( '%s %s %s %s %s\n' % ( checksum, blocks.blocks, blocks.blocksize, compressed_blocks.blocks, compressed_blocks.blocksize ) ) else: checksum_file.write( '%s %s %s\n' % ( checksum, blocks.blocks, blocks.blocksize ) )
def _create_checksum_file(self, checksum, filename): """ Creates the text file that contains the checksum :param str checksum: checksum to include into the file :param str filename: filename of the output file """ compressed_blocks = None compress = Compress(self.source_filename) if compress.get_format(): compressed_blocks = self._block_list( os.path.getsize(self.source_filename)) compress.uncompress(temporary=True) blocks = self._block_list( os.path.getsize(compress.uncompressed_filename)) checksum = self._calculate_hash_hexdigest( hashlib.md5(), compress.uncompressed_filename) else: blocks = self._block_list(os.path.getsize(self.source_filename)) with open(filename, encoding=self.ascii, mode='w') as checksum_file: if compressed_blocks: checksum_file.write( '%s %s %s %s %s\n' % (checksum, blocks.blocks, blocks.blocksize, compressed_blocks.blocks, compressed_blocks.blocksize)) else: checksum_file.write( '%s %s %s\n' % (checksum, blocks.blocks, blocks.blocksize))
def sync_data(self): """ Synchronize data from the given base image to the target root directory. """ if not self.unknown_uri: compressor = Compress(self.image_file) if compressor.get_format(): compressor.uncompress(True) self.uncompressed_image = compressor.uncompressed_filename else: self.uncompressed_image = self.image_file image_uri = '{0}:{1}'.format( self.archive_transport, self.uncompressed_image ) else: log.warning('Bypassing base image URI to OCI tools') image_uri = self.unknown_uri oci = OCI.new() oci.import_container_image(image_uri) oci.unpack() oci.import_rootfs(self.root_dir) # A copy of the uncompressed image and its checksum are # kept inside the root_dir in order to ensure the later steps # i.e. system create are atomic and don't need any third # party archive. image_copy = Defaults.get_imported_root_image(self.root_dir) Path.create(os.path.dirname(image_copy)) oci.export_container_image( image_copy, 'oci-archive', Defaults.get_container_base_image_tag() ) self._make_checksum(image_copy)
def extract_oci_image(self): """ Extract and converts to OCI the image from the provided image file to a temporary location to KIWI can work with it. """ if not self.unknown_uri: compressor = Compress(self.image_file) compressor.uncompress(True) self.uncompressed_image = compressor.uncompressed_filename skopeo_uri = 'docker-archive:{0}'.format(self.uncompressed_image) else: log.warning('Bypassing base image URI to skopeo tool') skopeo_uri = self.unknown_uri Command.run([ 'skopeo', 'copy', skopeo_uri, 'oci:{0}'.format(self.oci_layout_dir) ])
def extract_oci_image(self): """ Extract and converts to OCI the image from the provided image file to a temporary location to KIWI can work with it. """ if not self.unknown_uri: compressor = Compress(self.image_file) compressor.uncompress(True) self.uncompressed_image = compressor.uncompressed_filename skopeo_uri = 'docker-archive:{0}'.format(self.uncompressed_image) else: log.warning('Bypassing base image URI to skopeo tool') skopeo_uri = self.unknown_uri Command.run([ 'skopeo', 'copy', skopeo_uri, 'oci:{0}:base_layer'.format(self.oci_layout_dir) ])
def _create_checksum_file(self, checksum, filename): compressed_blocks = None compress = Compress(self.source_filename) if compress.get_format(): compressed_blocks = self._block_list( os.path.getsize(self.source_filename)) compress.uncompress(temporary=True) blocks = self._block_list( os.path.getsize(compress.uncompressed_filename)) checksum = self._calculate_hash_hexdigest( hashlib.md5(), compress.uncompressed_filename) else: blocks = self._block_list(os.path.getsize(self.source_filename)) with open(filename, 'w') as checksum_file: if compressed_blocks: checksum_file.write( '%s %s %s %s %s\n' % (checksum, blocks.blocks, blocks.blocksize, compressed_blocks.blocks, compressed_blocks.blocksize)) else: checksum_file.write( '%s %s %s\n' % (checksum, blocks.blocks, blocks.blocksize))
class TestCompress(object): @patch('os.path.exists') def setup(self, mock_exists): mock_exists.return_value = True self.compress = Compress('some-file', True) @raises(KiwiFileNotFound) def test_source_file_not_found(self): Compress('some-file') @patch('kiwi.command.Command.run') def test_xz(self, mock_command): self.compress.xz() mock_command.assert_called_once_with([ 'xz', '-f', '--check=crc32', '--lzma2=dict=512KiB', '--keep', 'some-file' ]) assert self.compress.compressed_filename == 'some-file.xz' @patch('kiwi.command.Command.run') def test_gzip(self, mock_command): self.compress.gzip() mock_command.assert_called_once_with( ['gzip', '-f', '-9', '--keep', 'some-file']) assert self.compress.compressed_filename == 'some-file.gz' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress(self, mock_format, mock_temp, mock_command): mock_format.return_value = 'xz' self.compress.uncompress() mock_command.assert_called_once_with(['xz', '-d', 'some-file']) assert self.compress.uncompressed_filename == 'some-file' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_temporary(self, mock_format, mock_temp, mock_command): tempfile = mock.Mock() tempfile.name = 'tempfile' mock_temp.return_value = tempfile mock_format.return_value = 'xz' self.compress.uncompress(temporary=True) mock_command.assert_called_once_with( ['bash', '-c', 'xz -c -d some-file > tempfile']) assert self.compress.uncompressed_filename == 'tempfile' @raises(KiwiCompressionFormatUnknown) @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_unknown_format(self, mock_format): mock_format.return_value = None self.compress.uncompress() def test_get_format(self): xz = Compress('../data/xz_data.xz') assert xz.get_format() == 'xz' gzip = Compress('../data/gz_data.gz') assert gzip.get_format() == 'gzip'
def _create_checksum_file(self, checksum, filename): """ Creates the text file that contains the checksum :param str checksum: checksum to include into the file :param str filename: filename of the output file """ compressed_blocks = None compress = Compress(self.source_filename) if compress.get_format(): compressed_blocks = self._block_list( os.path.getsize(self.source_filename) ) compress.uncompress(temporary=True) blocks = self._block_list( os.path.getsize(compress.uncompressed_filename) ) checksum = self._calculate_hash_hexdigest( hashlib.md5(), compress.uncompressed_filename ) else: blocks = self._block_list( os.path.getsize(self.source_filename) ) with open(filename, 'w') as checksum_file: if compressed_blocks: checksum_file.write( '%s %s %s %s %s\n' % ( checksum, blocks.blocks, blocks.blocksize, compressed_blocks.blocks, compressed_blocks.blocksize ) ) else: checksum_file.write( '%s %s %s\n' % ( checksum, blocks.blocks, blocks.blocksize ) )
class TestCompress(object): @patch('os.path.exists') def setup(self, mock_exists): mock_exists.return_value = True self.compress = Compress('some-file', True) @raises(KiwiFileNotFound) def test_source_file_not_found(self): Compress('some-file') @patch('kiwi.command.Command.run') def test_xz(self, mock_command): assert self.compress.xz() == 'some-file.xz' mock_command.assert_called_once_with( ['xz', '-f', '--threads=0', '--keep', 'some-file']) assert self.compress.compressed_filename == 'some-file.xz' @patch('kiwi.command.Command.run') def test_xz_with_custom_options(self, mock_command): assert self.compress.xz(options=['foo', 'bar']) == 'some-file.xz' mock_command.assert_called_once_with( ['xz', '-f', 'foo', 'bar', '--keep', 'some-file']) assert self.compress.compressed_filename == 'some-file.xz' @patch('kiwi.command.Command.run') def test_gzip(self, mock_command): assert self.compress.gzip() == 'some-file.gz' mock_command.assert_called_once_with( ['gzip', '-f', '-9', '--keep', 'some-file']) assert self.compress.compressed_filename == 'some-file.gz' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress(self, mock_format, mock_temp, mock_command): mock_format.return_value = 'xz' self.compress.uncompress() mock_command.assert_called_once_with(['xz', '-d', 'some-file']) assert self.compress.uncompressed_filename == 'some-file' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_temporary(self, mock_format, mock_temp, mock_command): tempfile = mock.Mock() tempfile.name = 'tempfile' mock_temp.return_value = tempfile mock_format.return_value = 'xz' self.compress.uncompress(temporary=True) mock_command.assert_called_once_with( ['bash', '-c', 'xz -c -d some-file > tempfile']) assert self.compress.uncompressed_filename == 'tempfile' @raises(KiwiCompressionFormatUnknown) @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_unknown_format(self, mock_format): mock_format.return_value = None self.compress.uncompress() @patch('kiwi.path.Path.which') def test_get_format(self, mock_which): mock_which.return_value = 'ziptool' xz = Compress('../data/xz_data.xz') assert xz.get_format() == 'xz' gzip = Compress('../data/gz_data.gz') assert gzip.get_format() == 'gzip' @patch('kiwi.logger.log.debug') @patch('kiwi.command.Command.run') def test_get_format_invalid_format(self, mock_run, mock_log): mock_run.side_effect = ValueError("nothing") invalid = Compress("../data/gz_data.gz") invalid.supported_zipper = ["mock_zip"] assert invalid.get_format() is None mock_run.assert_called_once_with( ['mock_zip', '-l', '../data/gz_data.gz']) mock_log.assert_called_once_with( 'Error running "mock_zip -l ../data/gz_data.gz", got a' ' ValueError: nothing')
class TestCompress(object): @patch('os.path.exists') def setup(self, mock_exists): mock_exists.return_value = True self.compress = Compress('some-file', True) @raises(KiwiFileNotFound) def test_source_file_not_found(self): Compress('some-file') @patch('kiwi.command.Command.run') def test_xz(self, mock_command): assert self.compress.xz() == 'some-file.xz' mock_command.assert_called_once_with( [ 'xz', '-f', '--threads=0', '--keep', 'some-file' ] ) assert self.compress.compressed_filename == 'some-file.xz' @patch('kiwi.command.Command.run') def test_xz_with_custom_options(self, mock_command): assert self.compress.xz(options=['foo', 'bar']) == 'some-file.xz' mock_command.assert_called_once_with( [ 'xz', '-f', 'foo', 'bar', '--keep', 'some-file' ] ) assert self.compress.compressed_filename == 'some-file.xz' @patch('kiwi.command.Command.run') def test_gzip(self, mock_command): assert self.compress.gzip() == 'some-file.gz' mock_command.assert_called_once_with( ['gzip', '-f', '-9', '--keep', 'some-file'] ) assert self.compress.compressed_filename == 'some-file.gz' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress(self, mock_format, mock_temp, mock_command): mock_format.return_value = 'xz' self.compress.uncompress() mock_command.assert_called_once_with( ['xz', '-d', 'some-file'] ) assert self.compress.uncompressed_filename == 'some-file' @patch('kiwi.command.Command.run') @patch('kiwi.utils.compress.NamedTemporaryFile') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_temporary(self, mock_format, mock_temp, mock_command): tempfile = mock.Mock() tempfile.name = 'tempfile' mock_temp.return_value = tempfile mock_format.return_value = 'xz' self.compress.uncompress(temporary=True) mock_command.assert_called_once_with( ['bash', '-c', 'xz -c -d some-file > tempfile'] ) assert self.compress.uncompressed_filename == 'tempfile' @raises(KiwiCompressionFormatUnknown) @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_unknown_format(self, mock_format): mock_format.return_value = None self.compress.uncompress() @patch('kiwi.path.Path.which') def test_get_format(self, mock_which): mock_which.return_value = 'ziptool' xz = Compress('../data/xz_data.xz') assert xz.get_format() == 'xz' gzip = Compress('../data/gz_data.gz') assert gzip.get_format() == 'gzip'