Skip to content

Commit a2aac38

Browse files
authored
Merge pull request #169 from fosslight/exclude
Modify README file
2 parents 0df7a2e + 84a87dc commit a2aac38

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Here a short summary:
2222

2323
- `add` --- Add copyright and license to missing file(s)
2424

25+
- `download` --- Download the text license file(s) that written in sbom-info.yaml or oss-pkg-info.yaml.
26+
2527

2628
## 👏 How to report issue
2729

src/fosslight_prechecker/_download_lic.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def download_license(target_path: str, input_license: str) -> None:
3131

3232
get_spdx_license_list()
3333

34-
# Download license text file of OSS-pkg-info.yaml
35-
download_oss_info_license(path_to_find)
36-
3734
# Find and create representative license file
3835
if input_license:
3936
find_representative_license(path_to_find, input_license)
37+
else:
38+
# Download license text file of OSS-pkg-info.yaml
39+
download_oss_info_license(path_to_find)
4040

41-
def lge_lic_download(path_to_find: str, input_license: str) -> bool:
41+
def lge_lic_download(temp_download_path: str, input_license: str) -> bool:
4242
success = False
4343

4444
input_license_url = input_license.replace(' ', '_').replace('/', '_').replace('LicenseRef-', '').replace('-', '_')
@@ -58,8 +58,8 @@ def lge_lic_download(path_to_find: str, input_license: str) -> bool:
5858
soup = BeautifulSoup(source, 'html.parser')
5959
try:
6060
lic_text = soup.find("p", "bdTop")
61-
Path(os.path.join(os.getcwd(), path_to_find, 'LICENSES')).mkdir(parents=True, exist_ok=True)
62-
lic_file_path = os.path.join(path_to_find, 'LICENSES', f'{input_license}.txt')
61+
Path(os.path.join(temp_download_path, 'LICENSES')).mkdir(parents=True, exist_ok=True)
62+
lic_file_path = os.path.join(temp_download_path, 'LICENSES', f'{input_license}.txt')
6363

6464
with open(lic_file_path, 'w', encoding='utf-8') as f:
6565
f.write(lic_text.get_text(separator='\n'))
@@ -71,9 +71,9 @@ def lge_lic_download(path_to_find: str, input_license: str) -> bool:
7171
return success
7272

7373

74-
def present_license_file(path_to_find: str, lic: str) -> bool:
74+
def present_license_file(path_to_check: str, lic: str) -> bool:
7575
present = False
76-
lic_file_path = os.path.join(os.getcwd(), path_to_find, 'LICENSES')
76+
lic_file_path = os.path.join(path_to_check, 'LICENSES')
7777
file_name = f"{lic}.txt"
7878
if file_name in os.listdir(lic_file_path):
7979
present = True
@@ -111,13 +111,17 @@ def download_oss_info_license(base_path: str = "") -> None:
111111
else:
112112
logger.info(" # There is no license in the path \n")
113113

114+
return converted_lic_list
115+
114116

115-
def copy_to_root(path_to_find: str, input_license: str) -> None:
117+
def copy_to_root(path_to_find: str, input_license: str, temp_download_path: str) -> None:
116118
lic_file = f"{input_license}.txt"
117119
try:
118-
source = os.path.join(path_to_find, 'LICENSES', f'{lic_file}')
120+
source = os.path.join(temp_download_path, 'LICENSES', f'{lic_file}')
119121
destination = os.path.join(path_to_find, 'LICENSE')
120122
shutil.copyfile(source, destination)
123+
logger.warning(f"# Created Representative License File ({source} -> LICENSE)\n")
124+
shutil.rmtree(temp_download_path)
121125
except Exception as ex:
122126
dump_error_msg(f"Error - Can't copy license file: {ex}")
123127

@@ -141,7 +145,6 @@ def find_representative_license(path_to_find: str, input_license: str) -> None:
141145
found_file = []
142146
found_license_file = False
143147
main_parser = reuse_parser()
144-
prj = Project(path_to_find)
145148
reuse_return_code = 0
146149
success_from_lge = False
147150
present_lic = False
@@ -160,29 +163,30 @@ def find_representative_license(path_to_find: str, input_license: str) -> None:
160163
input_license = check_input_license_format(input_license)
161164
logger.info(f"\n - Input license to be representative: {input_license}")
162165

163-
parsed_args = main_parser.parse_args(['download', f"{input_license}"])
164166
input_license = input_license.replace(os.path.sep, '')
165167
try:
166-
if found_license_file and input_license:
168+
if found_license_file:
167169
for lic_file in found_file:
168170
found_license = check_license_name(os.path.abspath(lic_file), True)
169-
if found_license != input_license:
170-
logger.warning(f"# Input License: {input_license}\n but found representative license: {found_license}, path: {os.path.abspath(lic_file)}\n")
171-
return
172-
173-
# 0: successfully downloaded, 1: failed to download
174-
reuse_return_code = reuse_download(parsed_args, prj)
175-
# Check if the license text file is present
176-
present_lic = present_license_file(path_to_find, input_license)
177-
178-
if reuse_return_code == 1 and not present_lic:
179-
# True : successfully downloaded from LGE
180-
success_from_lge = lge_lic_download(path_to_find, input_license)
181-
if success_from_lge:
182-
logger.warning(f"\n# Successfully downloaded from LGE")
183-
184-
if reuse_return_code == 0 or success_from_lge:
185-
logger.warning(f"# Created Representative License File : {input_license}.txt\n")
186-
copy_to_root(path_to_find, input_license)
171+
logger.warning(f"# Already exists representative license file: {found_license}, path: {os.path.abspath(lic_file)}\n")
172+
else:
173+
temp_download_path = os.path.join(path_to_find, "temp_lic")
174+
os.makedirs(temp_download_path, exist_ok=True)
175+
prj = Project(temp_download_path)
176+
parsed_args = main_parser.parse_args(['download', f"{input_license}"])
177+
178+
# 0: successfully downloaded, 1: failed to download
179+
reuse_return_code = reuse_download(parsed_args, prj)
180+
# Check if the license text file is present
181+
present_lic = present_license_file(temp_download_path, input_license)
182+
183+
if reuse_return_code == 1 and not present_lic:
184+
# True : successfully downloaded from LGE
185+
success_from_lge = lge_lic_download(temp_download_path, input_license)
186+
if success_from_lge:
187+
logger.warning(f"\n# Successfully downloaded from LGE")
188+
189+
if reuse_return_code == 0 or success_from_lge:
190+
copy_to_root(path_to_find, input_license, temp_download_path)
187191
except Exception as ex:
188192
dump_error_msg(f"Error - download representative license text: {ex}")

0 commit comments

Comments
 (0)