FOSSology  4.5.1
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 
47  public function getScriptIncludes(&$vars)
48  {
49  return "";
50  }
51 
67  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
68  {
69  $dependencies = array();
70  $flags = $request->get('scancodeFlags') ?: array();
71  $unpackArgs = intval($request->get('scm', 0)) == 1 ? 'I' : '';
72  $args = $this->getScanCodeArgs($flags, $unpackArgs);
73  if ($args === null) {
74  return 0;
75  }
76  if (!empty($unpackArgs)) {
77  $dependencies[] = 'agent_mimetype';
78  }
79  return parent::AgentAdd($jobId, $uploadId, $errorMsg, array_unique($dependencies) , $args);
80  }
81 
88  public function getScanCodeArgs($flags, $unpackArgs)
89  {
90  $scanMode = '';
91  foreach ($flags as $flag) {
92  switch ($flag)
93  {
94  case "license":
95  $scanMode .= 'l';
96  break;
97  case "copyright":
98  $scanMode .= 'r';
99  break;
100  case "email":
101  $scanMode .= 'e';
102  break;
103  case "url":
104  $scanMode .= 'u';
105  break;
106  }
107  }
108  if (empty($scanMode)) {
109  return null;
110  }
111  if (!empty($unpackArgs)) {
112  $scanMode .= $unpackArgs;
113  }
114  return self::SCAN_FLAG . $scanMode;
115  }
116 
121  function AgentHasResults($uploadId=0)
122  {
123  return CheckARS($uploadId, $this->AgentName, "scancode agent", "scancode_ars");
124  }
125 
132  protected function isAgentIncluded($dependencies, $agentName)
133  {
134  foreach ($dependencies as $dependency) {
135  if ($dependency == $agentName) {
136  return true;
137  }
138  if (is_array($dependency) && $agentName == $dependency['name']) {
139  return true;
140  }
141  }
142  return false;
143  }
144 
149  public function preInstall()
150  {
151  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
152  }
153 
159  public function isScanCodeInstalled()
160  {
161  global $SysConf;
162  return file_exists("/home/" .
163  $SysConf['DIRECTORIES']['PROJECTUSER'] . "/pythondeps/bin/scancode");
164  }
165 }
166 
167 $scanCode = new ScancodesAgentPlugin();
168 if ($scanCode->isScanCodeInstalled()) {
169  register_plugin($scanCode);
170 }
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.