/** * Launch an editor and edit the content. The edited content will be * returned. * * @return string Edited content. * @throws Exception The editor exited abnormally or something untoward * occurred. * * @task edit */ public function editInteractively() { $name = $this->getName(); $content = $this->getContent(); if (phutil_is_windows()) { $content = str_replace("\n", "\r\n", $content); } $tmp = Filesystem::createTemporaryDirectory('edit.'); $path = $tmp . DIRECTORY_SEPARATOR . $name; try { Filesystem::writeFile($path, $content); } catch (Exception $ex) { Filesystem::remove($tmp); throw $ex; } $editor = $this->getEditor(); $offset = $this->getLineOffset(); $err = $this->invokeEditor($editor, $path, $offset); if ($err) { Filesystem::remove($tmp); throw new Exception("Editor exited with an error code (#{$err})."); } try { $result = Filesystem::readFile($path); Filesystem::remove($tmp); } catch (Exception $ex) { Filesystem::remove($tmp); throw $ex; } if (phutil_is_windows()) { $result = str_replace("\r\n", "\n", $result); } $this->setContent($result); return $this->getContent(); }
private function detectCopiesIn($file) { $root = dirname(__FILE__) . '/diff/'; $parser = new ArcanistDiffParser(); $diff = DifferentialDiff::newFromRawChanges(PhabricatorUser::getOmnipotentUser(), $parser->parseDiff(Filesystem::readFile($root . $file))); return idx(head($diff->getChangesets())->getMetadata(), 'copy:lines'); }
public function getKeys(array $keys) { $this->validateKeys($keys); try { $this->lockCache(); } catch (PhutilLockException $ex) { return array(); } $now = time(); $results = array(); foreach ($keys as $key) { $key_file = $this->getKeyFile($key); try { $data = Filesystem::readFile($key_file); } catch (FilesystemException $ex) { continue; } $data = unserialize($data); if (!$data) { continue; } if (isset($data['ttl']) && $data['ttl'] < $now) { continue; } $results[$key] = $data['value']; } $this->unlockCache(); return $results; }
private function readTestCases($path) { $files = Filesystem::listDirectory($path, $include_hidden = false); $tests = array(); foreach ($files as $file) { $data = Filesystem::readFile($path . $file); $parts = preg_split('/^~{5,}$/m', $data); if (count($parts) < 2) { throw new Exception(pht('Expected test file "%s" to contain an input section in JSON, ' . 'then an expected result section in JSON, with the two sections ' . 'separated by a line of "~~~~~", but the divider is not present ' . 'in the file.', $file)); } else { if (count($parts) > 2) { throw new Exception(pht('Expected test file "%s" to contain exactly two sections, ' . 'but it has more than two sections.')); } } list($input, $expect) = $parts; try { $input = phutil_json_decode($input); $expect = phutil_json_decode($expect); } catch (Exception $ex) { throw new PhutilProxyException(pht('Exception while decoding test data for test "%s".', $file), $ex); } $tests[$file] = array('input' => $input, 'expect' => $expect); } return $tests; }
public function execute(PhutilArgumentParser $args) { $console = PhutilConsole::getConsole(); $public_keyfile = $args->getArg('public'); if (!strlen($public_keyfile)) { throw new PhutilArgumentUsageException(pht('You must specify the path to a public keyfile with %s.', '--public')); } if (!Filesystem::pathExists($public_keyfile)) { throw new PhutilArgumentUsageException(pht('Specified public keyfile "%s" does not exist!', $public_keyfile)); } $public_key = Filesystem::readFile($public_keyfile); $pkcs8_keyfile = $args->getArg('pkcs8'); if (!strlen($pkcs8_keyfile)) { throw new PhutilArgumentUsageException(pht('You must specify the path to a pkcs8 keyfile with %s.', '--pkc8s')); } if (!Filesystem::pathExists($pkcs8_keyfile)) { throw new PhutilArgumentUsageException(pht('Specified pkcs8 keyfile "%s" does not exist!', $pkcs8_keyfile)); } $pkcs8_key = Filesystem::readFile($pkcs8_keyfile); $warning = pht('Adding a PKCS8 keyfile to the cache can be very dangerous. If the ' . 'PKCS8 file really encodes a different public key than the one ' . 'specified, an attacker could use it to gain unauthorized access.' . "\n\n" . 'Generally, you should use this option only in a development ' . 'environment where ssh-keygen is broken and it is inconvenient to ' . 'fix it, and only if you are certain you understand the risks. You ' . 'should never cache a PKCS8 file you did not generate yourself.'); $console->writeOut("%s\n", phutil_console_wrap($warning)); $prompt = pht('Really trust this PKCS8 keyfile?'); if (!phutil_console_confirm($prompt)) { throw new PhutilArgumentUsageException(pht('Aborted workflow.')); } $key = PhabricatorAuthSSHPublicKey::newFromRawKey($public_key); $key->forcePopulatePKCS8Cache($pkcs8_key); $console->writeOut("%s\n", pht('Cached PKCS8 key for public key.')); return 0; }
private function markupText($markup_file) { $contents = Filesystem::readFile($markup_file); $file = basename($markup_file); $parts = explode("\n~~~~~~~~~~\n", $contents); $this->assertEqual(3, count($parts), $markup_file); list($input_remarkup, $expected_output, $expected_text) = $parts; $engine = $this->buildNewTestEngine(); switch ($file) { case 'raw-escape.txt': // NOTE: Here, we want to test PhutilRemarkupRuleEscapeRemarkup and // PhutilRemarkupBlockStorage, which are triggered by "\1". In the // test, "~" is used as a placeholder for "\1" since it's hard to type // "\1". $input_remarkup = str_replace("~", "", $input_remarkup); $expected_output = str_replace("~", "", $expected_output); $expected_text = str_replace("~", "", $expected_text); break; case 'toc.txt': $engine->setConfig('header.generate-toc', true); break; } $actual_output = (string) $engine->markupText($input_remarkup); switch ($file) { case 'toc.txt': $table_of_contents = PhutilRemarkupEngineRemarkupHeaderBlockRule::renderTableOfContents($engine); $actual_output = $table_of_contents . "\n\n" . $actual_output; break; } $this->assertEqual($expected_output, $actual_output, "Failed to markup HTML in file '{$file}'."); $engine->setMode(PhutilRemarkupEngine::MODE_TEXT); $actual_output = (string) $engine->markupText($input_remarkup); $this->assertEqual($expected_text, $actual_output, "Failed to markup text in file '{$file}'."); }
public function testParseAll() { $root = dirname(__FILE__) . '/mercurial/'; foreach (Filesystem::listDirectory($root, $hidden = false) as $file) { $this->parseData(basename($file), Filesystem::readFile($root . '/' . $file)); } }
public function run() { $conduit = $this->getConduit(); $results = array(); foreach ($this->paths as $path) { $name = basename($path); $this->writeStatusMessage("Uploading '{$name}'...\n"); try { $data = Filesystem::readFile($path); } catch (FilesystemException $ex) { $this->writeStatusMessage("Unable to upload file: " . $ex->getMessage() . "\n"); $results[$path] = null; continue; } $phid = $conduit->callMethodSynchronous('file.upload', array('data_base64' => base64_encode($data), 'name' => $name)); $info = $conduit->callMethodSynchronous('file.info', array('phid' => $phid)); $results[$path] = $info; if (!$this->getJSON()) { echo " {$name}: " . $info['uri'] . "\n\n"; } } if ($this->getJSON()) { echo json_encode($results) . "\n"; } else { $this->writeStatusMessage("Done.\n"); } return 0; }
public function testDifferentialCommitMessageParser() { $dir = dirname(__FILE__) . '/messages/'; $list = Filesystem::listDirectory($dir, $include_hidden = false); foreach ($list as $file) { if (!preg_match('/.txt$/', $file)) { continue; } $data = Filesystem::readFile($dir . $file); $divider = "~~~~~~~~~~\n"; $parts = explode($divider, $data); if (count($parts) !== 4) { throw new Exception(pht('Expected test file "%s" to contain four parts (message, fields, ' . 'output, errors) divided by "%s".', $file, '~~~~~~~~~~')); } list($message, $fields, $output, $errors) = $parts; $fields = phutil_json_decode($fields); $output = phutil_json_decode($output); $errors = phutil_json_decode($errors); $parser = id(new DifferentialCommitMessageParser())->setLabelMap($fields)->setTitleKey('title')->setSummaryKey('summary'); $result_output = $parser->parseCorpus($message); $result_errors = $parser->getErrors(); $this->assertEqual($output, $result_output); $this->assertEqual($errors, $result_errors); } }
protected function readBookConfiguration($book_path) { if ($book_path === null) { throw new PhutilArgumentUsageException('Specify a Diviner book configuration file with --book.'); } $book_data = Filesystem::readFile($book_path); $book = json_decode($book_data, true); if (!is_array($book)) { throw new PhutilArgumentUsageException("Book configuration '{$book_path}' is not in JSON format."); } PhutilTypeSpec::checkMap($book, array('name' => 'string', 'title' => 'optional string', 'short' => 'optional string', 'preface' => 'optional string', 'root' => 'optional string', 'uri.source' => 'optional string', 'rules' => 'optional map<regex, string>', 'exclude' => 'optional regex|list<regex>', 'groups' => 'optional map<string, map<string, wild>>')); // If the book specifies a "root", resolve it; otherwise, use the directory // the book configuration file lives in. $full_path = dirname(Filesystem::resolvePath($book_path)); if (empty($book['root'])) { $book['root'] = '.'; } $book['root'] = Filesystem::resolvePath($book['root'], $full_path); if (!preg_match('/^[a-z][a-z-]*\\z/', $book['name'])) { $name = $book['name']; throw new PhutilArgumentUsageException("Book configuration '{$book_path}' has name '{$name}', but book names " . "must include only lowercase letters and hyphens."); } foreach (idx($book, 'groups', array()) as $group) { PhutilTypeSpec::checkmap($group, array('name' => 'string', 'include' => 'optional regex|list<regex>')); } $this->bookConfigPath = $book_path; $this->config = $book; }
public function testParseRender() { $dir = dirname(__FILE__) . '/data/'; foreach (Filesystem::listDirectory($dir, $show_hidden = false) as $file) { if (!preg_match('/\\.diff$/', $file)) { continue; } $data = Filesystem::readFile($dir . $file); $opt_file = $dir . $file . '.options'; if (Filesystem::pathExists($opt_file)) { $options = Filesystem::readFile($opt_file); $options = json_decode($options, true); if (!is_array($options)) { throw new Exception("Invalid options file: {$opt_file}."); } } else { $options = array(); } foreach (array('one', 'two') as $type) { $parser = $this->buildChangesetParser($type, $data, $file); $actual = $parser->render(null, null, array()); $expect = Filesystem::readFile($dir . $file . '.' . $type . '.expect'); $this->assertEqual($expect, (string) $actual, $file . '.' . $type); } } }
private static function loadSkinSpecification($path) { $config_path = $path . DIRECTORY_SEPARATOR . 'skin.json'; $config = array(); if (Filesystem::pathExists($config_path)) { $config = Filesystem::readFile($config_path); try { $config = phutil_json_decode($config); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht("Skin configuration file '%s' is not a valid JSON file.", $config_path), $ex); } $type = idx($config, 'type', self::TYPE_BASIC); } else { $type = self::TYPE_BASIC; } $spec = new PhameSkinSpecification(); $spec->setRootDirectory($path); $spec->setConfig($config); switch ($type) { case self::TYPE_BASIC: $spec->setSkinClass('PhameBasicTemplateBlogSkin'); break; case self::TYPE_ADVANCED: $spec->setSkinClass($config['class']); $spec->addPhutilLibrary($path . DIRECTORY_SEPARATOR . 'src'); break; default: throw new Exception(pht('Unknown skin type!')); } $spec->setType($type); return $spec; }
public function run() { $argv = $this->getArgv(); if (count($argv) !== 1) { throw new Exception("usage: PhabricatorIRCBot <json_config_file>"); } $json_raw = Filesystem::readFile($argv[0]); $config = json_decode($json_raw, true); if (!is_array($config)) { throw new Exception("File '{$argv[0]}' is not valid JSON!"); } $server = idx($config, 'server'); $port = idx($config, 'port', 6667); $handlers = idx($config, 'handlers', array()); $pass = idx($config, 'pass'); $nick = idx($config, 'nick', 'phabot'); $user = idx($config, 'user', $nick); $ssl = idx($config, 'ssl', false); $nickpass = idx($config, 'nickpass'); $this->config = $config; if (!preg_match('/^[A-Za-z0-9_`[{}^|\\]\\-]+$/', $nick)) { throw new Exception("Nickname '{$nick}' is invalid!"); } foreach ($handlers as $handler) { $obj = newv($handler, array($this)); $this->handlers[] = $obj; } $conduit_uri = idx($config, 'conduit.uri'); if ($conduit_uri) { $conduit_user = idx($config, 'conduit.user'); $conduit_cert = idx($config, 'conduit.cert'); $conduit = new ConduitClient($conduit_uri); $response = $conduit->callMethodSynchronous('conduit.connect', array('client' => 'PhabricatorIRCBot', 'clientVersion' => '1.0', 'clientDescription' => php_uname('n') . ':' . $nick, 'user' => $conduit_user, 'certificate' => $conduit_cert)); $this->conduit = $conduit; } $errno = null; $error = null; if (!$ssl) { $socket = fsockopen($server, $port, $errno, $error); } else { $socket = fsockopen('ssl://' . $server, $port, $errno, $error); } if (!$socket) { throw new Exception("Failed to connect, #{$errno}: {$error}"); } $ok = stream_set_blocking($socket, false); if (!$ok) { throw new Exception("Failed to set stream nonblocking."); } $this->socket = $socket; $this->writeCommand('USER', "{$user} 0 * :{$user}"); if ($pass) { $this->writeCommand('PASS', "{$pass}"); } if ($nickpass) { $this->writeCommand("NickServ IDENTIFY ", "{$nickpass}"); } $this->writeCommand('NICK', "{$nick}"); $this->runSelectLoop(); }
function read($file) { if ($file == '-') { return file_get_contents('php://stdin'); } else { return Filesystem::readFile($file); } }
public static function getDeviceID() { $device_id_path = self::getKeyPath('device.id'); if (Filesystem::pathExists($device_id_path)) { return trim(Filesystem::readFile($device_id_path)); } return null; }
public function testValidJSON() { $parser = new PhutilJSONParser(); $tests = array('{}' => array(), '[]' => array(), '{"foo": "bar"}' => array('foo' => 'bar'), '[1, "foo", true, null]' => array(1, 'foo', true, null), '{"foo": {"bar": "baz"}}' => array('foo' => array('bar' => 'baz')), '{"foo": "bar", "bar": ["baz"]}' => array('foo' => 'bar', 'bar' => array('baz')), '{"foo": "bar", "bar": {"baz": "foo"}}' => array('foo' => 'bar', 'bar' => array('baz' => 'foo')), '{"": ""}' => array('' => ''), '{"test":"\\u00c9v\\u00e9nement"}' => array('test' => "Événement"), '["\\u00c9v\\u00e9nement"]' => array("Événement"), '{"test":"http:\\/\\/foo\\\\zomg"}' => array('test' => 'http://foo\\zomg'), '["http:\\/\\/foo\\\\zomg"]' => array('http://foo\\zomg'), Filesystem::readFile(dirname(__FILE__) . '/json/base64.json') => array('action' => 'candidate.create', 'actionId' => '80653a26cc46357ff79ff83b47e27c3cb7a668bd', 'params' => array('attachments' => array(Filesystem::readFile(dirname(__FILE__) . '/json/base64.data'))))); foreach ($tests as $input => $expect) { $this->assertEqual($expect, $parser->parse($input), pht('Parsing JSON: %s', $input)); } }
public function testDetectCopiedCode() { $root = dirname(__FILE__) . '/diff/'; $parser = new ArcanistDiffParser(); $diff = DifferentialDiff::newFromRawChanges($parser->parseDiff(Filesystem::readFile($root . 'lint_engine.diff'))); $copies = idx(head($diff->getChangesets())->getMetadata(), 'copy:lines'); $this->assertEqual(array_combine(range(237, 252), range(167, 182)), ipull($copies, 1)); }
public function testPHPFragmentLexer() { $dir = dirname(__FILE__) . '/php'; foreach (Filesystem::listDirectory($dir, $hidden = false) as $file) { $data = Filesystem::readFile($dir . '/' . $file); $this->runLexer($file, $data); } }
private function lintComposerJson($path) { $composer_hash = md5(Filesystem::readFile(dirname($path) . '/composer.json')); $composer_lock = phutil_json_decode(Filesystem::readFile(dirname($path) . '/composer.lock')); if ($composer_hash !== $composer_lock['hash']) { $this->raiseLintAtPath(self::LINT_OUT_OF_DATE, pht("The '%s' file seems to be out-of-date. " . "You probably need to run `%s`.", 'composer.lock', 'composer update')); } }
public final function getPID() { $pid = null; if (Filesystem::pathExists($this->getPIDPath())) { $pid = (int) Filesystem::readFile($this->getPIDPath()); } return $pid; }
private function getPatchPath($file) { $root = dirname(phutil_get_library_root('phabricator')); $path = $root . '/resources/sql/patches/' . $file; // Make sure it exists. Filesystem::readFile($path); return $path; }
public function readFile($path) { $target = new TempFile(); $future = $this->getExecFuture($path); $future->write(csprintf('get %s %s', $path, $target)); $future->resolvex(); return Filesystem::readFile($target); }
private function checkLog($expect, $format, $data) { $tmp = new TempFile(); $log = new PhutilDeferredLog($tmp, $format); $log->setData($data); unset($log); $this->assertEqual($expect, Filesystem::readFile($tmp), $format); }
public function loadBuiltinFileData() { $name = $this->getName(); $available = $this->getAllBuiltinFiles(); if (empty($available[$name])) { throw new Exception(pht('Builtin "%s" does not exist!', $name)); } return Filesystem::readFile($available[$name]); }
public function addFile($path) { $data = Filesystem::readFile($path); if (strncmp($data, "<?php\n", 6)) { throw new Exception(pht("Expected file '%s' to begin `%s`.", $path, '<?php\\n')); } $this->parts[] = substr($data, 6); return $this; }
public function addFile($path) { $data = Filesystem::readFile($path); if (strncmp($data, "<?php\n", 6)) { throw new Exception("Expected file '{$path}' to begin \"<?php\\n\"."); } $this->parts[] = substr($data, 6); return $this; }
public function parseTestResults($path) { $results = array(); foreach (glob($path . "/*.xml") as $filename) { $parser = new ArcanistXUnitTestResultParser(); $results[] = $parser->parseTestResults(Filesystem::readFile($filename)); } return array_mergev($results); }
public function testShellLexer() { $dir = dirname(__FILE__) . '/shell'; foreach (Filesystem::listDirectory($dir, $hidden = false) as $file) { $data = Filesystem::readFile($dir . '/' . $file); $data = rtrim($data, "\n"); $this->runLexer($file, $data); } }
private function emitError() { $temporary_log = new TempFile(); $old_log = ini_get('error_log'); ini_set('error_log', (string) $temporary_log); trigger_error(pht('(A synthetic error emitted during a unit test.)')); ini_set('error_log', $old_log); return Filesystem::readFile($temporary_log); }
private function loadSource(PhutilSprite $sprite) { $file = $sprite->getSourceFile(); if (empty($this->sources[$file])) { $data = Filesystem::readFile($file); $this->sources[$file] = imagecreatefromstring($data); } return $this->sources[$file]; }