22# -*- coding: utf-8 -*-
33# Copyright (c) 2021 LG Electronics Inc.
44# SPDX-License-Identifier: Apache-2.0
5-
65import yaml
76import logging
87import codecs
98import os
9+ import re
1010import sys
1111from .constant import LOGGER_NAME
1212from .oss_item import OssItem
1313
1414_logger = logging .getLogger (LOGGER_NAME )
15+ SUPPORT_OSS_INFO_FILES = [r"oss-pkg-info[\s\S]*.yaml" , r"sbom-info[\s\S]*.yaml" ]
16+ EXAMPLE_OSS_PKG_INFO_LINK = "https://github.com/fosslight/fosslight_prechecker/blob/main/tests/convert/sbom-info.yaml"
1517
1618
1719def parsing_yml (yaml_file , base_path ):
@@ -35,6 +37,8 @@ def parsing_yml(yaml_file, base_path):
3537 for root_element in doc :
3638 oss_items = doc [root_element ]
3739 if oss_items :
40+ if 'version' not in oss_items [0 ]:
41+ raise AttributeError (f"- Ref. { EXAMPLE_OSS_PKG_INFO_LINK } " )
3842 for oss in oss_items :
3943 item = OssItem (relative_path )
4044 if not is_old_format :
@@ -46,27 +50,28 @@ def parsing_yml(yaml_file, base_path):
4650 oss_list .append (item )
4751 license_list .extend (item .license )
4852 idx += 1
53+ except AttributeError as ex :
54+ _logger .error (f"Not supported yaml file format { ex } " )
4955 except yaml .YAMLError :
5056 _logger .warning (f"Can't parse yaml - skip to parse yaml file: { yaml_file } " )
5157 return oss_list , set (license_list )
5258
5359
54- def find_all_oss_pkg_files (path_to_find , file_to_find ):
60+ def find_sbom_yaml_files (path_to_find ):
5561 oss_pkg_files = []
5662
5763 if not os .path .isdir (path_to_find ):
5864 _logger .error (f"Can't find a path: { path_to_find } " )
5965 sys .exit (os .EX_DATAERR )
6066
6167 try :
62- # oss_pkg_files.extend(glob.glob(path_to_find + '/**/'+file, recursive=True)) #glib is too slow
63- for p , d , f in os .walk (path_to_find ):
64- for file in f :
68+ for root , dirs , files in os .walk (path_to_find ):
69+ for file in files :
6570 file_name = file .lower ()
66- if file_name in file_to_find or (
67- ( file_name . endswith ( "yml" ) or file_name . endswith ( "yaml" ) )
68- and file_name . startswith ( "oss-pkg-info" ) ):
69- oss_pkg_files .append (os .path .join (p , file ))
71+ for oss_info in SUPPORT_OSS_INFO_FILES :
72+ p = re . compile ( oss_info , re . I )
73+ if p . search ( file_name ):
74+ oss_pkg_files .append (os .path .join (root , file ))
7075 except Exception as ex :
7176 _logger .error (f"Error, find all oss-pkg-info files: { ex } " )
7277
0 commit comments