From b3b7f2dddc97f6f6c2ca83f6a708fb09ae2ed64c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 21 Mar 2015 20:37:16 +0100 Subject: [PATCH] support git clones and (PECL) package archives as sources --- bin/pharext | Bin 42069 -> 44265 bytes src/pharext/Cli/Command.php | 70 +++++++++++++++++++++++ src/pharext/Installer.php | 69 ----------------------- src/pharext/Packager.php | 109 ++++++++++++++++++++++++++++++++++-- src/pharext/Tempfile.php | 2 +- 5 files changed, 176 insertions(+), 74 deletions(-) diff --git a/bin/pharext b/bin/pharext index 75084747467458d4e9934f105792dd0c8494cbdb..79478198dd95ae29458a0a89bd69ed145bd26e2d 100755 GIT binary patch delta 2046 zcmah~&2Jk;6pu|)x|RYK%~xY5ofs3=Cb1KQ05wf$^U;C`Nz|5z5CwO#9@|USJL}nT zJ{mr~iT#zUi1Y#wG0CC{Zf;e*kseb@M93n1&5MsX8PF*CVL^C_@z2AH9 z_dfjNchO&dihlZLUv%&$d%JHWeyp>tZSZEt%i?_c?0S^l?t4_oe(ifA{(HBQiF4yD z{frGIazeJ0$i}}IR5EdX*!9U4dpD60vab?PiQ_Qwj5xB%yf{{p+4%cWWgyNQcmIjA z_ma}lm&q}q@|)5e=ocP>`TqD&P+j^u8ko!U7^9W`U}yU8M}k|cf5c8KRv?*y6tZt- zud+WYcOG;QbnBka=D_KPE%gB=z{=`)%wWS8cY@K^X?xcqA|*_!Kq}aEFJ^#jp1pOD zWQfI|56z$t()ykOJ2AsB>=H0-zfiP^*^p?Fk){>fADJZ8hKRu%T^)|?t~XzIV^w2$k=LOeWe>tG8y_BF&tYds8;<7~e6;&G+KKCUf=*B&yhuYpT$MF#TO ztVjUN)TjQVBsQ?sRt+NM5KEL zZ@LbKMLs)(@fNfA8egIq2LIJkyXNTS?>X{|Z|O4vhgm=+I*q*!EssQjc--j%V3Ir8Md z6`ozmc95A;5IR4J%GpD(TClLUU7)z2K27Z_OlV}XjOb;lX=a443-|jPndEHvDAZC6 zwlE-Z79p*;u!_vgILq%R_C&n=Pq&`#^OaT6jSbp#!)Q`C_HHa@gzYxFMG3U1?f^(Q zu^Y0Tz&Q`f7Nj=eRBP*&=INE)gA@iT0zOm7qL^$m-b8!)JtSMrSh|RO+MJd~|HyC^ zxjGx$EnzdI0~F<^_-Ks!yePqNiooGov6>|KVgP3>!tK5oUBqoGv!H zpXd>^iT|(<6l>$bRQ*yU)10OA{yJYT1bEQ*H!&#!>;hUrd_%iFcH|CE@y%ZKXv-MM zDGIH`=mZ~a444rkxP9QaCx?Ij>+YQo=;4{y+8^lscJ$R9aPT?*$!)th_0rV8ENF@J delta 319 zcmaEPlj-USrVZ^cD4#q*S`#FRK1D`sKj+&njy ziF@)FpQy>~na4NtWj$e?oMWNB`CdUY<7CAe`NIJkadUU6HRI-nS{Ft}&dDFOgg0B)%V}(mT3*a5^P%#u#`#+_ a7%StZ?^yilzTA=PF)BbeGq^kXIspI=j&30U diff --git a/src/pharext/Cli/Command.php b/src/pharext/Cli/Command.php index 0ec52ff..706cb85 100644 --- a/src/pharext/Cli/Command.php +++ b/src/pharext/Cli/Command.php @@ -163,4 +163,74 @@ trait Command $this->error(null); } } + + /** + * Execute a program with escalated privileges handling interactive password prompt + * @param string $command + * @param string $output + * @return int + */ + 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; + } + return proc_close($proc); + } + + /** + * Execute a system command + * @param string $name pretty name + * @param string $command command + * @param array $args command arguments + * @param bool $sudo whether the command may need escalated privileges + */ + private function exec($name, $command, array $args = null, $sudo = false) { + $exec = escapeshellcmd($command); + if ($args) { + $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args)); + } + + if ($this->args->verbose) { + $this->info("Running %s ...\n", $exec); + } else { + $this->info("Running %s ... ", $name); + } + + if ($sudo && isset($this->args->sudo)) { + $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output); + } elseif ($this->args->verbose) { + passthru($exec ." 2>&1", $retval); + } else { + exec($exec ." 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", $output); + } + exit(2); + } + if (!$this->args->verbose) { + // we already have a bunch of output + $this->info("OK\n"); + } + } } diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 4caebc0..5e86f31 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -186,75 +186,6 @@ class Installer implements Command } } - /** - * Execute a program with escalated privileges handling interactive password prompt - * @param string $command - * @param string $output - * @return int - */ - 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; - } - return proc_close($proc); - } - /** - * Execute a system command - * @param string $name pretty name - * @param string $command command - * @param array $args command arguments - * @param bool $sudo whether the command may need escalated privileges - */ - private function exec($name, $command, array $args = null, $sudo = false) { - $exec = escapeshellcmd($command); - if ($args) { - $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args)); - } - - if ($this->args->verbose) { - $this->info("Running %s ...\n", $exec); - } else { - $this->info("Running %s ... ", $name); - } - - if ($sudo && isset($this->args->sudo)) { - $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output); - } elseif ($this->args->verbose) { - passthru($exec ." 2>&1", $retval); - } else { - exec($exec ." 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", $output); - } - exit(2); - } - if (!$this->args->verbose) { - // we already have a bunch of output - $this->info("OK\n"); - } - } - /** * Construct a command from prefix common-name and suffix * @param type $suffix diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 4d432ed..33aa72e 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,6 +3,7 @@ namespace pharext; use Phar; +use PharData; use pharext\Cli\Args as CliArgs; use pharext\Cli\Command as CliCommand; @@ -19,6 +20,12 @@ class Packager implements Command */ private $source; + /** + * Cleanups + * @var array + */ + private $cleanup = []; + /** * Create the command */ @@ -54,6 +61,19 @@ class Packager implements Command ]); } + /** + * Perform cleaniup + */ + function __destruct() { + foreach ($this->cleanup as $cleanup) { + if (is_dir($cleanup)) { + $this->rm($cleanup); + } else { + unlink($cleanup); + } + } + } + /** * @inheritdoc * @see \pharext\Command::run() @@ -76,12 +96,13 @@ class Packager implements Command try { if ($this->args["source"]) { + $source = $this->localize($this->args["source"]); if ($this->args["pecl"]) { - $this->source = new SourceDir\Pecl($this, $this->args["source"]); + $this->source = new SourceDir\Pecl($this, $source); } elseif ($this->args["git"]) { - $this->source = new SourceDir\Git($this, $this->args["source"]); + $this->source = new SourceDir\Git($this, $source); } else { - $this->source = new SourceDir\Pharext($this, $this->args["source"]); + $this->source = new SourceDir\Pharext($this, $source); } } } catch (\Exception $e) { @@ -109,6 +130,11 @@ class Packager implements Command $this->createPackage(); } + /** + * Dump program signature + * @param string $prog + * @return int exit code + */ function signature($prog) { try { $sig = (new Phar(Phar::running(false)))->getSignature(); @@ -121,6 +147,76 @@ class Packager implements Command } } + /** + * Download remote source + * @param string $source + * @return string local source + */ + private function download($source) { + if ($this->args["git"]) { + $local = $this->newtemp("gitclone"); + $this->exec("git clone", "git", ["clone", $source, $local]); + $source = $local; + } else { + $this->info("Fetching remote source %s ... ", $source); + if (!$remote = fopen($source, "r")) { + $this->error(null); + exit(2); + } + $local = new Tempfile("remote"); + if (!stream_copy_to_stream($remote, $local->getStream())) { + $this->error(null); + exit(2); + } + $local->closeStream(); + $source = $local->getPathname(); + $this->info("OK\n"); + } + + $this->cleanup[] = $local; + return $source; + } + + /** + * Extract local archive + * @param stirng $source + * @return string extracted directory + */ + private function extract($source) { + $dest = $this->newtemp("local"); + $this->info("Extracting to %s ... ", $dest); + $archive = new PharData($source); + $archive->extractTo($dest); + $this->info("OK\n"); + $this->cleanup[] = $dest; + return $dest; + } + + /** + * Localize a possibly remote source + * @param string $source + * @return string local source directory + */ + private function localize($source) { + if (!stream_is_local($source)) { + $source = $this->download($source); + } + if (!is_dir($source)) { + $source = $this->extract($source); + if ($this->args["pecl"]) { + $this->info("Sanitizing PECL dir ... "); + $dirs = glob("$source/*", GLOB_ONLYDIR); + $files = array_diff(glob("$source/*"), $dirs); + $source = current($dirs); + foreach ($files as $file) { + rename($file, "$source/" . basename($file)); + } + $this->info("OK\n"); + } + } + return $source; + } + /** * Traverses all pharext source files to bundle * @return Generator @@ -133,7 +229,12 @@ class Packager implements Command } } - + + /** + * Ask for password on the console + * @param string $prompt + * @return string password + */ private function askpass($prompt = "Password:") { system("stty -echo", $retval); if ($retval) { diff --git a/src/pharext/Tempfile.php b/src/pharext/Tempfile.php index 13ac411..d31f457 100644 --- a/src/pharext/Tempfile.php +++ b/src/pharext/Tempfile.php @@ -27,7 +27,7 @@ class Tempfile extends \SplFileInfo function __destruct() { @unlink($this->getPathname()); } - + function closeStream() { fclose($this->handle); } -- 2.30.2