30 use \GuzzleHttp\Client;
34 include_once(__DIR__ .
"/version.php");
76 private $softwareHeritageDao;
84 parent::__construct(SOFTWARE_HERITAGE_AGENT_NAME, AGENT_VERSION, AGENT_REV);
85 $this->uploadDao = $this->container->get(
'dao.upload');
86 $this->licenseDao = $this->container->get(
'dao.license');
87 $this->dbManeger = $this->container->get(
'db.manager');
88 $this->agentDao = $this->container->get(
'dao.agent');
89 $this->softwareHeritageDao = $this->container->get(
'dao.softwareHeritage');
90 $this->configuration = parse_ini_file(__DIR__ .
'/softwareHeritage.conf');
102 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
103 $pfileFileDetails = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
104 $pfileFks = $this->softwareHeritageDao->getSoftwareHeritagePfileFk($uploadId);
105 $agentId = $this->agentDao->getCurrentAgentId(
"softwareHeritage");
106 $maxTime = $this->configuration[
'api'][
'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) {
153 $URIToGetLicenses = $this->configuration[
'api'][
'url'].$this->configuration[
'api'][
'uri'].$sha256.$this->configuration[
'api'][
'content'];
154 $URIToGetContent = $this->configuration[
'api'][
'url'].$this->configuration[
'api'][
'uri'].$sha256;
156 if (array_key_exists(
'http_proxy', $SysConf[
'FOSSOLOGY']) &&
157 ! empty($SysConf[
'FOSSOLOGY'][
'http_proxy'])) {
158 $proxy[
'http'] = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
160 if (array_key_exists(
'https_proxy', $SysConf[
'FOSSOLOGY']) &&
161 ! empty($SysConf[
'FOSSOLOGY'][
'https_proxy'])) {
162 $proxy[
'https'] = $SysConf[
'FOSSOLOGY'][
'https_proxy'];
164 if (array_key_exists(
'no_proxy', $SysConf[
'FOSSOLOGY']) &&
165 ! empty($SysConf[
'FOSSOLOGY'][
'no_proxy'])) {
166 $proxy[
'no'] = explode(
',', $SysConf[
'FOSSOLOGY'][
'no_proxy']);
169 $client =
new Client([
'http_errors' =>
false,
'proxy' => $proxy]);
171 $response = $client->get($URIToGetLicenses);
172 $statusCode = $response->getStatusCode();
173 $cookedResult = array();
174 if ($statusCode == SoftwareHeritageDao::SWH_STATUS_OK) {
175 $responseContent = json_decode($response->getBody()->getContents(),
true);
176 $cookedResult = $responseContent[
"facts"][0][
"licenses"];
177 }
else if ($statusCode == SoftwareHeritageDao::SWH_RATELIMIT_EXCEED) {
178 $responseContent = $response->getHeaders();
179 $cookedResult = $responseContent[
"X-RateLimit-Reset"][0];
180 }
else if ($statusCode == SoftwareHeritageDao::SWH_NOT_FOUND) {
181 $response = $client->get($URIToGetContent);
182 $responseContent = json_decode($response->getBody(),
true);
183 if (isset($responseContent[
"status"])) {
184 $statusCode = SoftwareHeritageDao::SWH_STATUS_OK;
187 return array($statusCode, $cookedResult);
188 }
catch (RequestException $e) {
189 echo
"Sorry, something went wrong. check if the host is accessible!\n";
190 echo Psr7\str($e->getRequest());
191 if ($e->hasResponse()) {
192 echo Psr7\str($e->getResponse());
208 $licenseString = !empty($licenses) ? implode(
", ", $licenses) :
'';
209 $this->softwareHeritageDao->setSoftwareHeritageDetails($pfileId,
210 $licenseString, $status);
211 if (!empty($licenses)) {
212 foreach ($licenses as $license) {
213 $l = $this->licenseDao->getLicenseByShortName($license);
215 $this->dbManeger->insertTableRow(
'license_file',[
'agent_fk' =>
$agentId,
216 'pfile_fk' => $pfileId,
'rf_fk'=> $l->getId()]);
scheduler_disconnect($exitvalue)
Closes connection from scheduler.
heartbeat($newProcessed)
Send hear beat to the scheduler.
Structure of an Agent with all required parameters.
insertSoftwareHeritageRecord($pfileId, $licenses, $agentId, $status)
Insert the License Details in softwareHeritage table.
processUploadId($uploadId)
Run software heritage for a package.
processEachPfileForSWH($pfileDetail, $agentId, $maxTime)
process each pfile for software heritage and wait till the reset time
getSoftwareHeritageLicense($sha256)
Get the license details from software heritage.