|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * $Id: instrument.php,v 1.2 2010/12/14 17:35:49 moodlerobot Exp $ 00004 * 00005 * Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved. 00006 * Licensed under the Open Software License version 2.1 00007 * (See http://www.spikesource.com/license.html) 00008 */ 00009 ?> 00010 <?php 00011 #!/bin/php 00012 00013 if(!defined("__PHPCOVERAGE_HOME")) { 00014 define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__))); 00015 } 00016 require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php"; 00017 require_once __PHPCOVERAGE_HOME . "/util/Utility.php"; 00018 00019 ## Instruments the PHP Source files 00020 00026 function help() { 00027 echo "Usage: " . basename(__FILE__) . " -b <application-base-path> [-p <phpcoverage-home>] [-r] [-u] [-e <exclude-file-list>]" 00028 . "[-v] [-h] [path1 [path2 [...]]]\n"; 00029 echo "\n"; 00030 echo " Options: \n"; 00031 echo " -b <application-base-path> Application directory accessible via HTTP " 00032 . "where PHPCoverage files should be copied.\n"; 00033 echo " -p <phpcoverage-home> Path to PHPCoverage Home.\n"; 00034 echo " -r Recursively instrument PHP files.\n"; 00035 echo " -u Undo instrumentation.\n"; 00036 echo " -e <file1,file2,...> Execlude files in the file list.\n"; 00037 echo " -v Be verbose.\n"; 00038 echo " -h Print this help and exit.\n"; 00039 echo "\n"; 00040 exit(0); 00041 } 00042 00049 function error($msg) { 00050 echo basename(__FILE__) . ": [ERROR] " . $msg . "\n"; 00051 exit(1); 00052 } 00053 00060 function writeMsg($msg) { 00061 global $VERBOSE; 00062 if($VERBOSE) { 00063 echo basename(__FILE__) . ": [INFO] " . $msg . "\n"; 00064 } 00065 } 00066 00073 function instrument($file) { 00074 global $LOCAL_PHPCOVERAGE_LOCATION, $top, $bottom; 00075 $tmpfile = "$file.tmp"; 00076 $contents = file_get_contents($file); 00077 $len = strlen($contents); 00078 if(strpos($contents, $top) === 0 && strrpos($contents, $bottom) === ($len - strlen($bottom))) { 00079 writeMsg("Skipping $file."); 00080 return; 00081 } 00082 00083 $fp = fopen($tmpfile, "w"); 00084 if(!$fp) { 00085 error("Cannot write to file: $tmpfile"); 00086 } 00087 fputs($fp, $top); 00088 fwrite($fp, $contents); 00089 fputs($fp, $bottom); 00090 fclose($fp); 00091 // Delete if already exists - 'rename()' on Windows will return false otherwise 00092 if(file_exists($file)) { 00093 unlink($file); 00094 } 00095 $ret = rename($tmpfile, $file); 00096 if(!$ret) { 00097 error("Cannot save file: $file"); 00098 } 00099 writeMsg("Instrumented: $file."); 00100 } 00101 00108 function uninstrument($file) { 00109 global $LOCAL_PHPCOVERAGE_LOCATION, $top, $bottom; 00110 $tmpfile = "$file.tmp"; 00111 00112 $contents = file_get_contents($file); 00113 $len = strlen($contents); 00114 if(strpos($contents, $top) !== 0 && strrpos($contents, $bottom) !== ($len - strlen($bottom))) { 00115 writeMsg("Skipping $file."); 00116 return; 00117 } 00118 00119 $fr = fopen($file, "r"); 00120 $fw = fopen($tmpfile, "w"); 00121 if(!$fr) { 00122 error("Cannot read file: $file"); 00123 } 00124 if(!$fr) { 00125 error("Cannot write to file: $tmpfile"); 00126 } 00127 while(!feof($fr)) { 00128 $line = fgets($fr); 00129 if(strpos($line, $top) === false && strpos($line, $bottom) === false) { 00130 fputs($fw, $line); 00131 } 00132 } 00133 fclose($fr); 00134 fclose($fw); 00135 00136 // Delete if already exists - 'rename()' on Windows will return false otherwise 00137 if(file_exists($file)) { 00138 unlink($file); 00139 } 00140 $ret = rename($tmpfile, $file); 00141 if(!$ret) { 00142 error("Cannot save file: $file"); 00143 } 00144 writeMsg("Uninstrumented: $file"); 00145 } 00146 00155 function get_all_php_files($dir, &$excludeFiles, $recursive) { 00156 global $spc_config; 00157 $phpExtensions = $spc_config["extensions"]; 00158 $dirs[] = $dir; 00159 while(count($dirs) > 0) { 00160 $currDir = realpath(array_pop($dirs)); 00161 if(!is_readable($currDir)) { 00162 continue; 00163 } 00164 $currFiles = scandir($currDir); 00165 for($j = 0; $j < count($currFiles); $j++) { 00166 if($currFiles[$j] == "." || $currFiles[$j] == "..") { 00167 continue; 00168 } 00169 $currFiles[$j] = $currDir . "/" . $currFiles[$j]; 00170 if(is_file($currFiles[$j])) { 00171 $pathParts = pathinfo($currFiles[$j]); 00172 // Ignore phpcoverage bottom and top stubs 00173 if(strpos($pathParts['basename'], "phpcoverage.remote.") !== false) { 00174 continue; 00175 } 00176 // Ignore files specified in the exclude list 00177 if(in_array(realpath($currFiles[$j]), $excludeFiles) !== false) { 00178 continue; 00179 } 00180 if(isset($pathParts['extension']) 00181 && in_array($pathParts['extension'], $phpExtensions)) { 00182 $files[] = $currFiles[$j]; 00183 } 00184 } 00185 else if(is_dir($currFiles[$j]) && $recursive) { 00186 $dirs[] = $currFiles[$j]; 00187 } 00188 } 00189 } 00190 return $files; 00191 } 00192 00193 // Initialize 00194 00195 $RECURSIVE = false; 00196 $UNDO = false; 00197 00198 $top_file = "/phpcoverage.remote.top.inc.php"; 00199 $bottom_file = "/phpcoverage.remote.bottom.inc.php"; 00200 00201 //print_r($argv); 00202 for($i = 1; $i < $argc; $i++) { 00203 switch($argv[$i]) { 00204 case "-r": 00205 $RECURSIVE = true; 00206 break; 00207 00208 case "-p": 00209 $PHPCOVERAGE_HOME = $argv[++$i]; 00210 break; 00211 00212 case "-b": 00213 $LOCAL_PHPCOVERAGE_LOCATION = $argv[++$i]; 00214 break; 00215 00216 case "-u": 00217 $UNDO = true; 00218 break; 00219 00220 case "-e": 00221 $EXCLUDE_FILES = explode(",", $argv[++$i]); 00222 break; 00223 00224 case "-v": 00225 $VERBOSE = true; 00226 break; 00227 00228 case "-h": 00229 help(); 00230 break; 00231 00232 default: 00233 $paths[] = $argv[$i]; 00234 break; 00235 } 00236 } 00237 00238 00239 if(!is_dir($LOCAL_PHPCOVERAGE_LOCATION)) { 00240 error("LOCAL_PHPCOVERAGE_LOCATION [$LOCAL_PHPCOVERAGE_LOCATION] not found."); 00241 } 00242 if(empty($PHPCOVERAGE_HOME) || !is_dir($PHPCOVERAGE_HOME)) { 00243 $PHPCOVERAGE_HOME = __PHPCOVERAGE_HOME; 00244 if(empty($PHPCOVERAGE_HOME) || !is_dir($PHPCOVERAGE_HOME)) { 00245 error("PHPCOVERAGE_HOME does not exist. [" . $PHPCOVERAGE_HOME . "]"); 00246 } 00247 } 00248 00249 $LOCAL_PHPCOVERAGE_LOCATION = realpath($LOCAL_PHPCOVERAGE_LOCATION); 00250 if(file_exists($LOCAL_PHPCOVERAGE_LOCATION . $top_file)) { 00251 unlink($LOCAL_PHPCOVERAGE_LOCATION . $top_file); 00252 } 00253 $ret = copy($PHPCOVERAGE_HOME . $top_file, $LOCAL_PHPCOVERAGE_LOCATION . $top_file); 00254 if(!$ret) { 00255 error("Cannot copy to $LOCAL_PHPCOVERAGE_LOCATION"); 00256 } 00257 if(file_exists($LOCAL_PHPCOVERAGE_LOCATION . $bottom_file)) { 00258 unlink($LOCAL_PHPCOVERAGE_LOCATION . $bottom_file); 00259 } 00260 $ret = copy($PHPCOVERAGE_HOME . $bottom_file, $LOCAL_PHPCOVERAGE_LOCATION . $bottom_file); 00261 if(!$ret) { 00262 error("Cannot copy to $LOCAL_PHPCOVERAGE_LOCATION"); 00263 } 00264 $top="<?php require_once \"" . $LOCAL_PHPCOVERAGE_LOCATION . $top_file ."\"; ?>\n"; 00265 $bottom="<?php require \"" . $LOCAL_PHPCOVERAGE_LOCATION . $bottom_file . "\"; ?>\n"; 00266 00267 if(empty($paths)) { 00268 $paths[] = getcwd(); 00269 } 00270 if(!isset($EXCLUDE_FILES) || empty($EXCLUDE_FILES)) { 00271 $EXCLUDE_FILES = array(); 00272 } 00273 for($i = 0; $i < count($EXCLUDE_FILES); $i++) { 00274 // Remove a file from the array if it does not exist 00275 if(!file_exists($EXCLUDE_FILES[$i])) { 00276 array_splice($EXCLUDE_FILES, $i, 1); 00277 $i --; 00278 continue; 00279 } 00280 $EXCLUDE_FILES[$i] = realpath($EXCLUDE_FILES[$i]); 00281 } 00282 00283 //print_r($paths); 00284 foreach($paths as $path) { 00285 unset($files); 00286 if(is_dir($path)) { 00287 $files = get_all_php_files($path, $EXCLUDE_FILES, $RECURSIVE); 00288 } 00289 else if(is_file($path)) { 00290 $files[] = $path; 00291 } 00292 else { 00293 error("Unknown entity: $path"); 00294 } 00295 //print_r($files); 00296 foreach($files as $file) { 00297 if($UNDO) { 00298 uninstrument($file); 00299 } 00300 else { 00301 instrument($file); 00302 } 00303 } 00304 } 00305 ?>