99import json
1010import sys
1111import fosslight_util .constant as constant
12+ import urllib .request
1213from yaml import safe_dump
1314from fosslight_util .set_log import init_log
1415from fosslight_util .spdx_licenses import get_spdx_licenses_json
2122from reuse ._comment import EXTENSION_COMMENT_STYLE_MAP_LOWERCASE
2223from reuse ._main import parser as reuse_arg_parser
2324from reuse .project import Project
25+ from bs4 import BeautifulSoup
26+ from pathlib import Path
2427
2528
2629PKG_NAME = "fosslight_prechecker"
2932EXCLUDE_PREFIX = ("test" , "." , "doc" , "__" )
3033RESOURCES_DIR = 'resources'
3134LICENSES_JSON_FILE = 'convert_license.json'
35+ OPENSOURCE_LGE_COM_URL_PREFIX = "https://opensource.lge.com/license/"
3236_result_log = {}
3337spdx_licenses = []
3438
@@ -88,6 +92,7 @@ def convert_to_spdx_style(input_string):
8892
8993
9094def check_input_license_format (input_license ):
95+ global spdx_licenses
9196 if input_license in spdx_licenses :
9297 return input_license
9398
@@ -238,22 +243,68 @@ def save_result_log():
238243 logger .warning (f"Failed to print add result log. : { ex } " )
239244
240245
241- def copy_to_root (input_license ):
246+ def copy_to_root (path_to_find , input_license ):
242247 lic_file = f"{ input_license } .txt"
243248 try :
244- source = os .path .join ('LICENSES' , f'{ lic_file } ' )
245- destination = 'LICENSE'
249+ source = os .path .join (path_to_find , 'LICENSES' , f'{ lic_file } ' )
250+ destination = os . path . join ( path_to_find , 'LICENSE' )
246251 shutil .copyfile (source , destination )
247252 except Exception as ex :
248253 dump_error_msg (f"Error - Can't copy license file: { ex } " )
249254
250255
256+ def lge_lic_download (path_to_find , input_license ):
257+ success = False
258+
259+ input_license_url = input_license .replace (' ' , '_' ).replace ('/' , '_' ).replace ('LicenseRef-' , '' ).replace ('-' , '_' )
260+ lic_url = OPENSOURCE_LGE_COM_URL_PREFIX + input_license_url + ".html"
261+
262+ try :
263+ html = urllib .request .urlopen (lic_url )
264+ source = html .read ()
265+ html .close ()
266+ except urllib .error .URLError :
267+ logger .error ("Invalid URL address" )
268+ except ValueError as val_err :
269+ logger .error (f"Invalid Value : { val_err } " )
270+ except Exception as ex :
271+ logger .error (f"Error to open url - { lic_url } : { ex } " )
272+
273+ soup = BeautifulSoup (source , 'html.parser' )
274+ try :
275+ lic_text = soup .find ("p" , "bdTop" )
276+ Path (os .path .join (os .getcwd (), path_to_find , 'LICENSES' )).mkdir (parents = True , exist_ok = True )
277+ lic_file_path = os .path .join (path_to_find , 'LICENSES' , f'{ input_license } .txt' )
278+
279+ with open (lic_file_path , 'w' , encoding = 'utf-8' ) as f :
280+ f .write (lic_text .get_text (separator = '\n ' ))
281+ if os .path .isfile (lic_file_path ):
282+ logger .info (f"Successfully downloaded { lic_file_path } " )
283+ success = True
284+ except Exception as ex :
285+ logger .error (f"Error to download license from LGE : { ex } " )
286+ return success
287+
288+
289+ def present_license_file (path_to_find , lic ):
290+ present = False
291+ lic_file_path = os .path .join (os .getcwd (), path_to_find , 'LICENSES' )
292+ file_name = f"{ lic } .txt"
293+ if file_name in os .listdir (lic_file_path ):
294+ present = True
295+ logger .info (f"{ os .path .join (path_to_find , 'LICENSES' , file_name )} already exists." )
296+ return present
297+
298+
251299def find_representative_license (path_to_find , input_license ):
252300 files = []
253301 found_file = []
254302 found_license_file = False
255303 main_parser = reuse_arg_parser ()
256304 prj = Project (path_to_find )
305+ reuse_return_code = 0
306+ success_from_lge = False
307+ present_lic = False
257308
258309 all_items = os .listdir (path_to_find )
259310 for item in all_items :
@@ -270,17 +321,24 @@ def find_representative_license(path_to_find, input_license):
270321 if found_license_file :
271322 logger .warning (f" # Found representative license file : { found_file } " )
272323 else :
324+ logger .warning (" # There is no representative license file" )
273325 input_license = check_input_license_format (input_license )
274-
275326 logger .info (f" Input License : { input_license } " )
327+
276328 parsed_args = main_parser .parse_args (['download' , f"{ input_license } " ])
277329
278330 try :
279- logger .warning (" # There is no representative license file" )
280- reuse_download (parsed_args , prj )
281- copy_to_root (input_license )
282- if input_license in spdx_licenses :
331+ # 0: successfully downloaded, 1: failed to download
332+ reuse_return_code = reuse_download (parsed_args , prj )
333+ # Check if the license text file is present
334+ present_lic = present_license_file (path_to_find , input_license )
335+
336+ if reuse_return_code == 1 and not present_lic :
337+ success_from_lge = lge_lic_download (path_to_find , input_license )
338+
339+ if reuse_return_code == 0 or success_from_lge or present_lic :
283340 logger .warning (f" # Created Representative License File : { input_license } .txt" )
341+ copy_to_root (path_to_find , input_license )
284342
285343 except Exception as ex :
286344 dump_error_msg (f"Error - download representative license text: { ex } " )
@@ -333,7 +391,7 @@ def download_oss_info_license(base_path, input_license=""):
333391
334392
335393def add_content (target_path = "" , input_license = "" , input_copyright = "" , output_path = "" , need_log_file = True ):
336- global _result_log
394+ global _result_log , spdx_licenses
337395 _check_only_file_mode = False
338396 file_to_check_list = []
339397
@@ -419,7 +477,7 @@ def add_content(target_path="", input_license="", input_copyright="", output_pat
419477 all_files_list = get_allfiles_list (path_to_find )
420478
421479 # Get missing license / copyright file list
422- missing_license , missing_copyright , _ , project , _ = precheck_for_project (path_to_find )
480+ missing_license , missing_copyright , _ , project , _ = precheck_for_project (path_to_find , 'add' )
423481
424482 # Print Skipped Files
425483 missing_license_filtered , missing_copyright_filtered = \
0 commit comments