[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/spikephpcoverage/src/cli/ -> instrument.php (source)

   1  <?php
   2      /*
   3      *  $Id: instrument.php 14672 2005-03-23 21:37:47Z npac $
   4      *  
   5      *  Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
   6      *  Licensed under the Open Source License version 2.1
   7      *  (See http://www.spikesource.com/license.html)
   8      */
   9  ?>
  10  <?php
  11      #!/bin/php
  12  
  13      if(!defined("__PHPCOVERAGE_HOME")) {
  14          define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
  15      }
  16      require_once  __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php";
  17      require_once  __PHPCOVERAGE_HOME . "/util/Utility.php";
  18  
  19      ## Instruments the PHP Source files
  20  
  21      /** 
  22       * Print help message and exit
  23       * 
  24       * @access public
  25       */
  26      function help() {
  27          echo "Usage: " . basename(__FILE__) . " -b <application-base-path> [-p <phpcoverage-home>] [-r] [-u] [-e <exclude-file-list>]"
  28           . "[-v] [-h] [path1 [path2 [...]]]\n";
  29          echo "\n";
  30          echo "   Options: \n";
  31          echo "       -b <application-base-path>       Application directory accessible via HTTP "
  32              . "where PHPCoverage files should be copied.\n";
  33          echo "       -p <phpcoverage-home>            Path to PHPCoverage Home.\n";
  34          echo "       -r                               Recursively instrument PHP files.\n";
  35          echo "       -u                               Undo instrumentation.\n";
  36          echo "       -e <file1,file2,...>             Execlude files in the file list.\n";
  37          echo "       -v                               Be verbose.\n";
  38          echo "       -h                               Print this help and exit.\n";
  39          echo "\n";
  40          exit(0);
  41      }
  42  
  43      /** 
  44       * Print error message and exit 
  45       * 
  46       * @param $msg Message to write to console.
  47       * @access public
  48       */
  49      function error($msg) {
  50          echo basename(__FILE__) . ": [ERROR] " . $msg . "\n";
  51          exit(1);
  52      }
  53  
  54      /** 
  55       * Write a information message 
  56       * 
  57       * @param $msg Message to write to console.
  58       * @access public
  59       */
  60      function writeMsg($msg) {
  61          global $VERBOSE;
  62          if($VERBOSE) {
  63              echo basename(__FILE__) . ": [INFO] " . $msg . "\n";
  64          }
  65      }
  66  
  67      /** 
  68       * Instrument the PHP file. 
  69       * 
  70       * @param $file File path
  71       * @access public
  72       */
  73      function instrument($file) {
  74          global $LOCAL_PHPCOVERAGE_LOCATION, $top, $bottom;
  75          $tmpfile = "$file.tmp";
  76          $contents = file_get_contents($file);
  77          $len = strlen($contents);
  78          if(strpos($contents, $top) === 0 && strrpos($contents, $bottom) === ($len - strlen($bottom)-1)) {
  79              writeMsg("Skipping $file.");
  80              return;
  81          }
  82  
  83          $fp = fopen($tmpfile, "w");
  84          if(!$fp) {
  85              error("Cannot write to file: $tmpfile");
  86          }
  87          fputs($fp, $top);
  88          fwrite($fp, $contents);
  89          fputs($fp, $bottom);
  90          fclose($fp);
  91          // Delete if already exists - 'rename()' on Windows will return false otherwise
  92          if(file_exists($file)) {
  93              unlink($file);
  94          }
  95          $ret = rename($tmpfile, $file);
  96          if(!$ret) {
  97              error("Cannot save file: $file");
  98          }
  99          writeMsg("Instrumented: $file.");
 100      }
 101  
 102      /** 
 103       * Uninstrument the PHP file 
 104       * 
 105       * @param $file File path
 106       * @access public
 107       */
 108      function uninstrument($file) {
 109          global $LOCAL_PHPCOVERAGE_LOCATION, $top, $bottom;
 110          $tmpfile = "$file.tmp";
 111  
 112          $contents = file_get_contents($file);
 113          $len = strlen($contents);
 114          if(strpos($contents, $top) !== 0 && strrpos($contents, $bottom) !== ($len - strlen($bottom)-1)) {
 115              writeMsg("Skipping $file.");
 116              return;
 117          }
 118  
 119          $fr = fopen($file, "r");
 120          $fw = fopen($tmpfile, "w");
 121          if(!$fr) {
 122              error("Cannot read file: $file");
 123          }
 124          if(!$fr) {
 125              error("Cannot write to file: $tmpfile");
 126          }
 127          while(!feof($fr)) {
 128              $line = fgets($fr);
 129              if(strpos($line, $top) === false && strpos($line, $bottom) === false) {
 130                  fputs($fw, $line);
 131              }
 132          }
 133          fclose($fr);
 134          fclose($fw);
 135  
 136          // Delete if already exists - 'rename()' on Windows will return false otherwise
 137          if(file_exists($file)) {
 138              unlink($file);
 139          }
 140          $ret = rename($tmpfile, $file);
 141          if(!$ret) {
 142              error("Cannot save file: $file");
 143          }
 144          writeMsg("Uninstrumented: $file");
 145      }
 146  
 147      /** 
 148       * Retrive a list of all PHP files in the given directory
 149       * 
 150       * @param $dir Directory to scan
 151       * @param $recursive True is directory is scanned recursively
 152       * @return Array List of PHP files
 153       * @access public
 154       */
 155      function get_all_php_files($dir, &$excludeFiles, $recursive) {
 156          global $spc_config;
 157          $phpExtensions = $spc_config["extensions"];
 158          $dirs[] = $dir;
 159          while(count($dirs) > 0) {
 160              $currDir = realpath(array_pop($dirs));
 161              if(!is_readable($currDir)) {
 162                  continue;
 163              }
 164              $currFiles = scandir($currDir);
 165              for($j = 0; $j < count($currFiles); $j++) {
 166                  if($currFiles[$j] == "." || $currFiles[$j] == "..") {
 167                      continue;
 168                  }
 169                  $currFiles[$j] = $currDir . "/" . $currFiles[$j];
 170                  if(is_file($currFiles[$j])) {
 171                      $pathParts = pathinfo($currFiles[$j]);
 172                      // Ignore phpcoverage bottom and top stubs
 173                      if(strpos($pathParts['basename'], "phpcoverage.remote.") !== false) {
 174                          continue;
 175                      }
 176                      // Ignore files specified in the exclude list
 177                      if(in_array(realpath($currFiles[$j]), $excludeFiles) !== false) {
 178                          continue;
 179                      }
 180                      if(isset($pathParts['extension'])
 181                          && in_array($pathParts['extension'], $phpExtensions)) {
 182                          $files[] = $currFiles[$j];
 183                      }
 184                  }
 185                  else if(is_dir($currFiles[$j]) && $recursive) {
 186                      $dirs[] = $currFiles[$j];
 187                  }
 188              }
 189          }
 190          return $files;
 191      }
 192  
 193      // Initialize
 194  
 195      $RECURSIVE = false;
 196      $UNDO = false;
 197  
 198      $top_file = "/phpcoverage.remote.top.inc.php";
 199      $bottom_file = "/phpcoverage.remote.bottom.inc.php";
 200  
 201      //print_r($argv);
 202      for($i = 1; $i < $argc; $i++) {
 203          switch($argv[$i]) {
 204          case "-r":
 205              $RECURSIVE = true;
 206              break;
 207  
 208          case "-p":
 209              $PHPCOVERAGE_HOME = $argv[++$i];
 210              break;
 211  
 212          case "-b":
 213              $LOCAL_PHPCOVERAGE_LOCATION = $argv[++$i];
 214              break;
 215  
 216          case "-u":
 217              $UNDO = true;
 218              break;
 219  
 220          case "-e":
 221              $EXCLUDE_FILES = explode(",", $argv[++$i]);
 222              break;
 223  
 224          case "-v":
 225              $VERBOSE = true;
 226              break;
 227  
 228          case "-h":
 229              help();
 230              break;
 231  
 232          default:
 233              $paths[] = $argv[$i];
 234              break;
 235          }
 236      }
 237  
 238  
 239      if(!is_dir($LOCAL_PHPCOVERAGE_LOCATION)) {
 240          error("LOCAL_PHPCOVERAGE_LOCATION [$OCAL_PHPCOVERAGE_LOCATION] not found.");
 241      }
 242      if(empty($PHPCOVERAGE_HOME) || !is_dir($PHPCOVERAGE_HOME)) {
 243          $PHPCOVERAGE_HOME = __PHPCOVERAGE_HOME;
 244          if(empty($PHPCOVERAGE_HOME) || !is_dir($PHPCOVERAGE_HOME)) {
 245              error("PHPCOVERAGE_HOME does not exist. [" . $PHPCOVERAGE_HOME . "]");
 246          }
 247      }
 248  
 249      $LOCAL_PHPCOVERAGE_LOCATION = realpath($LOCAL_PHPCOVERAGE_LOCATION);
 250      if(file_exists($LOCAL_PHPCOVERAGE_LOCATION . $top_file)) {
 251          unlink($LOCAL_PHPCOVERAGE_LOCATION . $top_file);
 252      }
 253      $ret = copy($PHPCOVERAGE_HOME . $top_file, $LOCAL_PHPCOVERAGE_LOCATION . $top_file);
 254      if(!$ret) {
 255          error("Cannot copy to $LOCAL_PHPCOVERAGE_LOCATION");
 256      }
 257      if(file_exists($LOCAL_PHPCOVERAGE_LOCATION . $bottom_file)) {
 258          unlink($LOCAL_PHPCOVERAGE_LOCATION . $bottom_file);
 259      }
 260      $ret = copy($PHPCOVERAGE_HOME . $bottom_file, $LOCAL_PHPCOVERAGE_LOCATION . $bottom_file);
 261      if(!$ret) {
 262          error("Cannot copy to $LOCAL_PHPCOVERAGE_LOCATION");
 263      }
 264      $top="<?php require_once \"" . $LOCAL_PHPCOVERAGE_LOCATION . $top_file ."\"; ?>\n";
 265      $bottom="<?php require \"" . $LOCAL_PHPCOVERAGE_LOCATION . $bottom_file . "\"; ?>\n";
 266  
 267      if(empty($paths)) {
 268          $paths[] = getcwd();
 269      }
 270      if(!isset($EXCLUDE_FILES) || empty($EXCLUDE_FILES)) {
 271          $EXCLUDE_FILES = array();
 272      }
 273      for($i = 0; $i < count($EXCLUDE_FILES); $i++) {
 274          // Remove a file from the array if it does not exist
 275          if(!file_exists($EXCLUDE_FILES[$i])) {
 276              array_splice($EXCLUDE_FILES, $i, 1);
 277              $i --;
 278              continue;
 279          } 
 280          $EXCLUDE_FILES[$i] = realpath($EXCLUDE_FILES[$i]);
 281      }
 282  
 283      //print_r($paths);
 284      foreach($paths as $path) {
 285          unset($files);
 286          if(is_dir($path)) {
 287              $files = get_all_php_files($path, $EXCLUDE_FILES, $RECURSIVE);
 288          }
 289          else if(is_file($path)) {
 290              $files[] = $path;
 291          }
 292          else {
 293              error("Unknown entity: $path");
 294          }
 295          //print_r($files);
 296          foreach($files as $file) {
 297              if($UNDO) {
 298                  uninstrument($file);
 299              }
 300              else {
 301                  instrument($file);
 302              }
 303          }
 304      }
 305  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7