Skip to content

Commit 64ffe39

Browse files
committed
Run License Detection On Text Submission with tempfile #450
* license_scanview uses tempfile to run license detection on the provided input license text Signed-off-by: Akhil Raj <lf32.dev@gmail.com>
1 parent 747222d commit 64ffe39

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

scantext/apps.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424

2525

2626
class ScantextConfig(AppConfig):
27-
default_auto_field = "django.db.models.BigAutoField"
2827
name = "scantext"

scantext/views.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

23-
import pprint
2423
import sys
2524
import tempfile
2625

2726
from django.conf import settings
2827
from django.shortcuts import render
2928
from django.views import generic
3029

31-
from scantext.forms import EditorForm
30+
from scantext.forms import LicenseForm
3231

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

4039

4140
def license_scanview(request):
42-
form = EditorForm()
41+
form = LicenseForm()
4342
if request.method == "POST":
44-
form = EditorForm(request.POST)
43+
form = LicenseForm(request.POST)
4544
if form.is_valid():
4645
text = form.cleaned_data["input_text"]
47-
# license_location = tempfile.NamedTemporaryFile(mode="w", prefix="license_scan_", dir=settings.SCANCODEIO_WORKSPACE_LOCATION)
48-
# with license_location as f:
49-
# f.write(text)
50-
# f.flush()
51-
# x=get_licenses(location=f)
52-
# f.close()
53-
# the get_licenses in the above code (line 56) returns this error
54-
# error:
55-
# expected str, bytes or os.PathLike object, not _TemporaryFileWrapper
56-
# the below code just works
57-
58-
expressions = get_licenses(
59-
location="scantext/tests/data/LICENSES",
60-
include_text=True,
61-
license_text_diagnostics=True,
62-
)
46+
with tempfile.NamedTemporaryFile(
47+
mode="w",
48+
prefix="license_scan_",
49+
dir=settings.SCANCODEIO_WORKSPACE_LOCATION,
50+
) as temp_file:
51+
temp_file.write(text)
52+
temp_file.flush()
53+
expressions = get_licenses(
54+
location=temp_file.name,
55+
include_text=True,
56+
license_text_diagnostics=True,
57+
)
58+
temp_file.close()
59+
6360
return render(
6461
request,
6562
"scantext/license_detail.html",
6663
{
6764
"text": text,
68-
"expr": expressions,
65+
"result": expressions,
6966
},
7067
)
71-
return render(request, "scantext/license_scan.html", {"form": form})
68+
return render(request, "scantext/license_scan_form.html", {"form": form})
7269

7370

7471
def get_licenses(

0 commit comments

Comments
 (0)