From 0564c419762096242afc3c86dfa8d49ccc3396ed Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sun, 11 Dec 2016 23:45:21 +0100 Subject: [PATCH] hotfix: Added examples, initial python support --- examples/bash-spinner.fix | 8 ++++++++ examples/php-facts.fix | 10 ++++++++++ examples/python-facts.fix | 10 ++++++++++ src/Runner/PythonRunner.php | 39 ++++++++++++++++++++++++++++++++++++- src/stubs/python.stub | 2 ++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/bash-spinner.fix create mode 100644 examples/php-facts.fix create mode 100644 examples/python-facts.fix create mode 100644 src/stubs/python.stub diff --git a/examples/bash-spinner.fix b/examples/bash-spinner.fix new file mode 100644 index 0000000..cd0c83b --- /dev/null +++ b/examples/bash-spinner.fix @@ -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 +lang: bash +--- + +find /home >/dev/null & spinner diff --git a/examples/php-facts.fix b/examples/php-facts.fix new file mode 100644 index 0000000..8b88502 --- /dev/null +++ b/examples/php-facts.fix @@ -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 +lang: php +--- + +echo "Architecture: ".fact('system.arch')."\n"; +echo "Distribution: ".fact('lsb.id')."\n"; diff --git a/examples/python-facts.fix b/examples/python-facts.fix new file mode 100644 index 0000000..b57345b --- /dev/null +++ b/examples/python-facts.fix @@ -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 +lang: python +--- + +print "Architecture: " + fact("system.arch") +print "Distribution: " + fact("lsb.id") diff --git a/src/Runner/PythonRunner.php b/src/Runner/PythonRunner.php index 724ba43..3435313 100644 --- a/src/Runner/PythonRunner.php +++ b/src/Runner/PythonRunner.php @@ -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); } } \ No newline at end of file diff --git a/src/stubs/python.stub b/src/stubs/python.stub new file mode 100644 index 0000000..139597f --- /dev/null +++ b/src/stubs/python.stub @@ -0,0 +1,2 @@ + +