forked from KDE/licensedigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicenseregistry.cpp
152 lines (133 loc) · 5.79 KB
/
licenseregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* SPDX-FileCopyrightText: 2019 Andreas Cord-Landwehr <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "licenseregistry.h"
#include <QDebug>
#include <QDir>
#include <QDirIterator>
const QString LicenseRegistry::ToClarifyLicense("TO-CLARIFY");
const QString LicenseRegistry::UnknownLicense("UNKNOWN-LICENSE");
const QString LicenseRegistry::AmbigiousLicense("AMBIGIOUS");
const QString LicenseRegistry::MissingLicense("MISSING-LICENSE");
const QString LicenseRegistry::MissingLicenseForGeneratedFile("MISSING-LICENSE-GENERATED-FILE");
LicenseRegistry::LicenseRegistry(QObject *parent)
: QObject(parent)
{
loadLicenseHeaders();
loadLicenseFiles();
}
void LicenseRegistry::loadLicenseHeaders()
{
if (!m_registry.isEmpty()) {
m_registry.clear();
}
m_registry[LicenseRegistry::UnknownLicense] = QVector<QString> {"THIS IS A STUB HEADER FOR UNKNOWN LICENSES, IT SHALL NEVER MATCH"};
QDirIterator spdxIter(":/licenses_templates/");
while (spdxIter.hasNext()) {
QString filePath = spdxIter.next();
if (!spdxIter.fileInfo().isDir()) {
qWarning() << "A non-directory was found here unexpected:" << spdxIter.fileInfo();
continue;
}
QVector<QString> headerTexts;
QDirIterator headerIter(filePath);
while (headerIter.hasNext()) {
QFile file(headerIter.next());
file.open(QIODevice::ReadOnly);
headerTexts.append(file.readAll());
}
// sort license texts lexicographically decreasing
// this is a simple solution for the problem when one license text is a prefix of another license text
// which is known for license texts with omitted "." at the end
std::sort(headerTexts.begin(), headerTexts.end(), [](const QString &lhs, const QString &rhs) { return lhs > rhs; });
m_registry[spdxIter.fileName()] = headerTexts;
}
}
void LicenseRegistry::loadLicenseFiles()
{
QDirIterator textIter(":/licensetexts/");
while (textIter.hasNext()) {
QString filePath = textIter.next();
if (textIter.fileInfo().isDir()) {
qWarning() << "Unexpected directory found:" << textIter.fileInfo();
continue;
}
QString baseName = textIter.fileName().mid(0, textIter.fileName().length() - 4); // remove ".txt"
m_licenseFiles.insert(baseName, textIter.filePath());
}
}
QVector<LicenseRegistry::SpdxExpression> LicenseRegistry::expressions() const
{
return m_registry.keys().toVector();
}
QVector<LicenseRegistry::SpdxIdentifier> LicenseRegistry::identifiers() const
{
return m_licenseFiles.keys().toVector();
}
QMap<LicenseRegistry::SpdxIdentifier, QString> LicenseRegistry::licenseFiles() const
{
return m_licenseFiles;
}
QVector<QString> LicenseRegistry::headerTexts(const LicenseRegistry::SpdxExpression &identifier) const
{
return m_registry.value(identifier);
}
QVector<QRegularExpression> LicenseRegistry::headerTextRegExps(const SpdxExpression &identifier) const
{
if (!m_registry.contains(identifier)) {
qCritical() << identifier << "identifier not found, returning error matcher";
return QVector<QRegularExpression> {QRegularExpression("DOES_NOT_MATCH_ANY_LICENSE_HEADER")};
}
if (m_regexpsCache.contains(identifier)) {
return m_regexpsCache.value(identifier);
}
QVector<QString> patterns;
// additional to all headers also add the SPDX identifier
for (const QString &header : m_registry.value(identifier)) {
QString pattern(QRegularExpression::escape(header));
// start detection at first word of license string to make detection easier
pattern.replace("\\\n", "[#\\* \\/-]*\\\n[#\\* \\t\\/-]*"); // allow prefixes and suffixes of whitespace mixed with stars or -
// remove line-break pattern from last line
pattern = pattern.left(pattern.length() - QString("[\\* \\/]*\\\n[\\* \\/]*").length());
pattern.append("[\\* ]*");
patterns.append(pattern);
}
QVector<QString>::const_iterator iter = patterns.constBegin();
QString fullPattern = QString("(%1)").arg(*iter);
QVector<QRegularExpression> regexps;
QRegularExpression testExpr;
while (++iter != patterns.constEnd()) {
QString patternItem = QString("|(%1)").arg(*iter);
testExpr.setPattern(fullPattern + patternItem);
if (!testExpr.isValid()) {
regexps += QRegularExpression(fullPattern);
fullPattern = QString("(%1)").arg(*iter);
} else {
fullPattern.append(patternItem);
}
}
regexps += QRegularExpression(fullPattern);
m_regexpsCache[identifier] = regexps;
return regexps;
}
bool LicenseRegistry::isFakeLicenseMarker(const QString &expression) const
{
const QStringList fakeExpressions {LicenseRegistry::ToClarifyLicense, LicenseRegistry::UnknownLicense, LicenseRegistry::MissingLicense, LicenseRegistry::AmbigiousLicense, LicenseRegistry::MissingLicenseForGeneratedFile};
return fakeExpressions.contains(expression);
}