Import licenses from SPDX

Fixes #326
This commit is contained in:
rubenwardy 2021-07-31 21:03:45 +01:00
parent 2f2141f524
commit aae546a08e
7 changed files with 98 additions and 9 deletions

@ -18,6 +18,7 @@
import os
from typing import List
import requests
from celery import group
from flask import *
from sqlalchemy import or_
@ -232,3 +233,50 @@ def remind_outdated():
url_for('todo.view_user', username=user.username))
db.session.commit()
@action("Import licenses from SPDX")
def import_licenses():
renames = {
"GPLv2" : "GPL-2.0-only",
"GPLv3" : "GPL-3.0-only",
"AGPLv2" : "AGPL-2.0-only",
"AGPLv3" : "AGPL-3.0-only",
"LGPLv2.1" : "LGPL-2.1-only",
"LGPLv3" : "LGPL-3.0-only",
"Apache 2.0" : "Apache-2.0",
"BSD 2-Clause / FreeBSD": "BSD-2-Clause-FreeBSD",
"BSD 3-Clause" : "BSD-3-Clause",
"CC0": "CC0-1.0",
"CC BY 3.0": "CC-BY-3.0",
"CC BY 4.0": "CC-BY-4.0",
"CC BY-NC-SA 3.0": "CC-BY-NC-SA-3.0",
"CC BY-SA 3.0": "CC-BY-SA-3.0",
"CC BY-SA 4.0": "CC-BY-SA-4.0",
"NPOSLv3": "NPOSL-3.0",
"MPL 2.0": "MPL-2.0",
"EUPLv1.2": "EUPL-1.2",
"SIL Open Font License v1.1": "OFL-1.1",
}
for old_name, new_name in renames.items():
License.query.filter_by(name=old_name).update({ "name": new_name })
r = requests.get(
"https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json")
licenses = r.json()["licenses"]
existing_licenses = {}
for license in License.query.all():
assert license.name not in renames.keys()
existing_licenses[license.name.lower()] = license
for license in licenses:
obj = existing_licenses.get(license["licenseId"].lower())
if obj:
obj.url = license["reference"]
elif license.get("isOsiApproved") and license.get("isFsfLibre") and \
not license["isDeprecatedLicenseId"]:
obj = License(license["licenseId"], True, license["reference"])
db.session.add(obj)
db.session.commit()

@ -18,10 +18,11 @@
from flask import *
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.fields.html5 import URLField
from wtforms.validators import *
from app.models import *
from app.utils import rank_required
from app.utils import rank_required, nonEmptyOrNone
from . import bp
@ -33,6 +34,7 @@ def license_list():
class LicenseForm(FlaskForm):
name = StringField("Name", [InputRequired(), Length(3,100)])
is_foss = BooleanField("Is FOSS")
url = URLField("URL", [Optional], filters=[nonEmptyOrNone])
submit = SubmitField("Save")
@bp.route("/licenses/new/", methods=["GET", "POST"])

@ -98,7 +98,7 @@ def logout():
class RegisterForm(FlaskForm):
display_name = StringField("Display Name", [Optional(), Length(1, 20)], filters=[lambda x: nonEmptyOrNone(x)])
display_name = StringField("Display Name", [Optional(), Length(1, 20)], filters=[nonEmptyOrNone])
username = StringField("Username", [InputRequired(),
Regexp("^[a-zA-Z0-9._-]+$", message="Only a-zA-Z0-9._ allowed")])
email = StringField("Email", [InputRequired(), Email()])

@ -35,10 +35,12 @@ class License(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False, unique=True)
is_foss = db.Column(db.Boolean, nullable=False, default=True)
url = db.Column(db.String(128), nullable=True, default=None)
def __init__(self, v, is_foss=True):
def __init__(self, v: str, is_foss: bool = True, url: str = None):
self.name = v
self.is_foss = is_foss
self.url = url
def __str__(self):
return self.name

@ -12,12 +12,13 @@
<a class="btn btn-primary float-right" href="{{ url_for('admin.create_edit_license') }}">New License</a>
<a class="btn btn-secondary mb-4" href="{{ url_for('admin.license_list') }}">Back to list</a>
{% from "macros/forms.html" import render_field, render_submit_field %}
{% from "macros/forms.html" import render_field, render_checkbox_field, render_submit_field %}
<form method="POST" action="" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{{ render_field(form.name) }}
{{ render_field(form.is_foss) }}
{{ render_checkbox_field(form.is_foss) }}
{{ render_field(form.url) }}
{{ render_submit_field(form.submit) }}
</form>
{% endblock %}

@ -16,6 +16,14 @@
{% endif %}
{% endblock %}
{% macro render_license(license) %}
{% if license.url %}
<a href="{{ license.url }}">{{ license.name }}</a>
{% else %}
{{ license.name }}
{% endif %}
{% endmacro %}
{% block container %}
{% if not package.license.is_foss and not package.media_license.is_foss and package.type != package.type.TXP %}
{% set package_warning="Non-free code and media" %}
@ -363,12 +371,12 @@
<dt>{{ _("License") }}</dt>
<dd>
{% if package.license == package.media_license %}
{{ package.license.name }}
{{ render_license(package.license) }}
{% elif package.type == package.type.TXP %}
{{ package.media_license.name }}
{{ render_license(package.media_license) }}
{% else %}
{{ package.license.name }} for code,<br />
{{ package.media_license.name }} for media.
{{ render_license(package.license) }} for code,<br />
{{ render_license(package.media_license) }} for media.
{% endif %}
</dd>
<dt>Added</dt>

@ -0,0 +1,28 @@
"""empty message
Revision ID: 725ff70ea316
Revises: 51be0401bb85
Create Date: 2021-07-31 19:10:36.683434
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '725ff70ea316'
down_revision = '51be0401bb85'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('license', sa.Column('url', sa.String(length=128), nullable=True, default=None))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('license', 'url')
# ### end Alembic commands ###