Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update generateXml.py #559

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Django==3.2.25
jpype1==1.3.0
numpy==1.22.4
djangorestframework==3.12.2
Django>=4.2,<4.3
jpype1==1.4.0
numpy>=1.25.0
djangorestframework>=3.14.0
bs4==0.0.1
requests==2.32.3
lxml==4.9.4
webdriver-manager==3.4.2
social-auth-core==4.1.0
social-auth-app-django==4.0.0
python-dotenv==0.17.1
lxml>=5.3.0
webdriver-manager>=3.8.6
social-auth-core>=4.3,<5.0
social-auth-app-django>=5.0,<6.0
python-dotenv>=1.0.0
gevent==23.9.1
gunicorn==22.0.0
gunicorn==23.0.0
#for testing
selenium==3.14.1
django-oauth-toolkit==1.5.0
django-rest-framework-social-oauth2==1.1.0
selenium>=4.10.0
django-oauth-toolkit>=2.0.0
django-rest-framework-social-oauth2>=1.1.0
spdx-tools==0.8.3
ntia-conformance-checker==3.0.2
ntia-conformance-checker==3.1.0
-e git+https://github.com/spdx/[email protected]#egg=spdx-license-matcher
56 changes: 28 additions & 28 deletions src/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def license_compare_helper(request):
compareclass.onlineFunction(callfunc)
except Exception as ex:
"""Error raised by onlineFunction"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a util func for this and use that.

def is_ajax(request):
    return request.headers.get('x-requested-with') == 'XMLHttpRequest'

Also, make sure this works instead of
request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

ajaxdict["type"] = "warning2"
ajaxdict["files"] = filelist
ajaxdict["errors"] = errorlist
Expand All @@ -122,7 +122,7 @@ def license_compare_helper(request):
return result
if (warningoccurred==False):
"""If no warning raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["medialink"] = settings.MEDIA_URL + folder + "/"+ rfilename
response = dumps(ajaxdict)
result['response'] = response
Expand All @@ -134,7 +134,7 @@ def license_compare_helper(request):
result['context'] = context_dict
return result
else :
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "warning"
ajaxdict["files"] = filelist
ajaxdict["errors"] = errorlist
Expand All @@ -151,7 +151,7 @@ def license_compare_helper(request):
result['context'] = context_dict
return result
else :
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["files"] = filelist
ajaxdict["type"] = "error"
ajaxdict["errors"] = errorlist
Expand All @@ -173,7 +173,7 @@ def license_compare_helper(request):

except MultiValueDictKeyError:
""" If no files uploaded"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
filelist.append("Files not selected.")
errorlist.append("Please select at least 2 files.")
ajaxdict["files"] = filelist
Expand Down Expand Up @@ -220,7 +220,7 @@ def ntia_check_helper(request):
retval = tempstdout.getvalue().replace(",",", ").replace("\n","<br/>")
if not retval.startswith("No components with missing information."):
""" If any warnings are returned """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "warning"
warnings = str(retval)
ajaxdict["data"] = "The following warning(s) were raised:<br />\n" + warnings.replace('\n', '<br />\n')
Expand All @@ -232,7 +232,7 @@ def ntia_check_helper(request):
result['context'] = context_dict
result['status'] = 400
return result
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
""" Valid SPDX Document """
ajaxdict["data"] = "This SPDX Document is valid:\n" + retval
response = dumps(ajaxdict)
Expand All @@ -245,7 +245,7 @@ def ntia_check_helper(request):
return result
else:
""" If no file uploaded."""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = "No file uploaded"
Expand All @@ -259,7 +259,7 @@ def ntia_check_helper(request):
return result
except jpype.JException as ex:
""" Error raised by check_anything.check_minimum_elements without exiting the application"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = jpype.JException.message(ex)
Expand All @@ -273,7 +273,7 @@ def ntia_check_helper(request):
return result
except MultiValueDictKeyError:
""" If no files selected"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = "No files selected."
Expand All @@ -287,7 +287,7 @@ def ntia_check_helper(request):
return result
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = format_exc()
Expand Down Expand Up @@ -327,7 +327,7 @@ def license_validate_helper(request):
retval = verifyclass.verify(str(settings.APP_DIR+uploaded_file_url), fileformat)
if (len(retval) > 0):
""" If any warnings are returned """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "warning"
warnings = str(retval)
ajaxdict["data"] = "The following warning(s) were raised:<br />\n" + warnings.replace('\n', '<br />\n')
Expand All @@ -339,7 +339,7 @@ def license_validate_helper(request):
result['context'] = context_dict
result['status'] = 400
return result
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
""" Valid SPDX Document """
ajaxdict["data"] = "This SPDX Document is valid."
response = dumps(ajaxdict)
Expand All @@ -352,7 +352,7 @@ def license_validate_helper(request):
return result
else :
""" If no file uploaded."""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict=dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = "No file uploaded"
Expand All @@ -366,7 +366,7 @@ def license_validate_helper(request):
return result
except jpype.JException as ex :
""" Error raised by verifyclass.verify without exiting the application"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict=dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = jpype.JException.message(ex)
Expand All @@ -380,7 +380,7 @@ def license_validate_helper(request):
return result
except MultiValueDictKeyError:
""" If no files selected"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict=dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = "No files selected."
Expand All @@ -394,7 +394,7 @@ def license_validate_helper(request):
return result
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict=dict()
ajaxdict["type"] = "error"
ajaxdict["data"] = format_exc()
Expand All @@ -419,7 +419,7 @@ def license_check_helper(request):
try:
matchingId, matchingType, _ = utils.check_spdx_license(licensetext)
if not matchingId:
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["data"] = "There are no matching SPDX listed licenses"
response = dumps(ajaxdict)
Expand All @@ -435,7 +435,7 @@ def license_check_helper(request):
if isinstance(matchingId, list):
matchingId = ",".join(matchingId)
matching_str += matchingId
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["data"] = matching_str
response = dumps(ajaxdict)
Expand All @@ -448,7 +448,7 @@ def license_check_helper(request):
return result
except jpype.JException as ex :
""" Java exception raised without exiting the application """
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["data"] = jpype.JException.message(ex)
response = dumps(ajaxdict)
Expand All @@ -461,7 +461,7 @@ def license_check_helper(request):
return result
except Exception as ex:
""" Other exception raised """
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["data"] = format_exc()
response = dumps(ajaxdict)
Expand Down Expand Up @@ -508,7 +508,7 @@ def license_convert_helper(request):
warnings = verifyclass.verify(str(settings.MEDIA_ROOT+"/"+folder+"/"+"/"+convertfile), toFileFormat)
if (len(warnings) == 0) :
""" If no warnings raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["medialink"] = settings.MEDIA_URL + folder + "/"+ convertfile
response = dumps(ajaxdict)
result['response'] = response
Expand All @@ -520,7 +520,7 @@ def license_convert_helper(request):
result['status'] = 200
return result
else :
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "warning"
warnings = str(warnings)
ajaxdict["data"] = "The following warning(s) were raised by "+ myfile.name + ":<br />\n" + warnings.replace('\n', '<br />\n')
Expand All @@ -545,7 +545,7 @@ def license_convert_helper(request):
return result
except jpype.JException as ex :
""" Java exception raised without exiting the application"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "error"
ajaxdict["data"] = jpype.JException.message(ex)
response = dumps(ajaxdict)
Expand All @@ -559,7 +559,7 @@ def license_convert_helper(request):
return result
except MultiValueDictKeyError:
""" If no files uploaded"""
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "error"
ajaxdict["data"] = "No files selected."
response = dumps(ajaxdict)
Expand All @@ -573,7 +573,7 @@ def license_convert_helper(request):
return result
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict["type"] = "error"
ajaxdict["data"] = format_exc()
response = dumps(ajaxdict)
Expand Down Expand Up @@ -615,7 +615,7 @@ def license_diff_helper(request):
return data
except jpype.JException as ex :
""" Java exception raised without exiting the application """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict=dict()
ajaxdict["data"] = jpype.JException.message(ex)
response = dumps(ajaxdict)
Expand All @@ -628,7 +628,7 @@ def license_diff_helper(request):
return data
except Exception as ex:
""" Other exception raised """
if (request.is_ajax()):
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
ajaxdict = dict()
ajaxdict["data"] = format_exc()
response = dumps(ajaxdict)
Expand Down
23 changes: 18 additions & 5 deletions src/app/generateXml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
import re
import xml.etree.ElementTree as ET
from xml.dom import minidom
from itertools import chain, tee

entityMap = {
Expand All @@ -23,7 +23,7 @@ def previous_and_current(some_iterable):

def escapeXmlData(string):
for initial,final in list(entityMap.items()):
string.replace(initial, final)
string = string.replace(initial, final) # assign changed string back
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

return string


Expand All @@ -45,8 +45,9 @@ def wrapBullets(string, item):
numberBullet = re.search(numberBullets, string)
symbolBullet = re.search(symbolBullets, string)
bullet = letterBullet or numberBullet or symbolBullet
ET.SubElement(item, "bullet").text = bullet.group(2)
string = string.replace(bullet.group(2), '').strip()
if bullet: # Bullet must exist
ET.SubElement(item, "bullet").text = bullet.group(2)
string = string.replace(bullet.group(2), '').strip()
return string


Expand All @@ -61,7 +62,7 @@ def groupLines(lines):
if not matches:
depth = 0
else:
depth = int(math.floor(len(matches)/4))
depth = len(matches) // 4 # no need to use math.floor()
tagType = 'item'
lis.append({'data':line, 'depth':depth, 'tagType':tagType})
else:
Expand Down Expand Up @@ -154,4 +155,16 @@ def generateLicenseXml(licenseOsi, licenseIdentifier, licenseName, listVersionAd
textElement = getTextElement(points)
license.append(textElement)
xmlString = ET.tostring(root, method='xml', encoding='unicode')

# Format the XML string with indentation for better readability
xmlString = minidom.parseString(xmlString).toprettyxml(indent=" ")

# Remove extra blank lines added by `toprettyxml()`, ensuring clean output
clean_lines = []
for line in xmlString.splitlines():
if line.strip(): # Only keep non-empty lines
clean_lines.append(line)

xmlString = "\n".join(clean_lines)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this code block do? Can you provide an example?


return xmlString
13 changes: 9 additions & 4 deletions src/app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
import time
import datetime
Expand Down Expand Up @@ -648,7 +649,8 @@ class LicenseXMLEditorTestCase(StaticLiveServerTestCase):
def setUp(self):
options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
service = Service(GeckoDriverManager().install())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define this globally, instead of defining for each setUp

self.selenium = webdriver.Firefox(service=service, options=options)
self.initialXML = '<?xml version="1.0" encoding="UTF-8"?><SPDXLicenseCollection xmlns="http://www.spdx.org/license"><license></license></SPDXLicenseCollection>'
self.invalidXML = '<?xml version="1.0" encoding="UTF-8"?><SPDXLicenseCollection xmlns="http://www.spdx.org/license"><license></license>'
super(LicenseXMLEditorTestCase, self).setUp()
Expand Down Expand Up @@ -1080,7 +1082,8 @@ class ArchiveLicenseRequestsViewsTestCase(StaticLiveServerTestCase):
def setUp(self):
options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
service = Service(GeckoDriverManager().install())
self.selenium = webdriver.Firefox(service=service, options=options)
super(ArchiveLicenseRequestsViewsTestCase, self).setUp()

def tearDown(self):
Expand Down Expand Up @@ -1273,7 +1276,8 @@ class PromoteLicenseNamespaceViewsTestCase(StaticLiveServerTestCase):
def setUp(self):
options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
service = Service(GeckoDriverManager().install())
self.selenium = webdriver.Firefox(service=service, options=options)
#login
TEST_LOGIN_INFO = {
"provider": "github",
Expand Down Expand Up @@ -1349,7 +1353,8 @@ class ArchiveLicenseNamespaceViewsTestCase(StaticLiveServerTestCase):
def setUp(self):
options = Options()
options.add_argument('-headless')
self.selenium = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_options=options)
service = Service(GeckoDriverManager().install())
self.selenium = webdriver.Firefox(service=service, options=options)
super(ArchiveLicenseNamespaceViewsTestCase, self).setUp()

def tearDown(self):
Expand Down
Loading