$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");
+ }
+ }
}
}
}
- /**
- * 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
namespace pharext;
use Phar;
+use PharData;
use pharext\Cli\Args as CliArgs;
use pharext\Cli\Command as CliCommand;
*/
private $source;
+ /**
+ * Cleanups
+ * @var array
+ */
+ private $cleanup = [];
+
/**
* Create the 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()
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) {
$this->createPackage();
}
+ /**
+ * Dump program signature
+ * @param string $prog
+ * @return int exit code
+ */
function signature($prog) {
try {
$sig = (new Phar(Phar::running(false)))->getSignature();
}
}
+ /**
+ * 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
}
}
-
+
+ /**
+ * Ask for password on the console
+ * @param string $prompt
+ * @return string password
+ */
private function askpass($prompt = "Password:") {
system("stty -echo", $retval);
if ($retval) {
function __destruct() {
@unlink($this->getPathname());
}
-
+
function closeStream() {
fclose($this->handle);
}