\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()
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
$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");
- }
- }
}
*/
public function getArgs();
+ /**
+ * Print debug message
+ * @param string $fmt
+ * @param string ...$args
+ */
+ public function debug($fmt);
+
/**
* Print info
* @param string $fmt
// 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 {
$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) {
* @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]);
$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);
}
}
$this->cleanup[] = $local;
- return $local->getPathname();
+ return $local;
}
/**
*/
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;
}
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;
*/
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" : " ");
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");
}
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"]);
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);