X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FInstaller.php;h=6aae795cd19373d45285837393eaa80eb1617606;hb=aca5c81d18cbc301bbffba4313c674da1dbc9a55;hp=626cfa6a95b30f0254f9f1e290bd89864ce670f6;hpb=e1c1b76f0a87c7573e30468763d47c0b97b11e8e;p=pharext%2Fpharext diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 626cfa6..6aae795 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -11,6 +11,18 @@ class Installer implements Command { use CliCommand; + /** + * The temporary directory we should operate in + * @var string + */ + private $tmp; + + /** + * The directory we came from + * @var string + */ + private $cwd; + /** * Create the command */ @@ -35,15 +47,37 @@ class Installer implements Command ]); } + /** + * Cleanup temp directory + */ + public function __destruct() { + $this->cleanup(); + } + /** * @inheritdoc * @see \pharext\Command::run() */ public function run($argc, array $argv) { - if (($hook = stream_resolve_include_path("pharext_install.php"))) { - $callable = include $hook; - if (is_callable($callable)) { - $recv = $callable($this); + $this->cwd = getcwd(); + $this->tmp = $this->tempname(basename(Phar::running(false))); + + $phar = new Phar(Phar::running(false)); + foreach ($phar as $entry) { + if (fnmatch("*.ext.phar*", $entry->getBaseName())) { + $temp = $this->newtemp($entry->getBaseName()); + $phar->extractTo($temp, $entry->getFilename(), true); + $phars[$temp] = new Phar($temp."/".$entry->getFilename()); + } + } + $phars[$this->tmp] = $phar; + + foreach ($phars as $phar) { + if (isset($phar["pharext_install.php"])) { + $callable = include $phar["pharext_install.php"]; + if (is_callable($callable)) { + $recv[] = $callable($this); + } } } @@ -77,55 +111,115 @@ class Installer implements Command } if (isset($recv)) { - $recv($this); + foreach ($recv as $r) { + $r($this); + } } - - $this->installPackage(); + foreach ($phars as $temp => $phar) { + $this->installPackage($phar, $temp); + } + } + + private function newtemp($prefix) { + $temp = $this->tempname($prefix); + if (!is_dir($temp)) { + if (!mkdir($temp, 0750, true)) { + $this->error(null); + exit(3); + } + } + return $temp; } /** - * @inheritdoc - * @see \pharext\Command::getArgs() + * Prepares, configures, builds and installs the extension */ - public function getArgs() { - return $this->args; + private function installPackage(Phar $phar, $temp) { + $this->info("Installing %s ... \n", basename($phar->getAlias())); + try { + $phar->extractTo($temp, null, true); + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(3); + } + + if (!chdir($temp)) { + $this->error(null); + exit(4); + } + + $this->exec("phpize", $this->php("ize")); + $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". + implode(" ", (array) $this->args->configure)); + $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3"); + $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true); + + $this->cleanup($temp); + + $this->info("Don't forget to activiate the extension in your php.ini!\n\n"); } - + /** - * @inheritdoc - * @see \pharext\Command::info() + * Perform any cleanups */ - public function info($fmt) { - if (!$this->args->quiet) { - vprintf($fmt, array_slice(func_get_args(), 1)); + private function cleanup($temp = null) { + if (!isset($temp)) { + $temp = $this->tmp; + } + if (is_dir($temp)) { + chdir($this->cwd); + $this->info("Cleaning up %s ...\n", $temp); + $this->rm($temp); } } - + /** - * @inheritdoc - * @see \pharext\Command::error() + * rm -r + * @param string $dir */ - public function error($fmt) { - if (!$this->args->quiet) { - vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1)); + private function rm($dir) { + foreach (scandir($dir) as $entry) { + if ($entry === "." || $entry === "..") { + continue; + } elseif (is_dir("$dir/$entry")) { + $this->rm("$dir/$entry"); + } elseif (!unlink("$dir/$entry")) { + $this->error(null); + } + } + if (!rmdir($dir)) { + $this->error(null); } } /** - * Extract the phar to a temporary directory + * Execute a program with escalated privileges handling interactive password prompt + * @param string $command + * @param string $output + * @return int */ - private function extract() { - if (!$file = Phar::running(false)) { - $this->error("Did your run the ext.phar?\n"); - exit(3); + private function sudo($command, &$output) { + if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) { + return -1; + } + $stdout = $pipes[1]; + $passwd = 0; + while (!feof($stdout)) { + $R = [$stdout]; $W = []; $E = []; + if (!stream_select($R, $W, $E, null)) { + continue; + } + $data = fread($stdout, 0x1000); + /* only check a few times */ + if ($passwd++ < 10) { + if (stristr($data, "password")) { + printf("\n%s", $data); + } + } + $output .= $data; } - $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar"); - is_dir($temp) or mkdir($temp, 0750, true); - $phar = new Phar($file); - $phar->extractTo($temp, null, true); - chdir($temp); + return proc_close($proc); } - /** * Execute a system command * @param string $name pretty name @@ -135,20 +229,17 @@ class Installer implements Command private function exec($name, $command, $sudo = false) { $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " "); if ($sudo && isset($this->args->sudo)) { - if (($proc = proc_open(sprintf($this->args->sudo, $command)." 2>&1", [STDIN,STDOUT,STDERR], $pipes))) { - $retval = proc_close($proc); - } else { - $retval = -1; - } + $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output); } elseif ($this->args->verbose) { passthru($command ." 2>&1", $retval); } else { exec($command ." 2>&1", $output, $retval); + $output = implode("\n", $output); } if ($retval) { $this->error("Command %s failed with (%s)\n", $command, $retval); if (isset($output) && !$this->args->quiet) { - printf("%s\n", implode("\n", $output)); + printf("%s\n", $output); } exit(2); } @@ -167,16 +258,4 @@ class Installer implements Command } return $cmd; } - - /** - * Prepares, configures, builds and installs the extension - */ - private function installPackage() { - $this->extract(); - $this->exec("phpize", $this->php("ize")); - $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". - implode(" ", (array) $this->args->configure)); - $this->exec("make", "make -sj3"); - $this->exec("install", "make -s install", true); - } }