44# SPDX-License-Identifier: Apache-2.0
55
66import os
7- import sys
87import logging
98import platform
109import re
1110import base64
1211import subprocess
1312import shutil
1413import stat
14+ from packageurl .contrib import url2purl
15+ from askalono import identify
1516import fosslight_util .constant as constant
1617import fosslight_dependency .constant as const
17- from packageurl .contrib import url2purl
1818
1919try :
2020 from github import Github
2323
2424logger = 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-
3126gradle_config = ['runtimeClasspath' , 'runtime' ]
3227android_config = ['releaseRuntimeClasspath' ]
28+ ASKALONO_THRESHOLD = 0.7
3329
3430
3531class 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
0 commit comments