Skip to content

Commit a82b856

Browse files
authored
Change license scanner to askalono package (#242)
* Change license scanner to askalono package * Remove third party src (nomos) --------- Signed-off-by: jiyeong.seok <[email protected]>
1 parent b8592c9 commit a82b856

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+38
-37241
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# Apply override to all files in the directory
22
tests/** linguist-vendored
3-
src/fosslight_dependency/third_party/** linguist-vendored

.github/workflows/publish-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
7373
- name: Build exe with PyInstaller for windows
7474
run: |
75-
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --add-binary "src/fosslight_dependency/third_party/askalono/askalono.exe;third_party/askalono" --add-binary "LICENSE;LICENSES" --add-binary "LICENSES\LicenseRef-3rd_party_licenses.txt;LICENSES" --collect-datas fosslight_util --hidden-import=_cffi_backend
75+
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --add-binary "LICENSE;LICENSES" --add-binary "LICENSES\LicenseRef-3rd_party_licenses.txt;LICENSES" --collect-datas fosslight_util --hidden-import=_cffi_backend
7676
mkdir out
7777
move dist/cli.exe fosslight_dependency_windows.exe
7878

.reuse/dep5

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ Files: setup.cfg
2222
Copyright: 2021 LG Electronics
2323
License: Apache-2.0
2424

25-
Files: .gitignore
25+
Files: requirements.txt
2626
Copyright: 2021 LG Electronics
27-
License: Apache-2.0
27+
License: LicenseRef-3rd_party_licenses
2828

29-
Files: src/fosslight_dependency/third_party/askalono/*
30-
Copyright: 2018 Amazon.com, Inc. or its affiliates.
29+
Files: .gitignore
30+
Copyright: 2021 LG Electronics
3131
License: Apache-2.0
3232

33-
Files: src/fosslight_dependency/third_party/nomos/*
34-
Copyright: 2006-2009 Hewlett-Packard Development Company, L.P.
35-
License: LicenseRef-3rd_party_licenses
36-
3733
Files: tests/test_gradle/*
3834
Copyright: 2017-2021 Google LLC.
3935
License: Apache-2.0

setup.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@
5252
"Programming Language :: Python :: 3.8",
5353
"Programming Language :: Python :: 3.9", ],
5454
install_requires=required,
55-
package_data={_PACKAEG_NAME: [os.path.join('third_party', 'nomos', 'nomossa'),
56-
os.path.join('third_party', 'askalono', 'askalono.exe'),
57-
os.path.join('third_party', 'askalono', 'askalono_macos'),
58-
os.path.join(_LICENSE_DIR, '*')]},
55+
package_data={_PACKAEG_NAME: [os.path.join(_LICENSE_DIR, '*')]},
5956
include_package_data=True,
6057
entry_points={
6158
"console_scripts": [

src/fosslight_dependency/_package_manager.py

Lines changed: 15 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
# SPDX-License-Identifier: Apache-2.0
55

66
import os
7-
import sys
87
import logging
98
import platform
109
import re
1110
import base64
1211
import subprocess
1312
import shutil
1413
import stat
14+
from packageurl.contrib import url2purl
15+
from askalono import identify
1516
import fosslight_util.constant as constant
1617
import fosslight_dependency.constant as const
17-
from packageurl.contrib import url2purl
1818

1919
try:
2020
from github import Github
@@ -23,13 +23,9 @@
2323

2424
logger = logging.getLogger(constant.LOGGER_NAME)
2525

26-
# binary url to check license text
27-
_license_scanner_linux = os.path.join('third_party', 'nomos', 'nomossa')
28-
_license_scanner_macos = os.path.join('third_party', 'askalono', 'askalono_macos')
29-
_license_scanner_windows = os.path.join('third_party', 'askalono', 'askalono.exe')
30-
3126
gradle_config = ['runtimeClasspath', 'runtime']
3227
android_config = ['releaseRuntimeClasspath']
28+
ASKALONO_THRESHOLD = 0.7
3329

3430

3531
class PackageManager:
@@ -54,7 +50,6 @@ def __init__(self, package_manager_name, dn_url, input_dir, output_dir):
5450
self.dep_items = []
5551

5652
self.platform = platform.system()
57-
self.license_scanner_bin = check_license_scanner(self.platform)
5853

5954
def __del__(self):
6055
self.input_package_list_file = []
@@ -316,9 +311,8 @@ def connect_github(github_token):
316311
return g
317312

318313

319-
def get_github_license(g, github_repo, platform, license_scanner_bin):
314+
def get_github_license(g, github_repo):
320315
license_name = ''
321-
tmp_license_txt_file_name = 'tmp_license.txt'
322316

323317
try:
324318
repository = g.get_repo(github_repo)
@@ -334,96 +328,26 @@ def get_github_license(g, github_repo, platform, license_scanner_bin):
334328
if license_name == "" or license_name == "NOASSERTION":
335329
try:
336330
license_txt_data = base64.b64decode(repository.get_license().content).decode('utf-8')
337-
tmp_license_txt = open(tmp_license_txt_file_name, 'w', encoding='utf-8')
338-
tmp_license_txt.write(license_txt_data)
339-
tmp_license_txt.close()
340-
license_name = check_and_run_license_scanner(platform, license_scanner_bin, tmp_license_txt_file_name)
331+
license_name = check_license_name(license_txt_data)
341332
except Exception:
342-
logger.info("Cannot find the license name with license scanner binary.")
343-
344-
if os.path.isfile(tmp_license_txt_file_name):
345-
os.remove(tmp_license_txt_file_name)
333+
logger.info("Cannot find the license name with askalono.")
346334
except Exception:
347335
logger.info("Cannot find the license name with github api.")
348336

349337
return license_name
350338

351339

352-
def check_license_scanner(platform):
353-
license_scanner_bin = ''
354-
355-
if platform == const.LINUX:
356-
license_scanner = _license_scanner_linux
357-
elif platform == const.MACOS:
358-
license_scanner = _license_scanner_macos
359-
elif platform == const.WINDOWS:
360-
license_scanner = _license_scanner_windows
361-
else:
362-
logger.debug("Not supported OS to analyze license text with binary.")
363-
364-
if license_scanner:
365-
try:
366-
base_path = sys._MEIPASS
367-
except Exception:
368-
base_path = os.path.dirname(__file__)
369-
370-
data_path = os.path.join(base_path, license_scanner)
371-
license_scanner_bin = data_path
372-
373-
return license_scanner_bin
374-
375-
376-
def check_and_run_license_scanner(platform, license_scanner_bin, file_dir):
340+
def check_license_name(license_txt, is_filepath=False):
377341
license_name = ''
342+
if is_filepath:
343+
with open(license_txt, 'r', encoding='utf-8') as f:
344+
license_content = f.read()
345+
else:
346+
license_content = license_txt
378347

379-
if not license_scanner_bin:
380-
logger.error('Not supported OS for license scanner binary.')
381-
382-
try:
383-
tmp_output_file_name = "tmp_license_scanner_output.txt"
384-
385-
if file_dir == "UNKNOWN":
386-
license_name = ""
387-
else:
388-
if platform == const.LINUX:
389-
run_license_scanner = f"{license_scanner_bin} {file_dir} > {tmp_output_file_name}"
390-
elif platform == const.MACOS:
391-
run_license_scanner = f"{license_scanner_bin} identify {file_dir} > {tmp_output_file_name}"
392-
elif platform == const.WINDOWS:
393-
run_license_scanner = f"{license_scanner_bin} identify {file_dir} > {tmp_output_file_name}"
394-
else:
395-
run_license_scanner = ''
396-
397-
if run_license_scanner is None:
398-
license_name = ""
399-
return license_name
400-
else:
401-
ret = subprocess.run(run_license_scanner, shell=True, stderr=subprocess.PIPE)
402-
if ret.returncode != 0 or ret.stderr:
403-
os.remove(tmp_output_file_name)
404-
return ""
405-
406-
fp = open(tmp_output_file_name, "r", encoding='utf8')
407-
license_output = fp.read()
408-
fp.close()
409-
410-
if platform == const.LINUX:
411-
license_output_re = re.findall(r'.*contains license\(s\)\s(.*)', license_output)
412-
else:
413-
license_output_re = re.findall(r"License:\s{1}(\S*)\s{1}", license_output)
414-
415-
if len(license_output_re) == 1:
416-
license_name = license_output_re[0]
417-
if license_name == "No_license_found":
418-
license_name = ""
419-
else:
420-
license_name = ""
421-
os.remove(tmp_output_file_name)
422-
423-
except Exception as ex:
424-
logger.error(f"Failed to run license scan binary. {ex}")
425-
license_name = ""
426-
348+
detect_askalono = identify(license_content)
349+
if detect_askalono.score > ASKALONO_THRESHOLD:
350+
license_name = detect_askalono.name
427351
return license_name
428352

429353

src/fosslight_dependency/package_manager/Carthage.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import fosslight_util.constant as constant
1010
import fosslight_dependency.constant as const
1111
from fosslight_dependency._package_manager import PackageManager
12-
from fosslight_dependency._package_manager import connect_github, get_github_license, check_and_run_license_scanner
13-
from fosslight_dependency._package_manager import get_url_to_purl
12+
from fosslight_dependency._package_manager import connect_github, get_github_license
13+
from fosslight_dependency._package_manager import get_url_to_purl, check_license_name
1414
from fosslight_dependency.dependency_item import DependencyItem
1515
from fosslight_util.oss_item import OssItem
1616

@@ -79,17 +79,15 @@ def parse_oss_information(self, f_name):
7979
for license_file_reg in license_file_regs:
8080
match_result = re.match(license_file_reg, filename_in_dir.lower())
8181
if match_result is not None:
82-
license_name = check_and_run_license_scanner(self.platform,
83-
self.license_scanner_bin,
84-
filename_with_checkout_path)
82+
license_name = check_license_name(filename_with_checkout_path, True)
8583
find_license = True
8684
break
8785
if license_name == '':
8886
if repo == github:
8987
try:
9088
if not g:
9189
g = connect_github(self.github_token)
92-
license_name = get_github_license(g, oss_path, self.platform, self.license_scanner_bin)
90+
license_name = get_github_license(g, oss_path)
9391
except Exception as e:
9492
logger.warning(f"Failed to get license with github api: {e}")
9593
license_name == ''

src/fosslight_dependency/package_manager/Nuget.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import fosslight_util.constant as constant
1313
import fosslight_dependency.constant as const
1414
from fosslight_dependency._package_manager import PackageManager
15-
from fosslight_dependency._package_manager import check_and_run_license_scanner, get_url_to_purl
15+
from fosslight_dependency._package_manager import check_license_name, get_url_to_purl
1616
from fosslight_dependency.dependency_item import DependencyItem, change_dependson_to_purl
1717
from fosslight_util.oss_item import OssItem
1818

@@ -73,14 +73,9 @@ def parse_oss_information(self, f_name):
7373
if license_url is not None:
7474
url_res = requests.get(license_url.text)
7575
if url_res.status_code == 200:
76-
tmp_license_txt = open(tmp_license_txt_file_name, 'w', encoding='utf-8')
77-
tmp_license_txt.write(url_res.text)
78-
tmp_license_txt.close()
79-
license_name_with_license_scanner = check_and_run_license_scanner(self.platform,
80-
self.license_scanner_bin,
81-
tmp_license_txt_file_name)
82-
if license_name_with_license_scanner != "":
83-
license_name = license_name_with_license_scanner
76+
license_name_with_scanner = check_license_name(url_res.text)
77+
if license_name_with_scanner != "":
78+
license_name = license_name_with_scanner
8479
else:
8580
license_name = license_url.text
8681
oss_item.license = license_name

src/fosslight_dependency/package_manager/Pub.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import shutil
1111
import yaml
1212
import subprocess
13-
from askalono import identify
1413
import fosslight_util.constant as constant
1514
import fosslight_dependency.constant as const
1615
from fosslight_dependency._package_manager import PackageManager
17-
from fosslight_dependency._package_manager import get_url_to_purl
16+
from fosslight_dependency._package_manager import get_url_to_purl, check_license_name
1817
from fosslight_dependency.dependency_item import DependencyItem, change_dependson_to_purl
1918
from fosslight_util.oss_item import OssItem
2019

@@ -135,9 +134,7 @@ def parse_oss_information(self, f_name):
135134
purl_dict[f'{oss_origin_name}({oss_item.version})'] = dep_item.purl
136135
license_txt = json_data['license']
137136
if license_txt is not None:
138-
detect_askalono = identify(license_txt)
139-
if detect_askalono.score > 0.7:
140-
oss_item.license = detect_askalono.name
137+
oss_item.license = check_license_name(license_txt)
141138

142139
if self.direct_dep:
143140
if oss_origin_name not in self.total_dep_list:

src/fosslight_dependency/package_manager/Pypi.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import fosslight_util.constant as constant
1414
import fosslight_dependency.constant as const
1515
from fosslight_dependency._package_manager import PackageManager
16-
from fosslight_dependency._package_manager import check_and_run_license_scanner, get_url_to_purl
16+
from fosslight_dependency._package_manager import check_license_name, get_url_to_purl
1717
from fosslight_dependency.dependency_item import DependencyItem, change_dependson_to_purl
1818
from fosslight_util.oss_item import OssItem
1919

@@ -302,12 +302,7 @@ def parse_oss_information(self, f_name):
302302
if license_name is not None:
303303
license_name = license_name.replace(';', ',')
304304
else:
305-
license_file_dir = d['LicenseFile']
306-
license_name_with_lic_scanner = check_and_run_license_scanner(self.platform,
307-
self.license_scanner_bin,
308-
license_file_dir)
309-
if license_name_with_lic_scanner != "":
310-
license_name = license_name_with_lic_scanner
305+
license_name = check_license_name(d['LicenseFile'], True)
311306
oss_item.license = license_name
312307

313308
if oss_init_name == self.package_name:

src/fosslight_dependency/package_manager/Swift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def parse_oss_information(self, f_name):
140140
github_repo = "/".join(oss_item.homepage.split('/')[-2:])
141141
dep_item.purl = get_url_to_purl(oss_item.download_location, self.package_manager_name, github_repo, oss_item.version)
142142
purl_dict[f'{oss_origin_name}({oss_item.version})'] = dep_item.purl
143-
oss_item.license = get_github_license(g, github_repo, self.platform, self.license_scanner_bin)
143+
oss_item.license = get_github_license(g, github_repo)
144144

145145
if self.direct_dep and len(self.direct_dep_list) > 0:
146146
if oss_origin_name in self.direct_dep_list:

0 commit comments

Comments
 (0)