def fill(self, package_dict): '''The package dict looks like this: name: <name> version: <version> pkg_license: <package license string> copyright: <package copyright text> proj_url: <project url> files: <package files> the way to use this method is to instantiate the class with the name and then give it a package dictionary to fill in the rest return true if package name is the same as the one used to instantiate the object, false if not''' raw_file_list = package_dict.get('files', False) if raw_file_list: file_list = [] for file_path in raw_file_list.split('\n'): if file_path != '': file_name = os.path.split(file_path)[1] fd = FileData(file_name, file_path) fd.fill({'name': file_name, 'path': file_path}) file_list.append(fd) package_dict['files'] = file_list success = True if self.name == package_dict['name']: self.__fill_properties(package_dict) else: success = False return success
def fill(self, package_dict): '''The package dict looks like this: name: <name> version: <version> pkg_license: <package license string> copyright: <package copyright text> proj_url: <project url> files: <package files> the way to use this method is to instantiate the class with the name and then give it a package dictionary to fill in the rest return true if package name is the same as the one used to instantiate the object, false if not''' if self.name == package_dict['name']: # update the package dictionary with file info file_list = package_dict.get('files', False) if file_list: fd_list = [] for file_dict in file_list: fd = FileData(file_dict['name'], file_dict['path']) fd.fill(file_dict) fd_list.append(fd) package_dict['files'] = fd_list self.__fill_properties(package_dict) return True return False
def testFill(self): f_dict1 = {'name': 'a.txt', 'path': '/usr/a.txt'} f_dict2 = {'name': 'b.txt', 'path': '/lib/b.txt'} fd1 = FileData(f_dict1['name'], f_dict1['path']) fd2 = FileData(f_dict2['name'], f_dict2['path']) fd1.fill(f_dict1) fd2.fill(f_dict2) p_dict = {'name': 'p1', 'version': '1.0', 'pkg_license': 'Apache 2.0', 'checksum': 'abcxyz', 'pkg_licenses': ['MIT', 'GPL'], 'files': '/usr/a.txt\n/lib/b.txt'} p = Package('p1') p.fill(p_dict) self.assertEqual(p.name, 'p1') self.assertEqual(p.version, '1.0') self.assertEqual(p.pkg_license, 'Apache 2.0') self.assertEqual(p.checksum, 'abcxyz') self.assertEqual(p.pkg_licenses, ['MIT', 'GPL']) self.assertTrue(fd1.is_equal(p.files[0])) self.assertTrue(fd2.is_equal(p.files[1])) self.assertFalse(p.copyright) self.assertFalse(p.proj_url) self.assertEqual(len(p.origins.origins), 1) self.assertEqual(p.origins.origins[0].origin_str, 'p1') self.assertEqual(len(p.origins.origins[0].notices), 3) self.assertEqual(p.origins.origins[0].notices[0].message, "No metadata for key: copyright") self.assertEqual(p.origins.origins[0].notices[0].level, 'warning') self.assertEqual(p.origins.origins[0].notices[1].message, "No metadata for key: proj_url") self.assertEqual(p.origins.origins[0].notices[2].message, "No metadata for key: download_url")
def create_image_layer(report): """Given a report file, create an ImageLayer object with the metadata""" # expect a json input, raise an error if it is not content = {} try: with open(os.path.abspath(report), encoding='utf-8') as f: content = json.load(f) except OSError as err: logger.critical("Cannot access file %s: %s", report, err) raise ConsumerError(f"Error with given report file: {report}") from err except json.JSONDecodeError as err: logger.critical("Cannot parse JSON in file %s: %s", report, err) raise ConsumerError(f"Error with given report file: {report}") from err # we should have some content but it may be empty if not content: raise ConsumerError("No content consumed from given report file") # instantiate a layer and fill it layer = ImageLayer("") try: layer.os_guess = content['os_guess'] for pkg in content['packages']: pkg_obj = Package(pkg['name']) pkg_obj.fill(pkg) layer.add_package(pkg_obj) for filedict in content['files']: file_obj = FileData(filedict['name'], filedict['path']) file_obj.fill(filedict) layer.add_file(file_obj) return layer except ValueError as err: logger.critical("Cannot find required data in report: %s", err) return None
def load_files_from_cache(layer): '''Given a layer object, populate file level information''' loaded = False raw_file_list = cache.get_files(layer.fs_hash) if raw_file_list: logger.debug('Loading files from cache: layer \"%s\"', layer.fs_hash[:10]) for file_dict in raw_file_list: f = FileData(file_dict['name'], file_dict['path']) f.fill(file_dict) # collect file origins if 'origins' in file_dict.keys(): for origin_dict in file_dict['origins']: for notice in origin_dict['notices']: f.origins.add_notice_to_origins( origin_dict['origin_str'], Notice(notice['message'], notice['level'])) layer.add_file(f) loaded = True else: # if there are no files, generate them from the pre-calculated # hash file logger.debug('Reading files in filesystem...') layer.add_files() return loaded
def testFill(self): file_dict = { 'name': 'zconf.h', 'path': '/usr/include/zconf.h', 'checksum_type': 'sha256', 'checksum': '77304005ceb5f0d03ad4c37eb8386a10866e' '4ceeb204f7c3b6599834c7319541', 'extattrs': '-rw-r--r-- 1 1000 1000 16262 Nov 13 17:57' ' /usr/include/zconf.h' } f = FileData('zconf.h', '/usr/include/zconf.h') f.fill(file_dict) self.assertEqual(f.name, 'zconf.h') self.assertEqual(f.path, '/usr/include/zconf.h') self.assertEqual(f.checksum_type, 'sha256') self.assertEqual( f.checksum, '77304005ceb5f0d03ad4c37eb838' '6a10866e4ceeb204f7c3b6599834c7319541') self.assertEqual( f.extattrs, '-rw-r--r-- 1 1000 1000 ' '16262 Nov 13 17:57 /usr/include/zconf.h') self.assertEqual(f.origins.origins[0].notices[1].message, 'No metadata for key: file_type') self.assertEqual(f.origins.origins[0].notices[1].level, 'warning') self.assertEqual(f.origins.origins[0].notices[0].message, 'No metadata for key: date') self.assertEqual(f.origins.origins[0].notices[2].message, 'No metadata for key: version_control')
def load_files_from_cache(layer): '''Given a layer object, populate file level information''' raw_file_list = cache.get_files(layer.fs_hash) if raw_file_list: logger.debug('Loading files from cache: layer \"%s\"', layer.fs_hash[:10]) for file_dict in raw_file_list: f = FileData(file_dict['name'], file_dict['path']) f.fill(file_dict) # collect file origins if 'origins' in file_dict.keys(): for origin_dict in file_dict['origins']: for notice in origin_dict['notices']: f.origins.add_notice_to_origins( origin_dict['origin_str'], Notice(notice['message'], notice['level'])) layer.add_file(f)
def testFill(self): file_dict = { 'name': 'zconf.h', 'path': '/usr/include/zconf.h', 'checksum_type': 'sha256', 'checksum': '77304005ceb5f0d03ad4c37eb8386a10866e' '4ceeb204f7c3b6599834c7319541', 'extattrs': '-rw-r--r-- 1 1000 1000 16262 Nov 13 17:57' ' /usr/include/zconf.h', 'checksums': { 'sha1': '12345abcde', 'md5': '1ff38cc592c4c5d0c8e3ca38be8f1eb1' } } f = FileData('zconf.h', '/usr/include/zconf.h') f.fill(file_dict) self.assertEqual(f.name, 'zconf.h') self.assertEqual(f.path, '/usr/include/zconf.h') self.assertEqual(f.checksum_type, 'sha256') self.assertEqual( f.checksum, '77304005ceb5f0d03ad4c37eb838' '6a10866e4ceeb204f7c3b6599834c7319541') self.assertEqual( f.extattrs, '-rw-r--r-- 1 1000 1000 ' '16262 Nov 13 17:57 /usr/include/zconf.h') self.assertEqual(f.checksums, { 'sha1': '12345abcde', 'md5': '1ff38cc592c4c5d0c8e3ca38be8f1eb1' }) self.assertEqual(f.origins.origins[0].notices[0].message, 'No metadata for key: date') self.assertEqual(f.origins.origins[0].notices[1].message, 'No metadata for key: file_type') self.assertEqual(f.origins.origins[0].notices[1].level, 'warning') self.assertEqual(f.origins.origins[0].notices[2].message, 'No metadata for key: short_file_type')