8 #include <boost/tokenizer.hpp>
9 #include "ninkawrapper.hpp"
12 string scanFileWithNinka(
const State& state,
const fo::File& file)
19 if (!(in = popen(command.c_str(),
"r")))
21 cout <<
"could not execute ninka command: " << command << endl;
32 cout <<
"could not execute ninka command: " << command << endl;
39 vector<string> extractLicensesFromNinkaResult(
string ninkaResult)
41 string licensePart = extractLicensePartFromNinkaResult(ninkaResult);
42 return splitLicensePart(licensePart);
46 string extractLicensePartFromNinkaResult(
string ninkaResult)
48 string delimiters =
";\r\n";
50 size_t first = ninkaResult.find_first_of(delimiters);
51 size_t last = ninkaResult.find_first_of(delimiters, first + 1);
53 return ninkaResult.substr(first + 1, last - 1 - first);
56 vector<string> splitLicensePart(
string licensePart)
58 typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
59 boost::char_separator<char> separator(
",");
60 tokenizer tokens(licensePart, separator);
62 vector<string> licenses;
64 for (tokenizer::iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
66 licenses.push_back(*iter);
72 vector<LicenseMatch> createMatches(vector<string> ninkaLicenseNames)
74 vector<LicenseMatch> matches;
75 for (vector<string>::const_iterator it = ninkaLicenseNames.begin(); it != ninkaLicenseNames.end(); ++it)
77 const string& ninkaLicenseName = *it;
78 if(isLicenseCollection(ninkaLicenseName,matches))
82 string fossologyLicenseName = mapLicenseFromNinkaToFossology(ninkaLicenseName);
83 unsigned percentage = (ninkaLicenseName.compare(
"NONE") == 0 || ninkaLicenseName.compare(
"UNKNOWN") == 0) ? 0 : 100;
85 matches.push_back(
match);
90 string mapLicenseFromNinkaToFossology(
string name)
92 if (name.compare(
"NONE") == 0)
return string(
"No_license_found");
93 if (name.compare(
"UNKNOWN") == 0)
return string(
"UnclassifiedLicense");
94 if (name.compare(
"spdxMIT") ==0 )
return string(
"MIT");
95 if (name.compare(
"Apachev1.0") == 0)
return string(
"Apache-1.0");
96 if (name.compare(
"Apachev2") == 0
97 || name.compare(
"Apache-2") == 0)
return string(
"Apache-2.0");
98 if (name.compare(
"GPLv1+") == 0)
return string(
"GPL-1.0+");
99 if (name.compare(
"GPLv2") == 0)
return string(
"GPL-2.0");
100 if (name.compare(
"GPLv2+") == 0)
return string(
"GPL-2.0+");
101 if (name.compare(
"GPLv3") == 0)
return string(
"GPL-3.0");
102 if (name.compare(
"GPLv3+") == 0)
return string(
"GPL-3.0+");
103 if (name.compare(
"LGPLv2") == 0)
return string(
"LGPL-2.0");
104 if (name.compare(
"LGPLv2+") == 0)
return string(
"LGPL-2.0+");
105 if (name.compare(
"LGPLv2_1") == 0
106 || name.compare(
"LGPLv2.1") == 0)
return string(
"LGPL-2.1");
107 if (name.compare(
"LGPLv2_1+") == 0)
return string(
"LGPL-2.1+");
108 if (name.compare(
"LGPLv3") == 0)
return string(
"LGPL-3.0");
109 if (name.compare(
"LGPLv3+") == 0)
return string(
"LGPL-3.0+");
110 if (name.compare(
"GPLnoVersion") == 0)
return string(
"GPL");
111 if (name.compare(
"LesserGPLnoVersion") == 0
112 || name.compare(
"LibraryGPLnoVersion")==0)
return string(
"LGPL");
113 if (name.compare(
"intelBSDLicense") == 0)
return string(
"Intel-EULA");
114 if (name.compare(
"spdxSleepyCat") == 0
115 || name.compare(
"SleepyCat") == 0)
return string(
"Sleepycat");
116 if (name.compare(
"spdxBSD2") == 0
117 || name.compare(
"BSD2") == 0)
return string(
"BSD-2-Clause");
118 if (name.compare(
"spdxBSD3") == 0
119 || name.compare(
"BSD3") == 0)
return string(
"BSD-3-Clause");
120 if (name.compare(
"BSD3") == 0)
return string(
"BSD-4-Clause");
121 if (name.compare(
"spdxMIT") == 0)
return string(
"MIT");
122 if (name.compare(
"ZLIB") == 0)
return string(
"Zlib");
123 if (name.compare(
"openSSL") == 0
124 || name.compare(
"openSSLvar1") ==0
125 || name.compare(
"openSSLvar3") ==0)
return string(
"OpenSSL");
126 if (name.compare(
"QPLt") == 0)
return string(
"QT(Commercial)");
127 if (name.compare(
"Cecill") == 0)
return string(
"CECILL");
128 if (name.compare(
"QPLv1") == 0)
return string(
"QPL-1.0");
129 if (name.compare(
"MPLv1_1") == 0)
return string(
"MPL-1.1");
130 if (name.compare(
"NPLv1_1") == 0)
return string(
"NPL-1.1");
131 if (name.compare(
"MPLv1_0") == 0)
return string(
"MPL-1.0");
132 if (name.compare(
"NPLv1_0") == 0)
return string(
"NPL-1.0");
133 if (name.compare(
"MPLv2") == 0)
return string(
"MPL-2.0");
134 if (name.compare(
"MITVariant") == 0)
return string(
"MIT-style");
135 if (name.compare(
"EPLv1") == 0)
return string(
"EPL-1.0");
136 if (name.compare(
"CDDLic") == 0)
return string(
"CDDL");
137 if (name.compare(
"CDDLicV1") == 0)
return string(
"CDDL-1.0");
138 if (name.compare(
"publicDomain") == 0)
return string(
"Public-domain");
139 if (name.compare(
"ClassPathExceptionGPLv2") == 0)
return string(
"GPL-2.0-with-classpath-exception");
140 if (name.compare(
"CPLv1") == 0)
return string(
"CPL-1.0");
141 if (name.compare(
"CPLv0.5") == 0)
return string(
"CPL-0.5");
142 if (name.compare(
"SeeFile") == 0)
return string(
"See-file");
143 if (name.compare(
"LibGCJLic") == 0)
return string(
"LIBGCJ");
144 if (name.compare(
"W3CLic") == 0)
return string(
"W3C");
145 if (name.compare(
"IBMv1") == 0)
return string(
"IPL-1.0");
146 if (name.compare(
"ArtisticLicensev1") == 0)
return string(
"Artistic-1.0");
147 if (name.compare(
"MX4JLicensev1") == 0)
return string(
"MX4J-1.0");
148 if (name.compare(
"phpLicV3.01") == 0)
return string(
"PHP-3.01");
149 if (name.compare(
"postgresql") == 0
150 || name.compare(
"postgresqlRef") == 0)
return string(
"PostgreSQL");
151 if (name.compare(
"FSFUnlimited") == 0)
return string(
"FSF");
156 bool isLicenseCollection(
string name, vector<LicenseMatch>& matches)
158 if (name.compare(
"spdxBSD4") == 0)
160 matches.push_back(
LicenseMatch(
string(
"BSD-4-Clause"), 50));
161 matches.push_back(
LicenseMatch(
string(
"BSD-4-Clause-UC"), 50));
164 if (name.compare(
"GPL2orBSD3") == 0)
166 matches.push_back(
LicenseMatch(
string(
"BSD-3-Clause"), 50));
170 if (name.compare(
"LGPLv2orv3") == 0)
172 matches.push_back(
LicenseMatch(
string(
"LGPL-2.0"), 50));
173 matches.push_back(
LicenseMatch(
string(
"LGPL-3.0"), 50));
176 if (name.compare(
"LGPLv2_1orv3") == 0)
178 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 50));
179 matches.push_back(
LicenseMatch(
string(
"LGPL-3.0"), 50));
182 if (name.compare(
"LGPLv2+MISTAKE") == 0)
184 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1+"), 50));
185 matches.push_back(
LicenseMatch(
string(
"LGPL-2.0+"), 50));
188 if (name.compare(
"LGPLv2MISTAKE") == 0)
190 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 50));
191 matches.push_back(
LicenseMatch(
string(
"LGPL-2.0"), 50));
194 if (name.compare(
"GPLv1orArtistic") == 0)
197 matches.push_back(
LicenseMatch(
string(
"Artistic-1.0"), 25));
198 matches.push_back(
LicenseMatch(
string(
"Artistic-2.0"), 25));
201 if (name.compare(
"GPL2orOpenIB") == 0)
204 matches.push_back(
LicenseMatch(
string(
"BSD-2-Clause"), 50));
207 if (name.compare(
"CDDLv1orGPLv2") == 0)
209 matches.push_back(
LicenseMatch(
string(
"CDDL-1.0"), 50));
213 if (name.compare(
"Apache-2orLGPLgeneric") == 0)
215 matches.push_back(
LicenseMatch(
string(
"Apache-2.0"), 50));
219 if (name.compare(
"orLGPLVer2.1") == 0)
221 matches.push_back(
LicenseMatch(
string(
"QT(Commercial)"), 50));
222 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 50));
225 if (name.compare(
"orLGPLVer2") == 0)
227 matches.push_back(
LicenseMatch(
string(
"QT(Commercial)"), 50));
228 matches.push_back(
LicenseMatch(
string(
"LGPL-2.0"), 50));
231 if (name.compare(
"orGPLv3") == 0)
233 matches.push_back(
LicenseMatch(
string(
"QT(Commercial)"), 50));
237 if (name.compare(
"CDDLv1orGPLv2") == 0)
239 matches.push_back(
LicenseMatch(
string(
"CDDL-1.0"), 50));
243 if (name.compare(
"CDDLorGPLv2") == 0)
249 if (name.compare(
"MPLGPL2orLGPLv2_1") == 0)
253 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 34));
256 if (name.compare(
"MPL1_1andLGPLv2_1") == 0)
262 if (name.compare(
"MPL_LGPLsee") == 0)
268 if (name.compare(
"MITX11BSDvar") == 0)
272 matches.push_back(
LicenseMatch(
string(
"BSD-style"), 34));
275 if (name.compare(
"MITCMU") == 0 || name.compare(
"MITCMUvar2") == 0 || name.compare(
"MITCMUvar3") == 0)
281 if (name.compare(
"MITX11") == 0 || name.compare(
"MITX11noNotice") == 0 || name.compare(
"MITX11simple") == 0)
287 if (name.compare(
"MITandGPL") == 0)
293 if (name.compare(
"BisonException") == 0)
295 matches.push_back(
LicenseMatch(
string(
"GPL-2.0-with-bison-exception"), 50));
296 matches.push_back(
LicenseMatch(
string(
"GPL-3.0-with-bison-exception"), 50));
299 if (name.compare(
"ClassPathException") == 0)
301 matches.push_back(
LicenseMatch(
string(
"GPL-2.0-with-classpath-exception"), 30));
302 matches.push_back(
LicenseMatch(
string(
"GPL-2.0+-with-classpath-exception"), 30));
303 matches.push_back(
LicenseMatch(
string(
"GPL-3.0-with-classpath-exception"), 40));
306 if (name.compare(
"autoConfException") == 0)
308 matches.push_back(
LicenseMatch(
string(
"GPL-2.0-with-autoconf-exception"), 50));
309 matches.push_back(
LicenseMatch(
string(
"GPL-3.0-with-autoconf-exception"), 50));
312 if (name.compare(
"CPLv1orGPLv2+orLGPLv2+") == 0)
315 matches.push_back(
LicenseMatch(
string(
"GPL-2.0+"), 33));
316 matches.push_back(
LicenseMatch(
string(
"LGPL-2.0+"), 33));
319 if (name.compare(
"GPLVer2or3KDE+") == 0)
321 matches.push_back(
LicenseMatch(
string(
"GPL-2.0+-KDE-exception"), 50));
322 matches.push_back(
LicenseMatch(
string(
"GPL-3.0+-KDE-exception"), 50));
325 if (name.compare(
"LGPLVer2.1or3KDE+") == 0)
327 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1+-KDE-exception"), 50));
328 matches.push_back(
LicenseMatch(
string(
"LGPL-3.0+-KDE-exception"), 50));
331 if (name.compare(
"GPLv2orLGPLv2.1") == 0)
334 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 50));
337 if (name.compare(
"GPLv2+orLGPLv2.1") == 0)
339 matches.push_back(
LicenseMatch(
string(
"GPL-2.0+"), 50));
340 matches.push_back(
LicenseMatch(
string(
"LGPL-2.1"), 50));
Class to handle file related operations.
const std::string & getFileName() const
char buffer[2048]
The last thing received from the scheduler.
Store the results of a regex match.