Skip to content

Commit b8f05dd

Browse files
authored
Merge pull request #168 from fosslight/exclude
Add 'Download' mode to download license text
2 parents e12d86a + 1c30bf4 commit b8f05dd

File tree

5 files changed

+221
-167
lines changed

5 files changed

+221
-167
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ reuse==1.1.2
55
PyYAML
66
fosslight_util>=2.1.10
77
jinja2
8+
pyaskalono

src/fosslight_prechecker/_add.py

Lines changed: 16 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,27 @@
55
import os
66
import re
77
import logging
8-
import shutil
98
import sys
109
import fosslight_util.constant as constant
11-
import urllib.request
1210
import argparse
1311
from yaml import safe_dump
1412
from fosslight_util.set_log import init_log
1513
from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick
16-
from fosslight_util.parsing_yaml import find_sbom_yaml_files, parsing_yml
1714
from fosslight_util.output_format import check_output_format
1815
from fosslight_util.exclude import excluding_files
1916
from datetime import datetime
2017
from fosslight_prechecker._precheck import precheck_for_project, precheck_for_files, dump_error_msg, \
2118
get_path_to_find, DEFAULT_EXCLUDE_EXTENSION_FILES
2219
from fosslight_prechecker._result import get_total_file_list
2320
from fosslight_prechecker._add_header import add_header, reuse_parser
24-
from reuse.download import run as reuse_download
2521
from reuse._comment import EXTENSION_COMMENT_STYLE_MAP_LOWERCASE
2622
from reuse.project import Project
27-
from bs4 import BeautifulSoup
28-
from pathlib import Path
2923
from typing import List, Optional
3024

3125

3226
PKG_NAME = "fosslight_prechecker"
33-
LICENSE_INCLUDE_FILES = ["license", "license.md", "license.txt", "notice"]
3427
EXCLUDE_DIR = ["test", "tests", "doc", "docs"]
3528
EXCLUDE_PREFIX = ("test", ".", "doc", "__")
36-
OPENSOURCE_LGE_COM_URL_PREFIX = "https://opensource.lge.com/license/"
3729
_result_log = {}
3830
spdx_licenses = []
3931

@@ -253,104 +245,6 @@ def save_result_log() -> None:
253245
logger.warning(f"Failed to print add result log. : {ex}")
254246

255247

256-
def copy_to_root(path_to_find: str, input_license: str) -> None:
257-
lic_file = f"{input_license}.txt"
258-
try:
259-
source = os.path.join(path_to_find, 'LICENSES', f'{lic_file}')
260-
destination = os.path.join(path_to_find, 'LICENSE')
261-
shutil.copyfile(source, destination)
262-
except Exception as ex:
263-
dump_error_msg(f"Error - Can't copy license file: {ex}")
264-
265-
266-
def lge_lic_download(path_to_find: str, input_license: str) -> bool:
267-
success = False
268-
269-
input_license_url = input_license.replace(' ', '_').replace('/', '_').replace('LicenseRef-', '').replace('-', '_')
270-
lic_url = OPENSOURCE_LGE_COM_URL_PREFIX + input_license_url + ".html"
271-
272-
try:
273-
html = urllib.request.urlopen(lic_url)
274-
source = html.read()
275-
html.close()
276-
except urllib.error.URLError:
277-
logger.error("Invalid URL address")
278-
except ValueError as val_err:
279-
logger.error(f"Invalid Value : {val_err}")
280-
except Exception as ex:
281-
logger.error(f"Error to open url - {lic_url} : {ex}")
282-
283-
soup = BeautifulSoup(source, 'html.parser')
284-
try:
285-
lic_text = soup.find("p", "bdTop")
286-
Path(os.path.join(os.getcwd(), path_to_find, 'LICENSES')).mkdir(parents=True, exist_ok=True)
287-
lic_file_path = os.path.join(path_to_find, 'LICENSES', f'{input_license}.txt')
288-
289-
with open(lic_file_path, 'w', encoding='utf-8') as f:
290-
f.write(lic_text.get_text(separator='\n'))
291-
if os.path.isfile(lic_file_path):
292-
logger.info(f"Successfully downloaded {lic_file_path}")
293-
success = True
294-
except Exception as ex:
295-
logger.error(f"Error to download license from LGE : {ex}")
296-
return success
297-
298-
299-
def present_license_file(path_to_find: str, lic: str) -> bool:
300-
present = False
301-
lic_file_path = os.path.join(os.getcwd(), path_to_find, 'LICENSES')
302-
file_name = f"{lic}.txt"
303-
if file_name in os.listdir(lic_file_path):
304-
present = True
305-
return present
306-
307-
308-
def find_representative_license(path_to_find: str, input_license: str) -> None:
309-
files = []
310-
found_file = []
311-
found_license_file = False
312-
main_parser = reuse_parser()
313-
prj = Project(path_to_find)
314-
reuse_return_code = 0
315-
success_from_lge = False
316-
present_lic = False
317-
318-
all_items = os.listdir(path_to_find)
319-
for item in all_items:
320-
if os.path.isfile(item):
321-
files.append(os.path.basename(item))
322-
323-
for file in files:
324-
file_lower_case = file.lower()
325-
if file_lower_case in LICENSE_INCLUDE_FILES or file_lower_case.startswith("license") or file_lower_case.startswith("notice"):
326-
found_file.append(file)
327-
found_license_file = True
328-
329-
input_license = check_input_license_format(input_license)
330-
logger.info(f"\n - Representative license : {input_license}")
331-
332-
parsed_args = main_parser.parse_args(['download', f"{input_license}"])
333-
input_license = input_license.replace(os.path.sep, '')
334-
try:
335-
# 0: successfully downloaded, 1: failed to download
336-
reuse_return_code = reuse_download(parsed_args, prj)
337-
# Check if the license text file is present
338-
present_lic = present_license_file(path_to_find, input_license)
339-
340-
if reuse_return_code == 1 and not present_lic:
341-
# True : successfully downloaded from LGE
342-
success_from_lge = lge_lic_download(path_to_find, input_license)
343-
344-
if reuse_return_code == 0 or success_from_lge:
345-
if found_license_file:
346-
logger.info(f"# Found representative license file : {found_file}\n")
347-
else:
348-
logger.warning(f"# Created Representative License File : {input_license}.txt\n")
349-
copy_to_root(path_to_find, input_license)
350-
except Exception as ex:
351-
dump_error_msg(f"Error - download representative license text: {ex}")
352-
353-
354248
def is_exclude_dir(dir_path: str) -> Optional[bool]:
355249
if dir_path != "":
356250
dir_path = dir_path.lower()
@@ -362,39 +256,21 @@ def is_exclude_dir(dir_path: str) -> Optional[bool]:
362256
return
363257

364258

365-
def download_oss_info_license(base_path: str, input_license: str = "") -> None:
366-
license_list = []
367-
converted_lic_list = []
368-
oss_yaml_files = []
369-
main_parser = reuse_parser()
370-
prj = Project(base_path)
371-
372-
oss_yaml_files = find_sbom_yaml_files(base_path)
373-
374-
if input_license != "":
375-
license_list.append(input_license)
376-
377-
if oss_yaml_files is None or len(oss_yaml_files) == 0:
378-
logger.info("\n # There is no OSS package Info file in this path\n")
379-
return
380-
else:
381-
logger.info(f"\n # There is OSS Package Info file(s) : {oss_yaml_files}\n")
382-
383-
for oss_pkg_file in oss_yaml_files:
384-
_, license_list, _ = parsing_yml(oss_pkg_file, base_path)
385-
386-
for lic in license_list:
387-
converted_lic_list.append(check_input_license_format(lic))
388-
389-
if license_list is not None and len(license_list) > 0:
390-
parsed_args = main_parser.parse_args(['download'] + converted_lic_list)
259+
def get_spdx_license_list():
260+
global spdx_licenses
261+
try:
262+
success, error_msg, licenses = get_spdx_licenses_json()
263+
if success is False:
264+
dump_error_msg(f"Error to get SPDX Licesens : {error_msg}")
391265

392-
try:
393-
reuse_download(parsed_args, prj)
394-
except Exception as ex:
395-
dump_error_msg(f"Error - download license text in OSS-pkg-info.yml : {ex}")
396-
else:
397-
logger.info(" # There is no license in the path \n")
266+
licenseInfo = licenses.get("licenses")
267+
for info in licenseInfo:
268+
shortID = info.get("licenseId")
269+
isDeprecated = info.get("isDeprecatedLicenseId")
270+
if isDeprecated is False:
271+
spdx_licenses.append(shortID)
272+
except Exception as ex:
273+
dump_error_msg(f"Error access to get_spdx_licenses_json : {ex}")
398274

399275

400276
def add_content(
@@ -406,7 +282,7 @@ def add_content(
406282
need_log_file: bool = True,
407283
exclude_path: list = []
408284
) -> None:
409-
global _result_log, spdx_licenses
285+
global _result_log
410286
_check_only_file_mode = False
411287
file_to_check_list = []
412288
missing_license = []
@@ -431,19 +307,7 @@ def add_content(
431307
sys.exit(1)
432308

433309
# Get SPDX License List
434-
try:
435-
success, error_msg, licenses = get_spdx_licenses_json()
436-
if success is False:
437-
dump_error_msg(f"Error to get SPDX Licesens : {error_msg}")
438-
439-
licenseInfo = licenses.get("licenses")
440-
for info in licenseInfo:
441-
shortID = info.get("licenseId")
442-
isDeprecated = info.get("isDeprecatedLicenseId")
443-
if isDeprecated is False:
444-
spdx_licenses.append(shortID)
445-
except Exception as ex:
446-
dump_error_msg(f"Error access to get_spdx_licenses_json : {ex}")
310+
get_spdx_license_list()
447311

448312
if _check_only_file_mode:
449313
main_parser = reuse_parser()
@@ -473,9 +337,6 @@ def add_content(
473337
if input_dl_url:
474338
add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_to_check_list)
475339
else:
476-
# Download license text file of OSS-pkg-info.yaml
477-
download_oss_info_license(path_to_find, input_license)
478-
479340
# Get missing license / copyright file list
480341
missing_license, missing_copyright, _, project, prj_report = precheck_for_project(path_to_find, abs_path_to_exclude)
481342

@@ -502,9 +363,4 @@ def add_content(
502363
input_copyright,
503364
total_files_excluded,
504365
input_dl_url)
505-
506-
# Find and create representative license file
507-
if input_license != "" and len(missing_license) > 0:
508-
find_representative_license(path_to_find, input_license)
509-
510366
save_result_log()

0 commit comments

Comments
 (0)