FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
readmeoss.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Author: Daniele Fognini, Shaheem Azmal M MD
4  * Copyright (C) 2016-2018, Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
54 
55 include_once(__DIR__ . "/version.php");
56 
62 class ReadmeOssAgent extends Agent
63 {
64  const UPLOAD_ADDS = "uploadsAdd";
65 
70 
75 
80 
84  private $uploadDao;
85 
89  protected $additionalUploadIds = array();
90 
91  function __construct()
92  {
93  $this->cpClearedGetter = new XpClearedGetter("copyright", "statement");
94  $this->licenseClearedGetter = new LicenseClearedGetter();
95  $this->licenseMainGetter = new LicenseMainGetter();
96 
97  parent::__construct(README_AGENT_NAME, AGENT_VERSION, AGENT_REV);
98 
99  $this->uploadDao = $this->container->get('dao.upload');
100 
101  $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.':';
102  }
103 
109  function processUploadId($uploadId)
110  {
112 
113  $args = $this->args;
114  $this->additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,$args) ? explode(',',$args[self::UPLOAD_ADDS]) : array();
115  $uploadIds = $this->additionalUploadIds;
116  array_unshift($uploadIds, $uploadId);
117 
118  $this->heartbeat(0);
119  $licenseStmts = array();
120  $copyrightStmts = array();
121  $licenseStmtsMain = array();
122  $licenseAcknowledgements = array();
123 
124  foreach ($uploadIds as $addUploadId) {
125  if (!$this->uploadDao->isAccessible($addUploadId, $groupId)) {
126  continue;
127  }
128  $moreLicenses = $this->licenseClearedGetter->getCleared($addUploadId, $groupId, true, null, false, $this);
129  $licenseStmts = array_merge($licenseStmts, $moreLicenses['statements']);
130  $this->heartbeat(count($moreLicenses['statements']));
131  $this->licenseClearedGetter->setOnlyAcknowledgements(true);
132  $moreAcknowledgements = $this->licenseClearedGetter->getCleared($addUploadId, $groupId, true, null, false, $this);
133  $licenseAcknowledgements = array_merge($licenseAcknowledgements, $moreAcknowledgements['statements']);
134  $this->heartbeat(count($moreAcknowledgements['statements']));
135  $moreCopyrights = $this->cpClearedGetter->getCleared($addUploadId, $groupId, true, "copyright", false, $this);
136  $copyrightStmts = array_merge($copyrightStmts, $moreCopyrights['statements']);
137  $this->heartbeat(count($moreCopyrights['statements']));
138  $moreMainLicenses = $this->licenseMainGetter->getCleared($addUploadId, $groupId, true, null, false, $this);
139  $licenseStmtsMain = array_merge($licenseStmtsMain, $moreMainLicenses['statements']);
140  $this->heartbeat(count($moreMainLicenses['statements']));
141  }
142  list($licenseStmtsMain, $licenseStmts) = $this->licenseClearedGetter->updateIdentifiedGlobalLicenses($licenseStmtsMain, $licenseStmts);
143  $contents = array('licensesMain'=>$licenseStmtsMain, 'licenses'=>$licenseStmts, 'copyrights'=>$copyrightStmts, 'licenseAcknowledgements' => $licenseAcknowledgements);
144  $this->writeReport($contents, $uploadId);
145 
146  return true;
147  }
148 
154  private function writeReport($contents, $uploadId)
155  {
156  global $SysConf;
157 
158  $packageName = $this->uploadDao->getUpload($uploadId)->getFilename();
159 
160  $fileBase = $SysConf['FOSSOLOGY']['path']."/report/";
161  $fileName = $fileBase. "ReadMe_OSS_".$packageName.'_'.time().".txt" ;
162 
163  foreach ($this->additionalUploadIds as $addUploadId) {
164  $packageName .= ', ' . $this->uploadDao->getUpload($addUploadId)->getFilename();
165  }
166 
167  if (!is_dir($fileBase)) {
168  mkdir($fileBase, 0777, true);
169  }
170  umask(0133);
171  $message = $this->generateReport($contents, $packageName);
172 
173  file_put_contents($fileName, $message);
174 
175  $this->updateReportTable($uploadId, $this->jobId, $fileName);
176  }
177 
184  private function updateReportTable($uploadId, $jobId, $filename)
185  {
186  $this->dbManager->insertTableRow('reportgen', array('upload_fk'=>$uploadId, 'job_fk'=>$jobId, 'filepath'=>$filename), __METHOD__);
187  }
188 
197  private function createReadMeOSSFormat($addSeparator, $dataForReadME, $extract='text', $break)
198  {
199  $outData = "";
200  foreach ($dataForReadME as $statements) {
201  if ($extract == 'text') {
202  $outData .= $statements["content"] . $break;
203  }
204  $outData .= str_replace("\n", "\r\n", $statements[$extract]) . $break;
205  if (!empty($addSeparator)) {
206  $outData .= $addSeparator . $break;
207  }
208  }
209  return $outData;
210  }
211 
219  private function generateReport($contents, $packageName)
220  {
221  $separator1 = str_repeat("=", 120);
222  $separator2 = str_repeat("-", 120);
223  $break = str_repeat("\r\n", 2);
224  $output = $separator1 . $break . $packageName . $break . $separator2 . $break;
225  if (!empty($contents['licensesMain'])) {
226  $output .= $separator1 . $break . " MAIN LICENSES " . $break . $separator2 . $break;
227  $output .= $this->createReadMeOSSFormat($separator2, $contents['licensesMain'], 'text', $break);
228  }
229  if (!empty($contents['licenses'])) {
230  $output .= $separator1 . $break . " OTHER LICENSES " . $break . $separator2 . $break;
231  $output .= $this->createReadMeOSSFormat($separator2, $contents['licenses'], 'text', $break);
232  }
233  if (!empty($contents['licenseAcknowledgements'])) {
234  $output .= $separator1 . $break . " ACKNOWLEDGEMENTS " . $break . $separator2 . $break;
235  $output .= $this->createReadMeOSSFormat($separator2, $contents['licenseAcknowledgements'], 'text', $break);
236  }
237  $copyrights = $this->createReadMeOSSFormat("", $contents['copyrights'], 'content', "\r\n");
238  if (empty($copyrights)) {
239  $output .= "<Copyright notices>";
240  $output .= $break;
241  $output .= "<notices>";
242  } else {
243  $output .= "Copyright notices";
244  $output .= $break;
245  $output .= $copyrights;
246  }
247  return $output;
248  }
249 }
250 
251 $agent = new ReadmeOssAgent();
252 $agent->scheduler_connect();
253 $agent->run_scheduler_event_loop();
254 $agent->scheduler_disconnect(0);
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:214
writeReport($contents, $uploadId)
Write data to text file.
Definition: readmeoss.php:154
createReadMeOSSFormat($addSeparator, $dataForReadME, $extract='text', $break)
This function lists elements of array.
Definition: readmeoss.php:197
Structure of an Agent with all required parameters.
Definition: Agent.php:51
Readme_OSS agent generates list of licenses and copyrights found in an upload.
Definition: readmeoss.php:62
int jobId
The id of the job.
processUploadId($uploadId)
Given an upload ID, process the items in it.
Definition: readmeoss.php:109
generateReport($contents, $packageName)
Gather all the data.
Definition: readmeoss.php:219
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
const UPLOAD_ADDS
The HTTP GET parameter name.
Definition: readmeoss.php:64
updateReportTable($uploadId, $jobId, $filename)
Update the report path.
Definition: readmeoss.php:184