Menu

[r26]: / branches / phpcoverage-0.8.2 / src / cli / instrument.php  Maximize  Restore  History

Download this file

306 lines (277 with data), 9.5 kB

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