7 #include "scancode_wrapper.hpp"
22 unsigned getFilePointer(
const string &filename,
size_t start_line,
23 const string &match_text) {
24 ifstream checkfile(filename);
26 if (checkfile.is_open()) {
27 for (
size_t i = 0; i < start_line - 1; i++) {
28 getline(checkfile, str);
30 unsigned int file_p = checkfile.tellg();
31 getline(checkfile, str);
32 unsigned int pos = str.find(match_text);
33 if (pos != string::npos) {
36 LOG_NOTICE(
"Failed to find startbyte for %s\n", filename.c_str());
65 void scanFileWithScancode(
const State &state,
string fileLocation,
string outputFile) {
72 "PYTHONPATH='/home/" + projectUser +
"/pythondeps/' " +
75 to_string(MINSCORE):
"") +
" " + fileLocation +
" " + outputFile;
77 int returnvalue = system(command.c_str());
79 if (returnvalue != 0) {
80 LOG_FATAL(
"could not execute scancode command: %s \n", command.c_str());
84 if (unlink(fileLocation.c_str()) != 0)
86 LOG_FATAL(
"Unable to delete file %s \n", fileLocation.c_str());
117 map<string, vector<Match>> extractDataFromScancodeResult(
const string& scancodeResult,
const string& filename) {
118 Json::CharReaderBuilder json_reader_builder;
119 auto scanner = unique_ptr<Json::CharReader>(json_reader_builder.newCharReader());
120 Json::Value scancodevalue;
122 const bool isSuccessful =
scanner->parse(scancodeResult.c_str(),
123 scancodeResult.c_str() + scancodeResult.length(), &scancodevalue, &errors);
124 map<string, vector<Match>> result;
125 vector<Match> licenses;
127 Json::Value licensearrays = scancodevalue[
"licenses"];
128 if(licensearrays.empty())
130 result[
"scancode_license"].push_back(
Match(
"No_license_found"));
134 for (
auto oneresult : licensearrays)
136 string licensename = oneresult[
"spdx_license_key"].asString();
137 int percentage = (int)oneresult[
"score"].asFloat();
138 string full_name=oneresult[
"name"].asString();
139 string text_url=oneresult[
"text_url"].asString();
140 string match_text = oneresult[
"matched_text"].asString();
141 unsigned long start_line=oneresult[
"start_line"].asUInt();
142 string temp_text= match_text.substr(0,match_text.find(
"\n"));
143 unsigned start_pointer = getFilePointer(filename, start_line, temp_text);
144 unsigned length = match_text.length();
145 result[
"scancode_license"].push_back(
Match(licensename,percentage,full_name,text_url,start_pointer,length));
149 Json::Value copyarrays = scancodevalue[
"copyrights"];
150 for (
auto oneresult : copyarrays) {
151 string copyrightname = oneresult[
"value"].asString();
152 unsigned long start_line=oneresult[
"start"].asUInt();
153 string temp_text= copyrightname.substr(0,copyrightname.find(
"[\n\t]"));
154 unsigned start_pointer = getFilePointer(filename, start_line, temp_text);
155 unsigned length = copyrightname.length();
156 string type=
"scancode_statement";
157 result[
"scancode_statement"].push_back(
Match(copyrightname,type,start_pointer,length));
160 Json::Value holderarrays = scancodevalue[
"holders"];
161 for (
auto oneresult : holderarrays) {
162 string holdername = oneresult[
"value"].asString();
163 unsigned long start_line=oneresult[
"start"].asUInt();
164 string temp_text= holdername.substr(0,holdername.find(
"\n"));
165 unsigned start_pointer = getFilePointer(filename, start_line, temp_text);
166 unsigned length = holdername.length();
167 string type=
"scancode_author";
168 result[
"scancode_author"].push_back(
Match(holdername,type,start_pointer,length));
171 Json::Value emailarrays = scancodevalue[
"emails"];
172 for (
auto oneresult : emailarrays) {
173 string emailname = oneresult[
"value"].asString();
174 unsigned long start_line=oneresult[
"start"].asUInt();
175 string temp_text= emailname.substr(0,emailname.find(
"\n"));
176 unsigned start_pointer = getFilePointer(filename, start_line, temp_text);
177 unsigned length = emailname.length();
178 string type=
"scancode_email";
179 result[
"scancode_email"].push_back(
Match(emailname,type,start_pointer,length));
182 Json::Value urlarrays = scancodevalue[
"urls"];
183 for (
auto oneresult : urlarrays) {
184 string urlname = oneresult[
"value"].asString();
185 unsigned long start_line=oneresult[
"start"].asUInt();
186 string temp_text= urlname.substr(0,urlname.find(
"\n"));
187 unsigned start_pointer = getFilePointer(filename, start_line, temp_text);
188 unsigned length = urlname.length();
189 string type=
"scancode_url";
190 result[
"scancode_url"].push_back(
Match(urlname,type,start_pointer,length));
193 LOG_FATAL(
"JSON parsing failed %s \n", errors.c_str());
string getCliOptions() const
getter function for cliOptions
Abstract class to provide interface to scanners.
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found,...