FOSSology  4.4.0
Open Source License Compliance by Open Source Software
OjoAgent.cc
1 /*
2  SPDX-FileCopyrightText: © 2019 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "OjoAgent.hpp"
8 
9 using namespace std;
10 
17  regLicenseList(
18  boost::regex(SPDX_LICENSE_LIST, boost::regex_constants::icase)),
19  regLicenseName(
20  boost::regex(SPDX_LICENSE_NAMES, boost::regex_constants::icase)),
21  regDualLicense(
22  boost::regex(SPDX_DUAL_LICENSE, boost::regex_constants::icase))
23 {
24 }
25 
39 vector<ojomatch> OjoAgent::processFile(const string &filePath,
40  OjosDatabaseHandler &databaseHandler, const int groupId, const int userId)
41 {
42  ifstream stream(filePath);
43  std::stringstream sstr;
44  sstr << stream.rdbuf();
45  if (stream.fail())
46  {
47  throw std::runtime_error(filePath);
48  }
49  stream.close();
50  const string fileContent = sstr.str();
51  vector<ojomatch> licenseList;
52  vector<ojomatch> licenseNames;
53 
54  scanString(fileContent, regLicenseList, licenseList, 0, false);
55  for (auto m : licenseList)
56  {
57  scanString(m.content, regLicenseName, licenseNames, m.start, false);
58  scanString(m.content, regDualLicense, licenseNames, m.start, true);
59  }
60 
61  findLicenseId(licenseNames, databaseHandler, groupId, userId);
62  filterMatches(licenseNames);
63 
64  return licenseNames;
65 }
66 
74 vector<ojomatch> OjoAgent::processFile(const string &filePath)
75 {
76  ifstream stream(filePath);
77  std::stringstream sstr;
78  sstr << stream.rdbuf();
79  if (stream.fail())
80  {
81  throw std::runtime_error(filePath);
82  }
83  stream.close();
84  const string fileContent = sstr.str();
85  vector<ojomatch> licenseList;
86  vector<ojomatch> licenseNames;
87 
88  scanString(fileContent, regLicenseList, licenseList, 0, false);
89  for (auto m : licenseList)
90  {
91  scanString(m.content, regLicenseName, licenseNames, m.start, false);
92  scanString(m.content, regDualLicense, licenseNames, m.start, true);
93  }
94 
95  // Remove duplicate matches for CLI run
96  vector<ojomatch>::iterator uniqueListIt = std::unique(licenseNames.begin(),
97  licenseNames.end());
98  licenseNames.resize(std::distance(licenseNames.begin(), uniqueListIt));
99 
100  return licenseNames;
101 }
102 
111 void OjoAgent::scanString(const string &text, boost::regex reg,
112  vector<ojomatch> &result, unsigned int offset, bool isDualTest)
113 {
114  string::const_iterator end = text.end();
115  string::const_iterator pos = text.begin();
116 
117  while (pos != end)
118  {
119  // Find next match
120  boost::smatch res;
121  if (boost::regex_search(pos, end, res, reg))
122  {
123  string content = "Dual-license";
124  if (! isDualTest)
125  {
126  content = res[1].str();
127  }
128  // Found match
129  result.push_back(
130  ojomatch(offset + res.position(1),
131  offset + res.position(1) + res.length(1),
132  res.length(1),
133  content));
134  pos = res[0].second;
135  offset += res.position() + res.length();
136  }
137  else
138  {
139  // No match found
140  break;
141  }
142  }
143 }
144 
149 void OjoAgent::filterMatches(vector<ojomatch> &matches)
150 {
151  // Remvoe entries with license_fk < 1
152  matches.erase(
153  std::remove_if(matches.begin(), matches.end(), [](ojomatch match)
154  { return match.license_fk <= 0;}), matches.end());
155 }
156 
164 void OjoAgent::findLicenseId(vector<ojomatch> &matches,
165  OjosDatabaseHandler &databaseHandler, const int groupId, const int userId)
166 {
167  // Update license_fk
168  for (size_t i = 0; i < matches.size(); ++i)
169  {
170  matches[i].license_fk = databaseHandler.getLicenseIdForName(
171  matches[i].content, groupId, userId);
172  }
173 }
const boost::regex regLicenseList
Definition: OjoAgent.hpp:42
OjoAgent()
Definition: OjoAgent.cc:16
void scanString(const std::string &text, boost::regex reg, std::vector< ojomatch > &result, unsigned int offset, bool isDualTest)
Definition: OjoAgent.cc:111
const boost::regex regDualLicense
Definition: OjoAgent.hpp:42
void findLicenseId(std::vector< ojomatch > &matches, OjosDatabaseHandler &databaseHandler, const int groupId, const int userId)
Definition: OjoAgent.cc:164
void filterMatches(std::vector< ojomatch > &matches)
Definition: OjoAgent.cc:149
const boost::regex regLicenseName
Definition: OjoAgent.hpp:42
unsigned long getLicenseIdForName(std::string const &rfShortName, const int groupId, const int userId)
Get the license id for a given short name.
#define SPDX_DUAL_LICENSE
Regex to check if Dual-license.
Definition: ojoregex.hpp:41
#define SPDX_LICENSE_NAMES
Regex to filter license names from list of license list.
Definition: ojoregex.hpp:34
#define SPDX_LICENSE_LIST
Regex to filter the list of licenses.
Definition: ojoregex.hpp:24
Store the results of a regex match.
Definition: scanners.hpp:28
Store the results of a regex match.
Definition: ojomatch.hpp:17