Added Python3Runner

https://git.n64.cc/nosial/ncc/-/issues/32
This commit is contained in:
Netkas 2022-12-07 22:59:15 -05:00
parent d2acf224f3
commit e7e2cca62a

View file

@ -0,0 +1,53 @@
<?php
namespace ncc\Classes\PythonExtension;
use ncc\Abstracts\Runners;
use ncc\Exceptions\FileNotFoundException;
use ncc\Interfaces\RunnerInterface;
use ncc\Objects\ExecutionPointers\ExecutionPointer;
use ncc\Objects\Package\ExecutionUnit;
use ncc\Objects\ProjectConfiguration\ExecutionPolicy;
use ncc\ThirdParty\Symfony\Process\Process;
use ncc\Utilities\Base64;
use ncc\Utilities\IO;
use ncc\Utilities\PathFinder;
class Python3Runner implements RunnerInterface
{
/**
* @inheritDoc
*/
public static function processUnit(string $path, ExecutionPolicy $policy): ExecutionUnit
{
$execution_unit = new ExecutionUnit();
if(!file_exists($path) && !is_file($path))
throw new FileNotFoundException($path);
$policy->Execute->Target = null;
$execution_unit->ExecutionPolicy = $policy;
$execution_unit->Data = Base64::encode(IO::fread($path));
return $execution_unit;
}
/**
* @inheritDoc
*/
public static function getFileExtension(): string
{
return '.py';
}
/**
* @inheritDoc
*/
public static function prepareProcess(ExecutionPointer $pointer): Process
{
$python_bin = PathFinder::findRunner(Runners::python3);
if($pointer->ExecutionPolicy->Execute->Options !== null && count($pointer->ExecutionPolicy->Execute->Options) > 0)
return new Process(array_merge([$python_bin, $pointer->FilePointer], $pointer->ExecutionPolicy->Execute->Options));
return new Process([$python_bin, $pointer->FilePointer]);
}
}