def test_load(self, mock_exists, mock_pickle_load): mock_exists.return_value = True m_open = mock_open() with patch('builtins.open', m_open, create=True): Result.load('kiwi.result') m_open.assert_called_once_with('kiwi.result', 'rb') mock_pickle_load.assert_called_once_with(m_open.return_value)
def test_load(self, mock_open, mock_exists, mock_pickle_load): mock_open.return_value = self.context_manager_mock mock_exists.return_value = True Result.load('kiwi.result') mock_open.assert_called_once_with( 'kiwi.result', 'rb' ) mock_pickle_load.assert_called_once_with( self.file_mock )
def process(self): """ List result information from a previous system command """ self.manual = Help() if self._help(): return result_directory = os.path.abspath(self.command_args['--target-dir']) log.info('Listing results from %s', result_directory) result = Result.load(result_directory + '/kiwi.result') result.print_results()
def process(self): """ List result information from a previous system command """ self.manual = Help() if self._help(): return result_directory = os.path.abspath( self.command_args['--target-dir'] ) log.info( 'Listing results from %s', result_directory ) result = Result.load( result_directory + '/kiwi.result' ) result.print_results()
def process(self): """ Create result bundle from the image build results in the specified target directory. Each result image will contain the specified bundle identifier as part of its filename. Uncompressed image files will also become xz compressed and a sha sum will be created from every result image """ self.manual = Help() if self._help(): return # load serialized result object from target directory result_directory = os.path.abspath(self.command_args['--target-dir']) bundle_directory = os.path.abspath(self.command_args['--bundle-dir']) if result_directory == bundle_directory: raise KiwiBundleError( 'Bundle directory must be different from target directory' ) log.info( 'Bundle build results from %s', result_directory ) result = Result.load( result_directory + '/kiwi.result' ) image_version = result.xml_state.get_image_version() ordered_results = OrderedDict(sorted(result.get_results().items())) # hard link bundle files, compress and build checksum if not os.path.exists(bundle_directory): Path.create(bundle_directory) for result_file in list(ordered_results.values()): if result_file.use_for_bundle: bundle_file_basename = os.path.basename(result_file.filename) # The bundle id is only taken into account for image results # which contains the image version in its nane bundle_file_basename = bundle_file_basename.replace( image_version, image_version + '-' + self.command_args['--id'] ) log.info('Creating %s', bundle_file_basename) bundle_file = ''.join( [bundle_directory, '/', bundle_file_basename] ) checksum_file = ''.join( [bundle_directory, '/', bundle_file_basename, '.sha256'] ) Command.run( [ 'cp', result_file.filename, bundle_file ] ) if result_file.compress: log.info('--> XZ compressing') compress = Compress(bundle_file) compress.xz(self.runtime_config.get_xz_options()) bundle_file = compress.compressed_filename checksum_file = compress.compressed_filename + '.sha256' if self.command_args['--zsync-source']: zsyncmake = Path.which('zsyncmake', access_mode=os.X_OK) if zsyncmake: log.info('--> Creating zsync control file') Command.run( [ zsyncmake, '-e', '-u', os.sep.join( [ self.command_args['--zsync-source'], os.path.basename(bundle_file) ] ), '-o', bundle_file + '.zsync', bundle_file ] ) else: log.warning( '--> zsyncmake missing, zsync setup skipped' ) if result_file.shasum: log.info('--> Creating SHA 256 sum') checksum = Checksum(bundle_file) with open(checksum_file, 'w') as shasum: shasum.write(checksum.sha256())
def test_load_failed(self, mock_exists, mock_pickle_load): mock_exists.return_value = True mock_pickle_load.side_effect = Exception Result.load('kiwi.result')
def test_load_result_not_present(self, mock_exists): mock_exists.return_value = False Result.load('kiwi.result')
def test_load_result_not_present(self, mock_exists): mock_exists.return_value = False with raises(KiwiResultError): Result.load('kiwi.result')
def process(self): """ Create result bundle from the image build results in the specified target directory. Each result image will contain the specified bundle identifier as part of its filename. Uncompressed image files will also become xz compressed and a sha sum will be created from every result image """ self.manual = Help() if self._help(): return # load serialized result object from target directory result_directory = os.path.abspath(self.command_args['--target-dir']) bundle_directory = os.path.abspath(self.command_args['--bundle-dir']) if result_directory == bundle_directory: raise KiwiBundleError( 'Bundle directory must be different from target directory') log.info('Bundle build results from %s', result_directory) result = Result.load(result_directory + '/kiwi.result') image_version = result.xml_state.get_image_version() image_name = result.xml_state.xml_data.get_name() ordered_results = OrderedDict(sorted(result.get_results().items())) # hard link bundle files, compress and build checksum if not os.path.exists(bundle_directory): Path.create(bundle_directory) for result_file in list(ordered_results.values()): if result_file.use_for_bundle: bundle_file_basename = os.path.basename(result_file.filename) # The bundle id is only taken into account for image results # which contains the image version appended in its file name part_name = list(bundle_file_basename.partition(image_name)) bundle_file_basename = ''.join([ part_name[0], part_name[1], part_name[2].replace( image_version, image_version + '-' + self.command_args['--id']) ]) log.info('Creating %s', bundle_file_basename) bundle_file = ''.join( [bundle_directory, '/', bundle_file_basename]) Command.run(['cp', result_file.filename, bundle_file]) if result_file.compress: log.info('--> XZ compressing') compress = Compress(bundle_file) compress.xz(self.runtime_config.get_xz_options()) bundle_file = compress.compressed_filename if self.command_args['--zsync-source'] and result_file.shasum: # Files with a checksum are considered to be image files # and are therefore eligible to be provided via the # requested Partial/differential file download based on # zsync zsyncmake = Path.which('zsyncmake', access_mode=os.X_OK) if zsyncmake: log.info('--> Creating zsync control file') Command.run([ zsyncmake, '-e', '-u', os.sep.join([ self.command_args['--zsync-source'], os.path.basename(bundle_file) ]), '-o', bundle_file + '.zsync', bundle_file ]) else: log.warning( '--> zsyncmake missing, zsync setup skipped') if result_file.shasum: log.info('--> Creating SHA 256 sum') checksum = Checksum(bundle_file) with open(bundle_file + '.sha256', 'w') as shasum: shasum.write('{0} {1}'.format( checksum.sha256(), os.path.basename(bundle_file)))
def process(self): """ Create result bundle from the image build results in the specified target directory. Each result image will contain the specified bundle identifier as part of its filename. Uncompressed image files will also become xz compressed and a sha sum will be created from every result image """ self.manual = Help() if self._help(): return if self.command_args['--package-as-rpm']: Privileges.check_for_root_permissions() # load serialized result object from target directory result_directory = os.path.abspath(self.command_args['--target-dir']) bundle_directory = os.path.abspath(self.command_args['--bundle-dir']) if result_directory == bundle_directory: raise KiwiBundleError( 'Bundle directory must be different from target directory') log.info('Bundle build results from %s', result_directory) result = Result.load(result_directory + '/kiwi.result') image_version = result.xml_state.get_image_version() image_name = result.xml_state.xml_data.get_name() image_description = result.xml_state.get_description_section() ordered_results = OrderedDict(sorted(result.get_results().items())) # hard link bundle files, compress and build checksum if self.command_args['--package-as-rpm']: Path.wipe(bundle_directory) if not os.path.exists(bundle_directory): Path.create(bundle_directory) bundle_file_format_name = '' if 'bundle_format' in ordered_results: bundle_format = ordered_results['bundle_format'] tags = bundle_format['tags'] bundle_file_format_name = bundle_format['pattern'] # Insert image name bundle_file_format_name = bundle_file_format_name.replace( '%N', tags.N) # Insert Concatenated profile name (_) bundle_file_format_name = bundle_file_format_name.replace( '%P', tags.P) # Insert Architecture name bundle_file_format_name = bundle_file_format_name.replace( '%A', tags.A) # Insert Image build type name bundle_file_format_name = bundle_file_format_name.replace( '%T', tags.T) # Insert Image Major version number bundle_file_format_name = bundle_file_format_name.replace( '%M', format(tags.M)) # Insert Image Minor version number bundle_file_format_name = bundle_file_format_name.replace( '%m', format(tags.m)) # Insert Image Patch version number bundle_file_format_name = bundle_file_format_name.replace( '%p', format(tags.p)) # Insert Bundle ID bundle_file_format_name = bundle_file_format_name.replace( '%I', self.command_args['--id']) del (ordered_results['bundle_format']) for result_file in list(ordered_results.values()): if result_file.use_for_bundle: extension = result_file.filename.split('.').pop() if bundle_file_format_name: bundle_file_basename = '.'.join( [bundle_file_format_name, extension]) else: bundle_file_basename = os.path.basename( result_file.filename) # The bundle id is only taken into account for image results # which contains the image version appended in its file name part_name = list( bundle_file_basename.partition(image_name)) bundle_file_basename = ''.join([ part_name[0], part_name[1], part_name[2].replace( image_version, image_version + '-' + self.command_args['--id']) ]) log.info('Creating %s', bundle_file_basename) bundle_file = ''.join( [bundle_directory, '/', bundle_file_basename]) Command.run(['cp', result_file.filename, bundle_file]) if result_file.compress: log.info('--> XZ compressing') compress = Compress(bundle_file) compress.xz(self.runtime_config.get_xz_options()) bundle_file = compress.compressed_filename if self.command_args['--zsync-source'] and result_file.shasum: # Files with a checksum are considered to be image files # and are therefore eligible to be provided via the # requested Partial/differential file download based on # zsync zsyncmake = Path.which('zsyncmake', access_mode=os.X_OK) if zsyncmake: log.info('--> Creating zsync control file') Command.run([ zsyncmake, '-e', '-u', os.sep.join([ self.command_args['--zsync-source'], os.path.basename(bundle_file) ]), '-o', bundle_file + '.zsync', bundle_file ]) else: log.warning( '--> zsyncmake missing, zsync setup skipped') if result_file.shasum: log.info('--> Creating SHA 256 sum') checksum = Checksum(bundle_file) with open(bundle_file + '.sha256', 'w') as shasum: shasum.write('{0} {1}{2}'.format( checksum.sha256(), os.path.basename(bundle_file), os.linesep)) if self.command_args['--package-as-rpm']: ResultBundleTask._build_rpm_package( bundle_directory, bundle_file_format_name or image_name, image_version, image_description.specification, list(glob.iglob(f'{bundle_directory}/*')))
def main(): """ main-entry point for program """ image = {} if 'TOPDIR' not in os.environ: image['topdir'] = '/usr/src/packages' image['image_dir'] = '{0}/KIWI-docker'.format(image['topdir']) image['bundle_dir'] = '{0}/KIWI'.format(image['topdir']) image['sources'] = '{0}/SOURCES'.format(image['topdir']) image['build_dir'] = '/usr/lib/build' if 'BUILD_DIST' not in os.environ: raise Exception('Not building inside build service') if not os.path.exists('{0}/kiwi.result'.format(image['image_dir'])): raise Exception('Kiwi result file not found') image['build_data'] = os.environ['BUILD_DIST'].replace('.dist', '.data') if not os.path.exists(image['build_data']): print( "Data file {0} not found. Skipping metadata package build".format( image['build_data'])) return result = Result.load('{0}/kiwi.result'.format(image['image_dir'])) if result.xml_state.build_type.get_image() != 'docker': # Just leave if the image type is not docker return build_data = variable_file_parser(image['build_data']) image['version'] = result.xml_state.get_image_version() image['name'] = result.xml_state.xml_data.get_name() image['release'] = build_data['RELEASE'] image['arch'] = build_data['BUILD_ARCH'].partition(':')[0] image['disturl'] = build_data['DISTURL'] image['kiwi_file'] = '{0}/{1}'.format(image['sources'], build_data['RECIPEFILE']) image['references'] = get_image_references(result, image['kiwi_file'], image['version'], image['release']) image['description'] = 'Referencing metadata for {0} image'.format( image['name']) image['url'] = 'https://github.com/kubic-project/containermetadata-rpm' make_spec_from_template( image, '{0}/{1}-metadata.spec'.format(image['build_dir'], image['name'])) with open('{0}/{1}-metadata'.format(image['sources'], image['name']), 'w') as metadata: json.dump(image['references'], metadata) rpmbuild = ['rpmbuild', '--target', image['arch'], '-ba'] if 'disturl' in image: rpmbuild.extend(['--define', 'disturl {0}'.format(image['disturl'])]) rpmbuild.append('{0}/{1}-metadata.spec'.format(image['build_dir'], image['name'])) run_command(rpmbuild) shutil.move( '{0}/RPMS/{1}/{2}-metadata-{3}-{4}.{1}.rpm'.format( image['topdir'], image['arch'], image['name'], image['version'], image['release']), '{0}/OTHER'.format(image['topdir'])) shutil.move( '{0}/SRPMS/{1}-metadata-{2}-{3}.src.rpm'.format( image['topdir'], image['name'], image['version'], image['release']), '{0}/OTHER'.format(image['topdir']))