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

spdx export #57

Open
wants to merge 3 commits into
base: master
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
15 changes: 12 additions & 3 deletions debsources/app/copyright/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
from __future__ import absolute_import


from flask import jsonify, request, render_template
from flask import jsonify, request, render_template, make_response

from ..helper import bind_render, generic_before_request
from . import bp_copyright
from ..views import (IndexView, PrefixView, ListPackagesView, ErrorHandler,
Ping, PackageVersionsView, SearchView)
from .views import LicenseView, ChecksumLicenseView, SearchFileView, StatsView
from .views import (LicenseView, ChecksumLicenseView, SearchFileView,
StatsView, SPDXView)
from debsources.excepts import Http404Error


Expand All @@ -39,7 +40,7 @@ def skeleton_variables():
# Before request
@bp_copyright.before_request
def before_request():
endpoints = ['license', 'file', 'api_file']
endpoints = ['license', 'file', 'api_file', 'spdx']
if request.endpoint.replace('copyright.', '', 1) in endpoints:
try:
return generic_before_request(request, 3)
Expand Down Expand Up @@ -224,3 +225,11 @@ def before_request():
render_func=jsonify,
err_func=ErrorHandler(mode='json'),
get_objects='stats_suite'))

# SDPX view
bp_copyright.add_url_rule(
'/spdx/<string:packagename>/<string:version>/',
view_func=SPDXView.as_view(
'spdx',
render_func=make_response,
err_func=ErrorHandler('copyright')))
1 change: 1 addition & 0 deletions debsources/app/copyright/templates/copyright/license.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h2>{{ self.title() }} / {{ version }}</h2>
{% if dump == 'True' %}
{% include "source_file_code.inc.html" %}
{% else %}
<div class="warning"><a href="{{url_for('.spdx', packagename=package, version=version) }}">Export to SPDX</a></div>
{% include "copyright/license_render.inc.html" %}
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{#
Copyright (C) 2016 The Debsources developers <[email protected]>.
See the AUTHORS file at the top-level directory of this distribution and at
https://anonscm.debian.org/gitweb/?p=qa/debsources.git;a=blob;f=AUTHORS;hb=HEAD
License: GNU Affero General Public License, version 3 or above.
#}
{% extends name+"/base.html" %}

{% block title %}Error{% endblock %}
{% block content %}
<h2>{{ self.title() }}</h2>
<p>The debian/copyright file has a file paragraph without the <b>required</b> copyright field. The files paragraph is:
<ul>
{% for files in paragraph %}
<li>{{ files }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('sources.source', path_to=(package +'/'+version+'/debian/copyright'))}}">View raw copyright file</a>
</p>
<a href="{{ url_for('.license', packagename=package, version=version)}}">Go back to the license page</a>

{% endblock %}
20 changes: 20 additions & 0 deletions debsources/app/copyright/templates/copyright/value_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{#
Copyright (C) 2016 The Debsources developers <[email protected]>.
See the AUTHORS file at the top-level directory of this distribution and at
https://anonscm.debian.org/gitweb/?p=qa/debsources.git;a=blob;f=AUTHORS;hb=HEAD
License: GNU Affero General Public License, version 3 or above.
#}
{% extends name+"/base.html" %}

{% block title %}Error{% endblock %}
{% block content %}
<h2>{{ self.title() }}</h2>
<p>Parsing the debian/copyright file failed due to one or more of the following causes:
<ul>
<li>continued line must begin with " "</li>
<li>missing value in one of the required attributes (Files, Copyright, License)</li>
</ul>

<p><a href="{{ url_for('sources.source', packagename=package, version=version) }}">View raw copyright file</a></p>
</p>
{% endblock %}
54 changes: 46 additions & 8 deletions debsources/app/copyright/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import debsources.statistics as statistics
from debsources.excepts import (Http404ErrorSuggestions, FileOrFolderNotFound,
InvalidPackageOrVersionError,
Http404MissingCopyright, Http404Error)
Http404MissingCopyright, Http404Error,
CopyrightValueError)
from ..views import GeneralView, ChecksumView, session, app
from ..sourcecode import SourceCodeIterator
from ..pagination import Pagination
Expand Down Expand Up @@ -52,13 +53,16 @@ def get_objects(self, packagename, version):
code=sourcefile,
dump='True',
nlines=sourcefile.get_number_of_lines(),)
return dict(package=packagename,
version=version,
dump='False',
header=helper.get_copyright_header(c),
files=helper.parse_copyright_paragraphs_for_html_render(
c, "/src/" + packagename + "/" + version + "/"),
licenses=helper.parse_licenses_for_html_render(c))
try:
return dict(package=packagename,
version=version,
dump='False',
header=helper.get_copyright_header(c),
files=helper.parse_copyright_paragraphs_html_render(
c, "/src/" + packagename + "/" + version + "/"),
licenses=helper.parse_licenses_for_html_render(c))
except ValueError as e:
raise CopyrightValueError(packagename, version, e.message)


class ChecksumLicenseView(ChecksumView):
Expand Down Expand Up @@ -299,3 +303,37 @@ def get_stats(self):
dual_results=dual_res,
dual_licenses=sorted(dual_licenses),
suites=all_suites)


class SPDXView(GeneralView):

def _generate_file(self, spdx_values):
output = ''
for value in spdx_values:
output += value.decode('utf-8') + '\n'
return output

def get_objects(self, packagename, version):
try:
sources_path = helper.get_sources_path(session, packagename,
version,
current_app.config)
except FileOrFolderNotFound:
raise Http404ErrorSuggestions(packagename, version,
'debian/copyright')
except InvalidPackageOrVersionError:
raise Http404ErrorSuggestions(packagename, version, '')

try:
c = helper.parse_license(sources_path)
except Exception:
# non machine readable license
return dict(return_code=404)

spdx = helper.export_copyright_to_spdx(
c, session=session, package=packagename,
version=version)
attachment = "attachment;" + "filename=" + \
packagename + '_' + version + ".spdx"
return dict(spdx=self._generate_file(spdx),
header=attachment)
20 changes: 18 additions & 2 deletions debsources/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import six

from flask import (
current_app, jsonify, render_template, request, url_for, redirect)
current_app, jsonify, render_template, request, url_for, redirect,
make_response)
from flask.views import View

from debsources.excepts import (
Http500Error, Http404Error, Http404ErrorSuggestions, Http403Error,
InvalidPackageOrVersionError, Http404MissingCopyright)
InvalidPackageOrVersionError, Http404MissingCopyright,
MissingCopyrightField, CopyrightValueError)
from debsources.models import Package
import debsources.query as qry
from debsources.sqla_session import _close_session
Expand Down Expand Up @@ -126,6 +128,16 @@ def error_404(self, error):
else:
return render_template('copyright/404_missing.html',
suggestions=suggestions), 404
elif isinstance(error, MissingCopyrightField):
return render_template('copyright/missing_copyright.html',
paragraph=error.par,
package=error.package,
version=error.version)
elif isinstance(error, CopyrightValueError):
return render_template('copyright/value_error.html',
error=error.error,
package=error.package,
version=error.version)
else:
return render_template('404.html'), 404

Expand Down Expand Up @@ -189,6 +201,10 @@ def dispatch_request(self, **kwargs):
"""
try:
context = self.get_objects(**kwargs)
if self.render_func is make_response:
response = make_response(context['spdx'])
response.headers["Content-Disposition"] = context['header']
return response
return self.render_func(**context)
except Http403Error as e:
return self.err_func(e, http=403)
Expand Down
16 changes: 16 additions & 0 deletions debsources/excepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ def __init__(self, package, version, path):

class Http403Error(Exception):
pass


class MissingCopyrightField(Http404Error):
def __init__(self, package, version, par):
self.package = package
self.version = version
self.par = par
super(MissingCopyrightField, self).__init__()


class CopyrightValueError(Http404Error):
def __init__(self, package, version, error):
self.package = package
self.version = version
self.error = error
super(CopyrightValueError, self).__init__()
Loading