From 8b991a8b633610f7309d76f15b388bffd073d1f7 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 22 Mar 2015 09:09:29 +0100 Subject: [PATCH] download progress, cleanups --- bin/pharext | Bin 48777 -> 47475 bytes src/pharext/Cli/Command.php | 108 ++++-------------------------------- src/pharext/Command.php | 7 +++ src/pharext/Installer.php | 16 ++++-- src/pharext/Packager.php | 41 ++++++++++---- src/pharext/Tempdir.php | 2 +- src/pharext/Tempfile.php | 5 +- 7 files changed, 62 insertions(+), 117 deletions(-) diff --git a/bin/pharext b/bin/pharext index 65f4d24c12310395bd647732db313df98912fea3..cd8fe7c6d5c09b5260e70523259b3396f3abf322 100755 GIT binary patch delta 1442 zcma)6TWl0n815FKEJR(Ca>-uo={RF%*6nUrF_kv0&~`y7y`)_dNo^+6nKQd%+1cqi zXW15P`k+F)>H{1kCWa?8@xiEJ5->ob4?uoe z)d1a4j?v)2_v@>^^m=EME)BfBsUGQ`8Bn7ySSFC^#rIn1w~k0#zuM}y=Z`eiwP&5z z19Nn#cyq1lcjxRHm3wUZqb+p(LMOd@>{WVdAw>5bzdWxOZNhp7-2f8=wSwv^RGqB| zl*cC$W(?{YhF~l^n%O<54h?7D-*Zsy8y*_U^kqA%(ZQfd z@)mCE@%R%yv<0Qhw@k85ru@ZPoOuQ`GCI6>G&43g}ZJ z&b5?lt3g0|%a9~6F5w;tPXHm|*EYWrHJ)y7uNfF21gQu*Y!_sqTSx#@EZK(K(H^gt zp(qL91FiOF3F`iGx@~2MQj6dOvHWbzJqJ@&#s^3CXR?|88dIq_;H(4oW9IW%l)VFdBDGA(_bw6O7i8Ib#Gz^SvE0c zLBc5svum#*28Cy_nuA0o>^V>k3(1US(V5DXY-`$0pZI#65AnyT=2qz${@m9xWYIy; zv}WAR=l29g8CxSPL%&8eU_)qNpaFx?X4#BI2>8omYX#Ni_Y^mA7%Q6pkH~#4asX8!Pc&GjY~toyO~=G|s@zu z+&B$Y3M2|`5K;P6frL;_zyXk=tV9nSXfJR91U+$pLj(s@ae)dD5=G4HuH6Vr_8ZTe z_r34^-uvBq^}cs|E$qF04z7mNfrmOQ1mTZx zA2SF?js#G|(J|?Lx&9r@La?DF~-RHI5rWnJ{%cthlek8L@A1Hh{hsfSQ$w%aFOWruO?P7?B+V#ss=78b^B=6 zO6<5f+Z`g*jy>TyM?Zi#S9?Yb<(y+Gg_it2pU>5xQB>2E3yQ?2PSESi^lM;JGc;sY z4Mig*KAtc$2~$Xz@b!A=xb3%z%WN+D{k}|pzu$-Y(YS#{f{}yW0u4d#Wqx!*zx5kJ(x~fIUIj zh&D;&Mjp~b*5)&s zr_D(VbmV&j6Lnmy*>ReSCS?hO`A)cXx+B1nk7(`*c~C3(_^MS}uwz_3D<_&V8fH z+&e}*on;y!j*e!NLrvGzgHs#o1GX-8fiMz4RnatwY}9z09@=B$ID`J_!TrwBX%7y} z=>+7jCGvK~5#?VJ>5NHeK_N2Y6K2W@x0z9wwqwqsQcM#QzCi}D@YbIU3%&>A;vh8H z(qUsA>9pxlCq?k>ND#D*16(G97U;mJba13aB#Rf>^qjA0MH%TOCuOx7!y#m%q2AU)f^P`b#MV;qB$ahnz`Lw35!p$5mZZv@%>- z?gDH18+d7X1pa<44!ft2HTAm3^GG^d#^hO%$o3LR!Su=wb=5QI7RN!Es1rjh5(tS)Lv&UVfD+qEs!F@tJ<1Ru0&h8xB r&lEz#+FC_r>diyO%Jm=Ux^6i=Ha+$q@Ny(D diff --git a/src/pharext/Cli/Command.php b/src/pharext/Cli/Command.php index 706cb85..e19ce93 100644 --- a/src/pharext/Cli/Command.php +++ b/src/pharext/Cli/Command.php @@ -30,6 +30,16 @@ trait Command \pharext\VERSION); } + /** + * @inheritdoc + * @see \pharext\Command::debug() + */ + public function debug($fmt) { + if ($this->args->verbose) { + vprintf($fmt, array_slice(func_get_args(), 1)); + } + } + /** * @inheritdoc * @see \pharext\Command::info() @@ -117,34 +127,6 @@ trait Command printf("\n"); } - /** - * Create temporary file/directory name - * @param string $prefix - * @param string $suffix - */ - private function tempname($prefix, $suffix = null) { - if (!isset($suffix)) { - $suffix = uniqid(); - } - return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix); - } - - /** - * Create a new temp directory - * @param string $prefix - * @return string - */ - private function newtemp($prefix) { - $temp = $this->tempname($prefix); - if (!is_dir($temp)) { - if (!mkdir($temp, 0700, true)) { - $this->error(null); - exit(3); - } - } - return $temp; - } - /** * rm -r * @param string $dir @@ -163,74 +145,4 @@ 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/Command.php b/src/pharext/Command.php index f7b0c74..b174f7d 100644 --- a/src/pharext/Command.php +++ b/src/pharext/Command.php @@ -13,6 +13,13 @@ interface Command */ public function getArgs(); + /** + * Print debug message + * @param string $fmt + * @param string ...$args + */ + public function debug($fmt); + /** * Print info * @param string $fmt diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index b41d7fd..94f8149 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -179,7 +179,9 @@ class Installer implements Command // install $this->info("Running make install ... "); - $cmd->setSu($this->args->sudo); + if (isset($this->args->sudo)) { + $cmd->setSu($this->args->sudo); + } if ($this->args->verbose) { $cmd->run(["install"]); } else { @@ -258,20 +260,26 @@ class Installer implements Command $this->info("Running INI owner transfer ... "); $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]); $cmd = new ExecCmd("chown", $this->args->verbose); - $cmd->setSu($this->args->sudo); + if (isset($this->args->sudo)) { + $cmd->setSu($this->args->sudo); + } $cmd->run([$ugid, $path]); $this->info("OK\n"); $this->info("Running INI permission transfer ... "); $perm = decoct($stat["mode"] & 0777); $cmd = new ExecCmd("chmod", $this->args->verbose); - $cmd->setSu($this->args->sudo); + if (isset($this->args->sudo)) { + $cmd->setSu($this->args->sudo); + } $cmd->run([$perm, $path]); $this->info("OK\n"); $this->info("Running INI activation ... "); $cmd = new ExecCmd("mv", $this->args->verbose); - $cmd->setSu($this->args->sudo); + if (isset($this->args->sudo)) { + $cmd->setSu($this->args->sudo); + } $cmd->run([$path, $file]); $this->info("OK\n"); } catch (\Exception $e) { diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index d66e83b..d3e4000 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -160,8 +160,8 @@ class Packager implements Command * @return string local source */ private function download($source) { + $this->info("Fetching remote source %s ... ", $source); if ($this->args["git"]) { - $this->info("Cloning %s ... ", $source); $local = new Tempdir("gitclone"); $cmd = new ExecCmd("git", $this->args->verbose); $cmd->run(["clone", $source, $local]); @@ -169,8 +169,27 @@ class Packager implements Command $this->info("OK\n"); } } else { - $this->info("Fetching remote source %s ... ", $source); - if (!$remote = fopen($source, "r")) { + $context = stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) { + switch ($notification) { + case STREAM_NOTIFY_CONNECT: + $this->debug("\n"); + break; + case STREAM_NOTIFY_PROGRESS: + if ($bytes_max) { + $bytes_pct = $bytes_cur/$bytes_max; + $this->debug("\r %3d%% [%s>%s] ", + $bytes_pct*100, + str_repeat("=", round(70*$bytes_pct)), + str_repeat(" ", round(70*(1-$bytes_pct))) + ); + } + break; + case STREAM_NOTIFY_COMPLETED: + /* this is not generated, why? */ + break; + } + }]); + if (!$remote = fopen($source, "r", false, $context)) { $this->error(null); exit(2); } @@ -184,7 +203,7 @@ class Packager implements Command } $this->cleanup[] = $local; - return $local->getPathname(); + return $local; } /** @@ -194,12 +213,10 @@ class Packager implements Command */ private function extract($source) { $dest = new Tempdir("local"); - if ($this->args->verbose) { - $this->info("Extracting to %s ... ", $dest); - } + $this->debug("Extracting %s to %s ... ", $source, $dest); $archive = new PharData($source); $archive->extractTo($dest); - $this->info("OK\n"); + $this->debug("OK\n"); $this->cleanup[] = $dest; return $dest; } @@ -216,14 +233,14 @@ class Packager implements Command if (!is_dir($source)) { $source = $this->extract($source); if ($this->args["pecl"]) { - $this->info("Sanitizing PECL dir ... "); + $this->debug("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"); + $this->debug("OK\n"); } } return $source; @@ -266,7 +283,7 @@ class Packager implements Command */ private function createPackage() { $pkguniq = uniqid(); - $pkgtemp = $this->tempname($pkguniq, "phar"); + $pkgtemp = sprintf("%s/%s.phar", sys_get_temp_dir(), $pkguniq); $pkgdesc = "{$this->args->name}-{$this->args->release}"; $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " "); @@ -290,7 +307,7 @@ class Packager implements Command if (!chmod($pkgtemp, 0777)) { $this->error(null); } elseif ($this->args->verbose) { - $this->info("Created executable phar %s\n", $pkgtemp); + $this->debug("Created executable phar %s\n", $pkgtemp); } else { $this->info("OK\n"); } diff --git a/src/pharext/Tempdir.php b/src/pharext/Tempdir.php index 585033a..2de174d 100644 --- a/src/pharext/Tempdir.php +++ b/src/pharext/Tempdir.php @@ -7,7 +7,7 @@ class Tempdir extends \SplFileInfo private $dir; public function __construct($prefix) { - $temp = sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, uniqid()); + $temp = sprintf("%s/%s", sys_get_temp_dir(), uniqid($prefix)); if (!is_dir($temp)) { if (!mkdir($temp, 0700, true)) { throw new Exception("Could not create tempdir: ".error_get_last()["message"]); diff --git a/src/pharext/Tempfile.php b/src/pharext/Tempfile.php index d31f457..c890cd9 100644 --- a/src/pharext/Tempfile.php +++ b/src/pharext/Tempfile.php @@ -8,11 +8,12 @@ class Tempfile extends \SplFileInfo function __construct($prefix) { $tries = 0; - $template = sys_get_temp_dir()."/$prefix."; + /* PharData needs a dot in the filename, sure */ + $temp = sys_get_temp_dir() . "/"; $omask = umask(077); do { - $path = $template.uniqid(); + $path = $temp.uniqid($prefix).".tmp"; $this->handle = fopen($path, "x"); } while (!is_resource($this->handle) && $tries++ < 10); umask($omask); -- 2.30.2