FOSSology  4.4.0
Open Source License Compliance by Open Source Software
agent-scancode.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 Sarita Singh <saritasingh.0425@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Scancode\Ui;
9 
11 use Symfony\Component\HttpFoundation\Request;
12 
14 {
15  const SCAN_FLAG = '-';
16 
17  public function __construct()
18  {
19  $this->Name = "agent_scancode";
20  $this->Title = _("Scancode Toolkit");
21  $this->AgentName = "scancode";
22 
23  parent::__construct();
24  }
25 
31  public function renderContent(&$vars)
32  {
33  $renderer = $GLOBALS['container']->get('twig.environment');
34  return $renderer->load('scancode.html.twig')->render($vars);
35  }
36 
42  public function renderFoot(&$vars)
43  {
44  return "";
45  }
46 
62  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
63  {
64  $dependencies = array();
65  $flags = $request->get('scancodeFlags') ?: array();
66  $unpackArgs = intval(@$_POST['scm']) == 1 ? 'I' : '';
67  $args = $this->getScanCodeArgs($flags, $unpackArgs);
68  if ($args === null) {
69  return 0;
70  }
71  if (!empty($unpackArgs)) {
72  $dependencies[] = 'agent_mimetype';
73  }
74  return parent::AgentAdd($jobId, $uploadId, $errorMsg, array_unique($dependencies) , $args);
75  }
76 
83  public function getScanCodeArgs($flags, $unpackArgs)
84  {
85  $scanMode = '';
86  foreach ($flags as $flag) {
87  switch ($flag)
88  {
89  case "license":
90  $scanMode .= 'l';
91  break;
92  case "copyright":
93  $scanMode .= 'r';
94  break;
95  case "email":
96  $scanMode .= 'e';
97  break;
98  case "url":
99  $scanMode .= 'u';
100  break;
101  }
102  }
103  if (empty($scanMode)) {
104  return null;
105  }
106  if (!empty($unpackArgs)) {
107  $scanMode .= $unpackArgs;
108  }
109  return self::SCAN_FLAG . $scanMode;
110  }
111 
116  function AgentHasResults($uploadId=0)
117  {
118  return CheckARS($uploadId, $this->AgentName, "scancode agent", "scancode_ars");
119  }
120 
127  protected function isAgentIncluded($dependencies, $agentName)
128  {
129  foreach ($dependencies as $dependency) {
130  if ($dependency == $agentName) {
131  return true;
132  }
133  if (is_array($dependency) && $agentName == $dependency['name']) {
134  return true;
135  }
136  }
137  return false;
138  }
139 
144  public function preInstall()
145  {
146  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
147  }
148 
154  public function isScanCodeInstalled()
155  {
156  global $SysConf;
157  return file_exists("/home/" .
158  $SysConf['DIRECTORIES']['PROJECTUSER'] . "/pythondeps/bin/scancode");
159  }
160 }
161 
162 $scanCode = new ScancodesAgentPlugin();
163 if ($scanCode->isScanCodeInstalled()) {
164  register_plugin($scanCode);
165 }
scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
Schedule scancode agent.
renderContent(&$vars)
Render HTML from template.
isAgentIncluded($dependencies, $agentName)
CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
Check the ARS table to see if an agent has successfully scanned an upload.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.