Commit b5fe2b28 authored by Matthias Klumpp's avatar Matthias Klumpp
Browse files

Add function to test if a license string is for free software

parent 3b1a4c12
......@@ -645,3 +645,58 @@ as_get_license_url (const gchar *license)
return g_strdup_printf ("https://spdx.org/licenses/%s.html#page", license_id->str);
}
/**
* as_license_is_free_license:
* @license: The SPDX license string to test.
*
* Check if the given license is for free-as-in-freedom software.
* Currently, all licenses listed on the SPDX website are considered to
* be "free software" licenses.
*
* This definition may be tightened in future. In any case, this function does
* not give any legal advice. Please read the license texts to know more about
* the individual licenses.
*
* Returns: %TRUE if the license string contains only free-as-in-freedom licenses.
*/
gboolean
as_license_is_free_license (const gchar *license)
{
g_auto(GStrv) tokens = NULL;
gboolean is_free;
/* assume we have a free software license, unless proven otherwise */
is_free = TRUE;
tokens = as_spdx_license_tokenize (license);
for (guint i = 0; tokens[i] != NULL; i++) {
if (g_strcmp0 (tokens[i], "&") == 0 ||
g_strcmp0 (tokens[i], "+") == 0 ||
g_strcmp0 (tokens[i], "|") == 0 ||
g_strcmp0 (tokens[i], "^") == 0 ||
g_strcmp0 (tokens[i], "(") == 0 ||
g_strcmp0 (tokens[i], ")") == 0)
continue;
if (g_str_has_prefix (tokens[i], "@LicenseRef")) {
/* we only consider license ref's to be free if they explicitly state so */
if (!g_str_has_prefix (tokens[i], "@LicenseRef-free")) {
is_free = FALSE;
break;
}
} else if (g_str_has_prefix (tokens[i], "@NOASSERTION")
|| g_str_has_prefix (tokens[i], "@NONE")) {
/* no license info is fishy as well */
is_free = FALSE;
break;
}
if (tokens[i][0] != '@') {
/* if token has no license-id prefix, consider the license to be non-free */
is_free = FALSE;
break;
}
}
return is_free;
}
......@@ -39,6 +39,7 @@ gchar *as_spdx_license_detokenize (gchar **license_tokens);
gchar *as_license_to_spdx_id (const gchar *license);
gboolean as_license_is_metadata_license (const gchar *license);
gboolean as_license_is_free_license (const gchar *license);
gchar *as_get_license_url (const gchar *license);
......
......@@ -344,6 +344,14 @@ test_spdx (void)
tmp = as_get_license_url ("LicenseRef-proprietary=https://example.com/mylicense.txt");
g_assert_cmpstr (tmp, ==, "https://example.com/mylicense.txt");
g_free (tmp);
/* licenses are free-as-in-freedom */
g_assert (as_license_is_free_license ("CC0"));
g_assert (as_license_is_free_license ("GPL-2.0 AND FSFAP"));
g_assert (as_license_is_free_license ("OFL-1.1 OR (GPL-3.0-or-later WITH Font-exception-2.0)"));
g_assert (!as_license_is_free_license ("NOASSERTION"));
g_assert (!as_license_is_free_license ("LicenseRef-proprietary=https://example.com/mylicense.txt"));
g_assert (!as_license_is_free_license ("MIT AND LicenseRef-proprietary=https://example.com/lic.txt"));
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment