Skip to content

Commit

Permalink
Refactor scan_file to reuse scancode-toolkit scan_resource and benefi…
Browse files Browse the repository at this point in the history
…t from timeout support #135

Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Apr 7, 2021
1 parent 2fe9be0 commit 80d310e
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions scanpipe/pipes/scancode.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
from extractcode.extract import extract_file
from packageurl import PackageURL
from scancode import ScancodeError
from scancode import Scanner
from scancode import api as scancode_api
from scancode import cli as scancode_cli

from scanpipe import pipes
from scanpipe.models import CodebaseResource
Expand Down Expand Up @@ -122,26 +124,23 @@ def get_resource_info(location):

def scan_file(location):
"""
Run a license, copyright, email, and url scan functions on provided `location`.
Return a dict of `scan_results` and a list of `scan_errors`.
Run a license, copyright, email, and url scan functions on provided `location`,
using the scancode-toolkit scan_resource method to support timeout.
Return a dict of scan `results` and a list of `errors`.
"""
scan_functions = [
scancode_api.get_copyrights,
partial(scancode_api.get_licenses, include_text=True),
scancode_api.get_emails,
scancode_api.get_urls,
scanners = [
Scanner("copyrights", scancode_api.get_copyrights),
Scanner("licenses", partial(scancode_api.get_licenses, include_text=True)),
Scanner("emails", scancode_api.get_emails),
Scanner("urls", scancode_api.get_urls),
]

scan_results = {}
scan_errors = []

for function in scan_functions:
try:
scan_results.update(function(location))
except Exception as scan_error:
scan_errors.append(scan_error)
# `rid` is not needed in this context, yet required in the scan_resource args
location_rid = location, 0
_, _, errors, _, results, _ = scancode_cli.scan_resource(location_rid, scanners)

return scan_results, scan_errors
return results, errors


def scan_file_and_save_results(codebase_resource):
Expand Down

0 comments on commit 80d310e

Please # to comment.