Skip to content

Commit

Permalink
Run License Detection On Text Submission with tempfile #450
Browse files Browse the repository at this point in the history
* license_scanview uses tempfile to run license detection on the
  provided input license text

Signed-off-by: Akhil Raj <[email protected]>
  • Loading branch information
lf32 committed Jun 20, 2022
1 parent 747222d commit 64ffe39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
1 change: 0 additions & 1 deletion scantext/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@


class ScantextConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "scantext"
41 changes: 19 additions & 22 deletions scantext/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

import pprint
import sys
import tempfile

from django.conf import settings
from django.shortcuts import render
from django.views import generic

from scantext.forms import EditorForm
from scantext.forms import LicenseForm

SCANCODE_BASE_URL = "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses"
SCANCODE_LICENSE_TEXT_URL = SCANCODE_BASE_URL + "/{}.LICENSE"
Expand All @@ -39,36 +38,34 @@


def license_scanview(request):
form = EditorForm()
form = LicenseForm()
if request.method == "POST":
form = EditorForm(request.POST)
form = LicenseForm(request.POST)
if form.is_valid():
text = form.cleaned_data["input_text"]
# license_location = tempfile.NamedTemporaryFile(mode="w", prefix="license_scan_", dir=settings.SCANCODEIO_WORKSPACE_LOCATION)
# with license_location as f:
# f.write(text)
# f.flush()
# x=get_licenses(location=f)
# f.close()
# the get_licenses in the above code (line 56) returns this error
# error:
# expected str, bytes or os.PathLike object, not _TemporaryFileWrapper
# the below code just works

expressions = get_licenses(
location="scantext/tests/data/LICENSES",
include_text=True,
license_text_diagnostics=True,
)
with tempfile.NamedTemporaryFile(
mode="w",
prefix="license_scan_",
dir=settings.SCANCODEIO_WORKSPACE_LOCATION,
) as temp_file:
temp_file.write(text)
temp_file.flush()
expressions = get_licenses(
location=temp_file.name,
include_text=True,
license_text_diagnostics=True,
)
temp_file.close()

return render(
request,
"scantext/license_detail.html",
{
"text": text,
"expr": expressions,
"result": expressions,
},
)
return render(request, "scantext/license_scan.html", {"form": form})
return render(request, "scantext/license_scan_form.html", {"form": form})


def get_licenses(
Expand Down

0 comments on commit 64ffe39

Please sign in to comment.