11 namespace Fossology\SoftwareHeritage;
21 use GuzzleHttp\Exception\RequestException;
23 include_once(__DIR__ .
"/version.php");
65 private $softwareHeritageDao;
74 parent::__construct(SOFTWARE_HERITAGE_AGENT_NAME, AGENT_VERSION, AGENT_REV);
75 $this->uploadDao = $this->container->get(
'dao.upload');
76 $this->licenseDao = $this->container->get(
'dao.license');
77 $this->dbManeger = $this->container->get(
'db.manager');
78 $this->agentDao = $this->container->get(
'dao.agent');
79 $this->softwareHeritageDao = $this->container->get(
'dao.softwareHeritage');
80 $sysconfig = $SysConf[
'SYSCONFIG'];
81 $this->configuration = [
82 'url' =>
trim($sysconfig[
'SwhURL']),
83 'uri' =>
trim($sysconfig[
'SwhBaseURL']),
84 'content' =>
trim($sysconfig[
'SwhContent']),
85 'maxtime' => intval($sysconfig[
'SwhSleep']),
86 'token' =>
trim($sysconfig[
'SwhToken'])
101 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
102 $pfileFileDetails = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
103 $pfileFks = $this->softwareHeritageDao->getSoftwareHeritagePfileFk($uploadId);
104 $agentId = $this->agentDao->getCurrentAgentId(
"softwareHeritage");
105 $maxTime = $this->configuration[
'maxtime'];
106 $maxTime = ($maxTime < 2) ? 2 : $maxTime;
107 foreach ($pfileFileDetails as $pfileDetail) {
108 if (!in_array($pfileDetail[
'pfile_pk'], array_column($pfileFks,
'pfile_fk'))) {
126 if (SoftwareHeritageDao::SWH_RATELIMIT_EXCEED == $currentStatus) {
128 $timeToReset = $currentResult - time();
129 print
"INFO :Software Heritage X-RateLimit-Limit reached. Next slot unlocks in ".gmdate(
"H:i:s", $timeToReset).
"\n";
130 if ($timeToReset > $maxTime) {
151 $sha256 = strtolower($sha256);
152 $URIToGetContent = $this->configuration[
'uri'] . $sha256;
153 $URIToGetLicenses = $URIToGetContent . $this->configuration[
'content'];
156 $response = $this->guzzleClient->get($URIToGetLicenses);
157 $statusCode = $response->getStatusCode();
158 $cookedResult = array();
159 if ($statusCode == SoftwareHeritageDao::SWH_STATUS_OK) {
160 $responseContent = json_decode($response->getBody()->getContents(),
true);
161 $cookedResult = $responseContent[
"facts"][0][
"licenses"];
162 }
else if ($statusCode == SoftwareHeritageDao::SWH_RATELIMIT_EXCEED) {
163 $responseContent = $response->getHeaders();
164 $cookedResult = $responseContent[
"X-RateLimit-Reset"][0];
165 }
else if ($statusCode == SoftwareHeritageDao::SWH_NOT_FOUND) {
166 $response = $this->guzzleClient->get($URIToGetContent);
167 $responseContent = json_decode($response->getBody(),
true);
168 if (isset($responseContent[
"status"])) {
169 $statusCode = SoftwareHeritageDao::SWH_STATUS_OK;
172 return array($statusCode, $cookedResult);
173 }
catch (RequestException $e) {
174 echo
"Sorry, something went wrong. check if the host is accessible!\n";
175 echo Psr7\str($e->getRequest());
176 if ($e->hasResponse()) {
177 echo Psr7\str($e->getResponse());
193 $licenseString = !empty($licenses) ? implode(
", ", $licenses) :
'';
194 $this->softwareHeritageDao->setSoftwareHeritageDetails($pfileId,
195 $licenseString, $status);
196 if (!empty($licenses)) {
197 foreach ($licenses as $license) {
198 $l = $this->licenseDao->getLicenseByShortName($license);
200 $this->dbManeger->insertTableRow(
'license_file',[
'agent_fk' =>
$agentId,
201 'pfile_fk' => $pfileId,
'rf_fk'=> $l->getId()]);
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
scheduler_disconnect($exitvalue)
Closes connection from scheduler.
static getGuzzleClient(array $SysConf, string $baseUri, string $token="")
processUploadId($uploadId)
Run software heritage for a package.
processEachPfileForSWH($pfileDetail, $agentId, $maxTime)
process each pfile for software heritage and wait till the reset time
insertSoftwareHeritageRecord($pfileId, $licenses, $agentId, $status)
Insert the License Details in softwareHeritage table.
getSoftwareHeritageLicense($sha256)
Get the license details from software heritage.
char * trim(char *ptext)
Trimming whitespace.