Skip to content

Commit 9ee94ed

Browse files
authored
Merge pull request #115 from fosslight/develop
Modify downloading the license text
2 parents 9cd4d41 + bcb5f0f commit 9ee94ed

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

src/fosslight_prechecker/_add.py

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import sys
1111
import fosslight_util.constant as constant
12+
import urllib.request
1213
from yaml import safe_dump
1314
from fosslight_util.set_log import init_log
1415
from fosslight_util.spdx_licenses import get_spdx_licenses_json
@@ -21,6 +22,8 @@
2122
from reuse._comment import EXTENSION_COMMENT_STYLE_MAP_LOWERCASE
2223
from reuse._main import parser as reuse_arg_parser
2324
from reuse.project import Project
25+
from bs4 import BeautifulSoup
26+
from pathlib import Path
2427

2528

2629
PKG_NAME = "fosslight_prechecker"
@@ -29,6 +32,7 @@
2932
EXCLUDE_PREFIX = ("test", ".", "doc", "__")
3033
RESOURCES_DIR = 'resources'
3134
LICENSES_JSON_FILE = 'convert_license.json'
35+
OPENSOURCE_LGE_COM_URL_PREFIX = "https://opensource.lge.com/license/"
3236
_result_log = {}
3337
spdx_licenses = []
3438

@@ -88,6 +92,7 @@ def convert_to_spdx_style(input_string):
8892

8993

9094
def 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+
251299
def 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

335393
def 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 = \

src/fosslight_prechecker/_precheck.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def exclude_untracked_files(path):
4848
logger.error(f"Error to get git untracked files : {ex}")
4949

5050

51-
def exclude_gitignore_files(root_path, path):
51+
def exclude_gitignore_files(path):
5252
global DEFAULT_EXCLUDE_EXTENSION_FILES
5353
try:
54+
root_path = VCSStrategyGit.find_root(path)
5455
if os.path.isfile(os.path.join(root_path, '.gitignore')):
5556
cmd_result = subprocess.check_output(['git',
5657
'ls-files',
@@ -72,29 +73,27 @@ def exclude_gitignore_files(root_path, path):
7273

7374
def exclude_git_related_files(path):
7475
try:
75-
root_path = VCSStrategyGit.find_root(path)
76-
7776
# Change currnt path for git command
7877
current_path = os.getcwd()
7978
os.chdir(path)
8079

8180
# Exclude untracked files
8281
exclude_untracked_files(path)
8382
# Exclude ignore files
84-
exclude_gitignore_files(root_path, path)
83+
exclude_gitignore_files(path)
8584

8685
# Restore path
8786
os.chdir(current_path)
8887
except Exception as ex:
8988
logger.error(f"Error to get git related files : {ex}")
9089

9190

92-
def find_oss_pkg_info_and_exlcude_file(path):
91+
def find_oss_pkg_info_and_exlcude_file(path, mode='lint'):
9392
global DEFAULT_EXCLUDE_EXTENSION_FILES
9493
oss_pkg_info = []
9594
git_present = shutil.which("git")
9695

97-
if _turn_on_exclude_config and git_present and VCSStrategyGit.in_repo(path):
96+
if mode == 'lint' and _turn_on_exclude_config and git_present and VCSStrategyGit.in_repo(path):
9897
exclude_git_related_files(path)
9998

10099
try:
@@ -231,11 +230,11 @@ def precheck_for_files(path, files):
231230
return missing_license_list, missing_copyright_list, prj
232231

233232

234-
def precheck_for_project(path_to_find):
233+
def precheck_for_project(path_to_find, mode='lint'):
235234
missing_license = []
236235
missing_copyright = []
237236

238-
oss_pkg_info_files = find_oss_pkg_info_and_exlcude_file(path_to_find)
237+
oss_pkg_info_files = find_oss_pkg_info_and_exlcude_file(path_to_find, mode)
239238
if _turn_on_exclude_config:
240239
need_rollback, temp_file_name, temp_dir_name = create_reuse_dep5_file(path_to_find)
241240

@@ -341,7 +340,7 @@ def run_lint(target_path, disable, output_file_name, format='', need_log_file=Tr
341340
if _check_only_file_mode:
342341
license_missing_files, copyright_missing_files, project = precheck_for_files(path_to_find, file_to_check_list)
343342
else:
344-
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find)
343+
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find, 'lint')
345344

346345
if need_log_file:
347346
timer.stop = True

src/fosslight_prechecker/cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
from fosslight_prechecker._precheck import run_lint
1414

1515

16-
def run_main(mode, path, output, format, no_log, disable, copyright, license):
16+
def run_main(mode, path, output, format, no_log, disable, copyright, license, parser):
17+
if mode != "add" and (copyright != "" or license != ""):
18+
parser.print_help()
19+
sys.exit(1)
20+
1721
if mode == "lint":
1822
run_lint(path, disable, output, format, no_log)
1923
elif mode == "add":
@@ -34,8 +38,8 @@ def main():
3438
parser.add_argument('-p', '--path', help='Path to check', type=str, dest='path', default="")
3539
parser.add_argument('-f', '--format', help='Format of ouput', type=str, dest='format', default="")
3640
parser.add_argument('-o', '--output', help='Output file name', type=str, dest='output', default="")
37-
parser.add_argument('-l', '--license', help='License name to add', type=str, dest='license', default="")
38-
parser.add_argument('-c', '--copyright', help='Copyright to add', type=str, dest='copyright', default="")
41+
parser.add_argument('-l', '--license', help="License name to add(used in only 'add' mode)", type=str, dest='license', default="")
42+
parser.add_argument('-c', '--copyright', help="Copyright to add(used in only 'add' mode)", type=str, dest='copyright', default="")
3943
parser.add_argument('--notice', action='store_true', required=False)
4044
try:
4145
args = parser.parse_args()
@@ -62,7 +66,7 @@ def main():
6266
print(f.read())
6367
else:
6468
run_main(args.mode, args.path, args.output, args.format,
65-
args.log, args.disable, args.copyright, args.license)
69+
args.log, args.disable, args.copyright, args.license, parser)
6670

6771

6872
if __name__ == "__main__":

src/fosslight_prechecker/resources/convert_license.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,17 @@
1717
"apache-2.0": "Apache-2.0",
1818
"apache2.0": "Apache-2.0",
1919
"apache 2.0": "Apache-2.0",
20-
"mit": "MIT"
21-
}
20+
"bsd-3-clause": "BSD-3-clause",
21+
"bsd 3 clause": "BSD-3-clause",
22+
"bsd-3 clause": "BSD-3-clause",
23+
"bsd 3-clause": "BSD-3-clause",
24+
"agpl 3.0": "AGPL-3.0",
25+
"apgl-3.0": "APGL-3.0",
26+
"apgl3.0": "APGL-3.0",
27+
"agplv3": "AGPL-3.0",
28+
"mit": "MIT",
29+
"lge": "LicenseRef-LGE_Proprietary_License",
30+
"lge license": "LicenseRef-LGE_Proprietary_License",
31+
"lge proprietary": "LicenseRef-LGE_Proprietary_License",
32+
"lge-proprietary": "LicenseRef-LGE_Proprietary_License"
33+
}

0 commit comments

Comments
 (0)