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
@@ -238,22 +242,68 @@ def save_result_log():
238242 logger .warning (f"Failed to print add result log. : { ex } " )
239243
240244
241- def copy_to_root (input_license ):
245+ def copy_to_root (path_to_find , input_license ):
242246 lic_file = f"{ input_license } .txt"
243247 try :
244- source = os .path .join ('LICENSES' , f'{ lic_file } ' )
245- destination = 'LICENSE'
248+ source = os .path .join (path_to_find , 'LICENSES' , f'{ lic_file } ' )
249+ destination = os . path . join ( path_to_find , 'LICENSE' )
246250 shutil .copyfile (source , destination )
247251 except Exception as ex :
248252 dump_error_msg (f"Error - Can't copy license file: { ex } " )
249253
250254
255+ def lge_lic_download (path_to_find , input_license ):
256+ success = False
257+
258+ input_license_url = input_license .replace (' ' , '_' ).replace ('/' , '_' ).replace ('LicenseRef-' , '' ).replace ('-' , '_' )
259+ lic_url = OPENSOURCE_LGE_COM_URL_PREFIX + input_license_url + ".html"
260+
261+ try :
262+ html = urllib .request .urlopen (lic_url )
263+ source = html .read ()
264+ html .close ()
265+ except urllib .error .URLError :
266+ logger .error ("Invalid URL address" )
267+ except ValueError as val_err :
268+ logger .error (f"Invalid Value : { val_err } " )
269+ except Exception as ex :
270+ logger .error (f"Error to open url - { lic_url } : { ex } " )
271+
272+ soup = BeautifulSoup (source , 'html.parser' )
273+ try :
274+ lic_text = soup .find ("p" , "bdTop" )
275+ Path (os .path .join (os .getcwd (), path_to_find , 'LICENSES' )).mkdir (parents = True , exist_ok = True )
276+ lic_file_path = os .path .join (path_to_find , 'LICENSES' , f'{ input_license } .txt' )
277+
278+ with open (lic_file_path , 'w' , encoding = 'utf-8' ) as f :
279+ f .write (lic_text .get_text (separator = '\n ' ))
280+ if os .path .isfile (lic_file_path ):
281+ logger .info (f"Successfully downloaded { lic_file_path } " )
282+ success = True
283+ except Exception as ex :
284+ logger .error (f"Error to download license from LGE : { ex } " )
285+ return success
286+
287+
288+ def present_license_file (path_to_find , lic ):
289+ present = False
290+ lic_file_path = os .path .join (os .getcwd (), path_to_find , 'LICENSES' )
291+ file_name = f"{ lic } .txt"
292+ if file_name in os .listdir (lic_file_path ):
293+ present = True
294+ logger .info (f"{ os .path .join (path_to_find , 'LICENSES' , file_name )} already exists." )
295+ return present
296+
297+
251298def find_representative_license (path_to_find , input_license ):
252299 files = []
253300 found_file = []
254301 found_license_file = False
255302 main_parser = reuse_arg_parser ()
256303 prj = Project (path_to_find )
304+ reuse_return_code = 0
305+ success_from_lge = False
306+ present_lic = False
257307
258308 all_items = os .listdir (path_to_find )
259309 for item in all_items :
@@ -270,17 +320,24 @@ def find_representative_license(path_to_find, input_license):
270320 if found_license_file :
271321 logger .warning (f" # Found representative license file : { found_file } " )
272322 else :
323+ logger .warning (" # There is no representative license file" )
273324 input_license = check_input_license_format (input_license )
274-
275325 logger .info (f" Input License : { input_license } " )
326+
276327 parsed_args = main_parser .parse_args (['download' , f"{ input_license } " ])
277328
278329 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 :
330+ # 0: successfully downloaded, 1: failed to download
331+ reuse_return_code = reuse_download (parsed_args , prj )
332+ # Check if the license text file is present
333+ present_lic = present_license_file (path_to_find , input_license )
334+
335+ if reuse_return_code == 1 and not present_lic :
336+ success_from_lge = lge_lic_download (path_to_find , input_license )
337+
338+ if reuse_return_code == 0 or success_from_lge or present_lic :
283339 logger .warning (f" # Created Representative License File : { input_license } .txt" )
340+ copy_to_root (path_to_find , input_license )
284341
285342 except Exception as ex :
286343 dump_error_msg (f"Error - download representative license text: { ex } " )
@@ -419,7 +476,7 @@ def add_content(target_path="", input_license="", input_copyright="", output_pat
419476 all_files_list = get_allfiles_list (path_to_find )
420477
421478 # Get missing license / copyright file list
422- missing_license , missing_copyright , _ , project , _ = precheck_for_project (path_to_find )
479+ missing_license , missing_copyright , _ , project , _ = precheck_for_project (path_to_find , 'add' )
423480
424481 # Print Skipped Files
425482 missing_license_filtered , missing_copyright_filtered = \
0 commit comments