def set_file_name(self, doc, name): """Raises OrderError if no package defined. """ if self.has_package(doc): doc.package.files.append(file.File(name)) # A file name marks the start of a new file instance. # The builder must be reset self.reset_file_stat() return True else: raise OrderError('File::Name')
def create_files(scope_token: str, licenses_dict: dict) -> tuple: # filter set to contain only a single of SPDXNone and NOASSERT def filter_none_types(in_set): out_set = set() no_assert_in_set = False spdx_none_int_set = False for ent in in_set: ent_is_no_assert = isinstance(ent, NoAssert) ent_is_spdx_none = isinstance(ent, SPDXNone) if not no_assert_in_set and ent_is_no_assert: no_assert_in_set = True out_set.add(ent) elif not spdx_none_int_set and ent_is_spdx_none: spdx_none_int_set = True out_set.add(ent) elif not ent_is_spdx_none and not ent_is_no_assert: out_set.add(ent) return out_set def create_spdx_filename(lib_name: str, l_loc: dict) -> str: path = "" locations = l_loc.get('locations') if len(locations): if len(locations) > 1: logging.warning( f"Found {len(l_loc['locations'])} locations for lib {lib_name}. Using the first one" ) location = locations[0] try: split_path = re.split('\\\\|\\|/|//', location['path']) path = f"../{split_path[-4]}/{split_path[-3]}/{split_path[-2]}/" except KeyError: logging.error(f"No path value in lib: {lib_name} ") else: logging.error(f"No locations found for lib {lib_name} ") rel_path = path + lib_name logging.debug( f"Received filename: {lib_name}. SPDX relative path: {rel_path}") return rel_path global ws_conn files = [] all_licenses_from_files = set() all_copyright_from_files = set() all_extracted_licenses_from_files = list() dd_list = ws_conn.get_due_diligence(token=scope_token) dd_dict = ws_utilities.convert_dict_list_to_dict(lst=dd_list, key_desc=('library', 'name')) libs = ws_conn.get_licenses(token=scope_token) libs_loc_list = ws_conn.get_library_location(token=scope_token) libs_loc = ws_utilities.convert_dict_list_to_dict(libs_loc_list, 'keyUuid') lib_filenames = set() for i, lib in enumerate(libs): lib_loc = libs_loc[lib['keyUuid']] spdx_filename = create_spdx_filename(lib['filename'], lib_loc) if spdx_filename not in lib_filenames: lib_filenames.add( spdx_filename ) # Tracking lib names as SPDX does not allow duplications logging.debug(f"Handling library (filename: {spdx_filename}") spdx_file = file.File(name=spdx_filename, spdx_id=f"SPDXRef-FILE-{i+1}", chk_sum=Algorithm(identifier="SHA1", value=lib['sha1'])) spdx_file.comment = lib.get('description') spdx_file.type = set_file_type(lib['type'], spdx_filename) file_licenses, extracted_licenses = handle_file_licenses( lib['licenses'], licenses_dict) spdx_file.licenses_in_file = list(file_licenses) all_licenses_from_files.update(file_licenses) all_extracted_licenses_from_files.extend(extracted_licenses) spdx_file.conc_lics = SPDXNone() file_copyrights = handle_file_copyright(lib['licenses'], lib, dd_dict) if file_copyrights: spdx_file.copyright = ', '.join(file_copyrights) all_copyright_from_files.update(file_copyrights) else: spdx_file.copyright = NoAssert() files.append(spdx_file) else: logging.warning( f"Found duplicate library: {lib['name']}, filename: {spdx_filename} ID: {lib['keyUuid']}. Skipping" ) all_licenses_from_files = filter_none_types(all_licenses_from_files) return files, all_licenses_from_files, all_copyright_from_files, all_extracted_licenses_from_files