PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/tools/update_expected_slow_output.php

http://github.com/facebook/hiphop-php
PHP | 85 lines | 77 code | 5 blank | 3 comment | 1 complexity | ec510174b2f48b3b1560eee62825b973 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. #!/bin/env php
  2. <?hh
  3. $php = '/home/engshare/externals/cpp/hphp/centos-dev/php/bin/php';
  4. // $php = '/home/ptarjan/bin/php-5.4';
  5. $config = json_decode(file_get_contents(__DIR__.'/update_expected_slow_output.json'), true);
  6. if (!$config) {
  7. die("Invalid config file. Not JSON.\n");
  8. }
  9. function ends_with($big, $little) {
  10. return strpos($big, $little, strlen($big) - strlen($little)) !== false;
  11. }
  12. // More efficient lookups than array_search
  13. foreach ($config as $key => $value) {
  14. $config[$key] = array_fill_keys($value, true);
  15. }
  16. function is_valid_diff($wanted_re, $output) {
  17. $wanted_re = trim($wanted_re);
  18. $output = trim($output);
  19. $wanted_re = preg_quote($wanted_re, '/');
  20. $wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
  21. $wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
  22. $wanted_re = str_replace('%S', '[^\r\n]*', $wanted_re);
  23. $wanted_re = str_replace('%a', '.+', $wanted_re);
  24. $wanted_re = str_replace('%A', '.*', $wanted_re);
  25. $wanted_re = str_replace('%w', '\s*', $wanted_re);
  26. $wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
  27. $wanted_re = str_replace('%d', '\d+', $wanted_re);
  28. $wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
  29. $wanted_re = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $wanted_re);
  30. $wanted_re = str_replace('%c', '.', $wanted_re);
  31. return preg_match("/^$wanted_re\$/sm", $output);
  32. };
  33. print "Running all test/slow through $php...\n";
  34. foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ('test/slow')) as $f) {
  35. $filename = $f->getRealPath();
  36. $name = str_replace('.php', '', $f->getFilename());
  37. if (!$f->isFile() ||
  38. !ends_with($filename, '.php') ||
  39. isset($config['hard_coded'][$name])) {
  40. continue;
  41. }
  42. $opts = '-dapc.enable_cli=1 -ddisplay_errors=off';
  43. $output = shell_exec("$php $opts $filename 2>/dev/null");
  44. $expect = "$filename.expect";
  45. if (is_file($expect)) {
  46. file_put_contents($expect, $output);
  47. } else if (is_file($expect.'f')) {
  48. $wanted_re = file_get_contents($expect.'f');
  49. if (isset($config['no_warnings'][$name])) {
  50. $wanted_re = preg_replace('/^HipHop .*/m', '', $wanted_re);
  51. }
  52. $output = str_replace(realpath(__DIR__.'../../..'), '%s', $output);
  53. if (is_valid_diff($wanted_re, $output)) {
  54. continue;
  55. }
  56. $file = tempnam('/tmp', 'zend-');
  57. file_put_contents($file, $output);
  58. $diff = shell_exec("diff --text -u {$expect}f $file");
  59. echo "\n$diff\n";
  60. do {
  61. print "Output differs, use Zend? [y/N]? ";
  62. $prompt = strtolower(trim(fgets(STDIN)));
  63. if ($prompt == 'y') {
  64. file_put_contents($expect.'f', $output);
  65. break;
  66. } else if (!$prompt || $prompt == 'n') {
  67. break;
  68. }
  69. } while (true);
  70. } else {
  71. die("No $file.expect or $file.expectf. WTF?\n");
  72. }
  73. }