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 function 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 7e11c91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 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: 20 additions & 21 deletions scantext/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import tempfile

from django.conf import settings
from django.http import HttpResponseRedirect
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 +40,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 7e11c91

Please sign in to comment.