hotfix: Added examples, initial python support

This commit is contained in:
Chris 2016-12-11 23:45:21 +01:00
parent 6c5c558087
commit 0564c41976
5 changed files with 68 additions and 1 deletions

View File

@ -0,0 +1,8 @@
hotfix: This hotfix show how to use the bash spinner
info: >
The spinner shows an, um... spinner, while the command executes.
author: Noccy <cvagnetoft@gmail.com>
lang: bash
---
find /home >/dev/null & spinner

10
examples/php-facts.fix Normal file
View File

@ -0,0 +1,10 @@
hotfix: This hotfix show how to use facts in php
info: >
This lets you retrieve some additional system information during
runtime. Use 'hotfix facts' to see all the known facts.
author: Noccy <cvagnetoft@gmail.com>
lang: php
---
echo "Architecture: ".fact('system.arch')."\n";
echo "Distribution: ".fact('lsb.id')."\n";

10
examples/python-facts.fix Normal file
View File

@ -0,0 +1,10 @@
hotfix: This hotfix show how to use facts in python
info: >
This lets you retrieve some additional system information during
runtime. Use 'hotfix facts' to see all the known facts.
author: Noccy <cvagnetoft@gmail.com>
lang: python
---
print "Architecture: " + fact("system.arch")
print "Distribution: " + fact("lsb.id")

View File

@ -4,6 +4,7 @@ namespace NoccyLabs\Hotfix\Runner;
use NoccyLabs\Hotfix\System\Facts;
use NoccyLabs\Hotfix\Hotfix\Hotfix;
use NoccyLabs\Hotfix\Exception\UnsupportedRunnerException;
/**
* Run hotfixes written in Python.
@ -13,13 +14,49 @@ use NoccyLabs\Hotfix\Hotfix\Hotfix;
class PythonRunner implements RunnerInterface
{
const PYTHON_STUB = "/../stubs/python.stub";
protected $hotfix;
protected $facts;
public function prepare(Hotfix $hotfix, Facts $facts)
{
throw new NotImplementedException("The Python runner is not implemented");
$this->hotfix = $hotfix;
$this->facts = $facts;
$python = trim(exec("which python"));
if (!$python) {
throw new UnsupportedRunnerException("You need to install python to apply this hotfix");
}
}
public function apply()
{
$head = file(__DIR__.self::PYTHON_STUB, FILE_IGNORE_NEW_LINES);
$body = $this->hotfix->getBody();
$hash = $this->hotfix->getHash();
$facts = $this->facts->getFlat();
$head[] = "def fact(f):";
$head[] = " facts = {";
$tmp = [];
foreach ($facts as $key=>$fact) {
$tmp[] = sprintf(' "%s": "%s"', $key, $fact);
}
$head[] = join(",\n", $tmp)."\n";
$head[] = " }";
$head[] = " return facts[f]";
$head = join("\n", $head);
// Create temporary filename based on the hash of the hotfix
$tmpfile = sys_get_temp_dir()."/hotfix_".$hash;
file_put_contents($tmpfile, $head."\n".$body."\n");
// Execute the hotfix and clean up
passthru("python {$tmpfile}");
unlink($tmpfile);
}
}

2
src/stubs/python.stub Normal file
View File

@ -0,0 +1,2 @@