FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CompatibilityUtils.cc File Reference
#include "CompatibilityUtils.hpp"
#include "CompatibilityAgent.hpp"
#include <iostream>
Include dependency graph for CompatibilityUtils.cc:

Go to the source code of this file.

Functions

CompatibilityState getState (DbManager &dbManager, CompatibilityCliOptions &&cliOptions)
 Create a new state for the current agent based on CliOptions. More...
 
CompatibilityState getState (CompatibilityCliOptions &&cliOptions)
 Create a new state for the agent without DB manager. More...
 
int queryAgentId (DbManager &dbManager)
 
int writeARS (const CompatibilityState &state, int arsId, int uploadId, int success, DbManager &dbManager)
 
void bail (int exitval)
 
bool processUploadId (const CompatibilityState &state, int uploadId, CompatibilityDatabaseHandler &databaseHandler, int groupId)
 
bool parseCliOptions (int argc, char **argv, CompatibilityCliOptions &dest, std::string &types, std::string &rules, string &jFile, string &mainLicense)
 Parse the options sent by CLI to CliOptions object. More...
 
void appendToJson (const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName, bool &printComma)
 
void printResultToStdout (const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName)
 
std::set< std::string > mainLicenseToSet (const string &mainLicense)
 
bool are_licenses_compatible (const string &first_name, const string &first_type, const string &second_name, const string &second_type, const map< tuple< string, string, string, string >, bool > &rule_list)
 Check the licenses against rules and get the compatibility result. More...
 
int get_column_ids (const string &header, int &name_col, int &type_col)
 Get the column index for shortname and licensetype columns from CSV header. More...
 
unordered_map< string, string > initialize_license_map (const string &file_location)
 Parse license type CSV and create a map. More...
 
map< tuple< string, string, string, string >, bool > initialize_rule_list (const string &file_location)
 Read YAML file of rules and parse it as map. More...
 

Detailed Description

The utility functions for OJO agent

Definition in file CompatibilityUtils.cc.

Function Documentation

◆ appendToJson()

void appendToJson ( const std::vector< tuple< string, string, bool >> &  resultPair,
const std::string &  fileName,
bool &  printComma 
)

Append a new result from scanner to STDOUT

Parameters
fileNameFile which was scanned
resultPairContains the first license name, second license name and their compatibility result
printCommaSet true to print comma. Will be set true after first data is printed

Definition at line 289 of file CompatibilityUtils.cc.

◆ are_licenses_compatible()

bool are_licenses_compatible ( const string &  first_name,
const string &  first_type,
const string &  second_name,
const string &  second_type,
const map< tuple< string, string, string, string >, bool > &  rule_list 
)

Check the licenses against rules and get the compatibility result.

Parameters
first_nameName of the license 1
first_typeType of the license 1
second_nameName of the license 2
second_typeType of the license 2
rule_listMap of the compatibility rules
Returns
True or False based on rule list or default value.

Definition at line 421 of file CompatibilityUtils.cc.

◆ bail()

void bail ( int  exitval)

Disconnect scheduler and exit in case of failure.

Parameters
exitvalExit code to be sent to scheduler and returned by program

Definition at line 90 of file CompatibilityUtils.cc.

◆ get_column_ids()

int get_column_ids ( const string &  header,
int &  name_col,
int &  type_col 
)

Get the column index for shortname and licensetype columns from CSV header.

Parameters
headerString containing CSV header
[out]name_colIndex of shortname column
[out]type_colIndex of licensetype column
Returns
-1 if shortname not found, -2 if licensetype not found, 0 if both found.

Definition at line 482 of file CompatibilityUtils.cc.

◆ getState() [1/2]

CompatibilityState getState ( CompatibilityCliOptions &&  cliOptions)

Create a new state for the agent without DB manager.

Parameters
cliOptionsCLI options passed
Returns
New CompatibilityState object

Definition at line 38 of file CompatibilityUtils.cc.

◆ getState() [2/2]

CompatibilityState getState ( DbManager dbManager,
CompatibilityCliOptions &&  cliOptions 
)

Create a new state for the current agent based on CliOptions.

Called during instantiation of agent.

Parameters
cliOptionsCLI options passed to the agent
Returns
New CompatibilityState object for the agent

Definition at line 26 of file CompatibilityUtils.cc.

◆ initialize_license_map()

unordered_map<string, string> initialize_license_map ( const string &  file_location)

Parse license type CSV and create a map.

Parameters
file_locationLocation of the CSV file
Returns
Map with license name as key and type as value
Exceptions
invalid_argumentIf CSV is missing required headers

Definition at line 521 of file CompatibilityUtils.cc.

◆ initialize_rule_list()

map<tuple<string, string, string, string>, bool> initialize_rule_list ( const string &  file_location)

Read YAML file of rules and parse it as map.

The map has key of tuple (first name, first type, second name, second type) and value as boolean of compatibility result.

A special key with tuple (~, ~, ~, ~) holds the default value.

Parameters
file_locationLocation to rule YAML
Returns
Map of rules

Definition at line 567 of file CompatibilityUtils.cc.

◆ mainLicenseToSet()

std::set<std::string> mainLicenseToSet ( const string &  mainLicense)

Converts a main license string (which may contain AND) to a set of licenses.

Parameters
mainLicenseMain license string from CLI
Returns
List of individual licenses

Definition at line 390 of file CompatibilityUtils.cc.

◆ parseCliOptions()

bool parseCliOptions ( int  argc,
char **  argv,
CompatibilityCliOptions dest,
std::string &  types,
std::string &  rules,
string &  jFile,
string &  mainLicense 
)

Parse the options sent by CLI to CliOptions object.

Parameters
[in]argc
[in]argv
[out]destThe parsed OjoCliOptions object
[out]typesPath of the csv file to be scanned
[out]rulesPath of the yaml file to be scanned
[out]jFilePath of the json file to be scanned
[out]mainLicenseMain license for the package
Returns
True if success, false otherwise

Definition at line 191 of file CompatibilityUtils.cc.

◆ printResultToStdout()

void printResultToStdout ( const std::vector< tuple< string, string, bool >> &  resultPair,
const std::string &  fileName 
)

Print the result of current scan to stdout

Parameters
fileNameFile which was scanned
resultPairContains the first license name, second license name and their compatibility result

Definition at line 360 of file CompatibilityUtils.cc.

◆ processUploadId()

bool processUploadId ( const CompatibilityState state,
int  uploadId,
CompatibilityDatabaseHandler databaseHandler,
int  groupId 
)

Process a given upload id

Parameters
stateState of the agent
uploadIdUpload ID to be scanned
databaseHandlerDatabase handler to be used
groupIdGroup who scheduled the agent
Returns
True in case of successful scan, false otherwise.

Definition at line 104 of file CompatibilityUtils.cc.

◆ queryAgentId()

int queryAgentId ( DbManager dbManager)

Query the agent ID from the DB.

Parameters
dbManagerDbManager to be used
Returns
The agent if found, bail otherwise.

Definition at line 48 of file CompatibilityUtils.cc.

◆ writeARS()

int writeARS ( const CompatibilityState state,
int  arsId,
int  uploadId,
int  success,
DbManager dbManager 
)

Write ARS to the agent's ars table

Parameters
stateState of the agent
arsIdARS id (0 for new entry)
uploadIdUpload ID
successSuccess status
dbManagerDbManager to use
Returns
ARS ID.

Definition at line 76 of file CompatibilityUtils.cc.