From 57ed82eb32d0dc89401f0301f23ae1e3455d5b4d Mon Sep 17 00:00:00 2001 From: Jiyeong Seok Date: Fri, 5 Apr 2024 14:46:32 +0900 Subject: [PATCH] Add depends_on, purl in yaml Signed-off-by: Jiyeong Seok --- src/fosslight_util/oss_item.py | 56 +++++++++++++++++++++++++------- src/fosslight_util/write_yaml.py | 17 ++++++---- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/fosslight_util/oss_item.py b/src/fosslight_util/oss_item.py index 355dd13..e904562 100644 --- a/src/fosslight_util/oss_item.py +++ b/src/fosslight_util/oss_item.py @@ -5,7 +5,7 @@ import logging import os -from fosslight_util.constant import LOGGER_NAME +from fosslight_util.constant import LOGGER_NAME, FL_DEPENDENCY _logger = logging.getLogger(LOGGER_NAME) @@ -25,6 +25,8 @@ def __init__(self, value): self._yocto_recipe = [] self._yocto_package = [] self.is_binary = False + self._depends_on = [] + self.purl = "" def __del__(self): pass @@ -123,11 +125,29 @@ def yocto_package(self, value): self._yocto_package = [item.strip() for item in self._yocto_package] self._yocto_package = list(set(self._yocto_package)) - def set_sheet_item(self, item): + @property + def depends_on(self): + return self._depends_on + + @depends_on.setter + def depends_on(self, value): + if not value: + self._depends_on = [] + else: + if not isinstance(value, list): + value = value.split(",") + self._depends_on.extend(value) + self._depends_on = [item.strip() for item in self._depends_on] + self._depends_on = list(set(self._depends_on)) + + def set_sheet_item(self, item, scanner_name=''): if len(item) < 9: _logger.warning(f"sheet list is too short ({len(item)}): {item}") return - self.source_name_or_path = item[0] + if scanner_name == FL_DEPENDENCY: + self.purl = item[0] + else: + self.source_name_or_path = item[0] self.name = item[1] self.version = item[2] self.license = item[3] @@ -137,19 +157,29 @@ def set_sheet_item(self, item): self.exclude = item[7] self.comment = item[8] - def get_print_array(self): + if len(item) >= 10 and scanner_name == FL_DEPENDENCY: + self.depends_on = item[9] + + def get_print_array(self, scanner_name=''): items = [] - if len(self.source_name_or_path) == 0: - self.source_name_or_path.append("") + if scanner_name != FL_DEPENDENCY: + if len(self.source_name_or_path) == 0: + self.source_name_or_path.append("") if len(self.license) == 0: self.license.append("") exclude = "Exclude" if self.exclude else "" - - for source_name_or_path in self.source_name_or_path: - lic = ",".join(self.license) - items.append([os.path.join(self.relative_path, source_name_or_path), self.name, self.version, lic, - self.download_location, self.homepage, self.copyright, exclude, self.comment]) + lic = ",".join(self.license) + if scanner_name == FL_DEPENDENCY: + items = [self.purl, self.name, self.version, lic, + self.download_location, self.homepage, self.copyright, exclude, self.comment] + if len(self.depends_on) > 0: + items.append(",".join(self.depends_on)) + else: + for source_name_or_path in self.source_name_or_path: + oss_item = [os.path.join(self.relative_path, source_name_or_path), self.name, self.version, lic, + self.download_location, self.homepage, self.copyright, exclude, self.comment] + items.append(oss_item) return items def get_print_json(self): @@ -171,6 +201,10 @@ def get_print_json(self): json_item["exclude"] = self.exclude if self.comment != "": json_item["comment"] = self.comment + if len(self.depends_on) > 0: + json_item["depends on"] = self.depends_on + if self.purl != "": + json_item["purl"] = self.purl return json_item diff --git a/src/fosslight_util/write_yaml.py b/src/fosslight_util/write_yaml.py index 591538c..25a799f 100644 --- a/src/fosslight_util/write_yaml.py +++ b/src/fosslight_util/write_yaml.py @@ -35,11 +35,16 @@ def write_yaml(output_file, sheet_list_origin, separate_yaml=False): for sheet_name, sheet_contents in sheet_list.items(): if sheet_name not in constant.supported_sheet_and_scanner.keys(): continue + scanner_name = constant.supported_sheet_and_scanner[sheet_name] + sheet_contents_with_scanner = [] + for i in sheet_contents: + i.insert(0, scanner_name) + sheet_contents_with_scanner.append(i) if not separate_yaml: - merge_sheet.extend(sheet_contents) + merge_sheet.extend(sheet_contents_with_scanner) else: output_file = f'{separate_output_file}_{sheet_name}.yaml' - convert_sheet_to_yaml(sheet_contents, output_file) + convert_sheet_to_yaml(sheet_contents_with_scanner, output_file) output_files.append(output_file) if not separate_yaml: @@ -61,13 +66,13 @@ def write_yaml(output_file, sheet_list_origin, separate_yaml=False): return success, error_msg, output -def convert_sheet_to_yaml(sheet_contents, output_file): - sheet_contents = [list(t) for t in set(tuple(e) for e in sorted(sheet_contents))] +def convert_sheet_to_yaml(sheet_contents_with_scanner, output_file): + sheet_contents_with_scanner = [list(t) for t in set(tuple(e) for e in sorted(sheet_contents_with_scanner))] yaml_dict = {} - for sheet_item in sheet_contents: + for sheet_item in sheet_contents_with_scanner: item = OssItem('') - item.set_sheet_item(sheet_item) + item.set_sheet_item(sheet_item[1:], sheet_item[0]) create_yaml_with_ossitem(item, yaml_dict) with open(output_file, 'w') as f: