Repository navigation
Expand file tree
/
Copy pathdiff.class.php
More file actions
551 lines (507 loc) · 20.5 KB
/
Copy pathdiff.class.php
File metadata and controls
551 lines (507 loc) · 20.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
header("constent-type:text/html;charset=utf-8");
require_once dirname(__FILE__) . "/File.class.php";
require_once dirname(__FILE__) . "/inline_diff/inline_function.php";
/**
* 对不同的版本编译的结果进行diff
*/
class Diff{
/**
* @var 版本编译对象
*/
// protected $fisbuild_new;
// protected $fisbuild_old;
/**
* @var 文件依赖分析工具
*/
// protected $analyze;
/**
* @var $config 配置信息
*/
private static $config = array();
/**
* @var string
*/
protected $Diff = array();
// protected $requireDiff = array();
/**
* @var string
*/
protected $proInfo = array();
// protected $loginfo =array();
//
// protected $buildtimeinfo =array();
protected static $rootPath;
public function __construct($productName){
self::$rootPath = File::normalize(dirname(__FILE__));
// FILE::delete(self::$rootPath."/".$productName);
self::loadConfig(self::$rootPath.'/config'.$_SESSION['o'].'.php');
$product = self::getConfig('product');
$this->proInfo = $product[$productName];
// $this->analyze = new Analyze();
}
// protected function getProduct(){
// $productsvn = $this->proInfo['svn'];
// $svncmd = "svn co --username=mawenlin --password= --no-auth-cache $productsvn ".$this->proInfo['dir']." 2>&1";
// exec($svncmd,$info,$ret);
// if($ret != 0){
// echo "svn get code error! exist!"."\n";
// var_dump($info);
// exit;
// }
// }
/**
* 用新老版本的FIS对产品线的代码进行编译。
*/
// public function build(){
// $new = self::getConfig('new');
// $old = self::getConfig('old');
// $productmodules = $this->proInfo['modules'];
//
//// $this->getProduct();
// //使用新版本的fis进行编译
// $cmd_buildnew="php -f build.class.php ".$new['dir']." ".
// $this->proInfo['newoutputdir']." ".
// $this->proInfo['dir']." ".
// $productmodules." ".
// $this->proInfo['result']." ".
// $new['fisname']." 2>&1";
//
//
// //使用老版本的fis进行编译
// $cmd_buildold="php -f build.class.php ".$old['dir']." ".
// $this->proInfo['oldoutputdir']." ".
// $this->proInfo['dir']." ".
// $productmodules." ".
// $this->proInfo['result']." ".
// $old['fisname']." 2>&1";
//
// var_dump($cmd_buildnew);
// var_dump($cmd_buildold);
// exec($cmd_buildnew,$outputnew,$retnew);
// if($retnew){
// var_dump($outputnew);
// exit;
// }
// exec($cmd_buildold,$outputold,$retold);
// if($retold){
// var_dump($outputold);
// exit;
// }
// reset_product_svn($this->proInfo['dir']);
// }
/**
* 调用文件依赖分析工具,获取分析结果。
*/
// protected function analyzeEach($fisname,$outputDir,$resultDir){
// $this->analyze->init($outputDir,ANALYZE_ROOT_PATH);
//
// $this->analyze->analyzeFile();
// //检查文件依赖关系是否正确
// $this->analyze->lookForDefine();
//
// $defines = "<?php $".$fisname."_define=".var_export(Info::getDefines(),TRUE).";";
// $requires = "<?php $".$fisname."_require=".var_export(Info::getRequires(),TRUE).";";
// $log = "<?php $".$fisname."_log=".var_export(Info::getLogInfo(),TRUE).";";
//
// File::write($resultDir."/"."$fisname"."_define.php",$defines);
// File::write($resultDir."/"."$fisname"."_require.php",$requires);
// File::write($resultDir."/"."$fisname"."_log.php",$log);
// }
/**
* 分析新老版本的文件依赖数据。
*/
// public function analyze(){
// $new = self::getConfig('new');
// $old = self::getConfig('old');
// /** 分别分析两个编译后的output文件夹 */
// $this->analyzeEach($new['fisname'],$this->proInfo['newoutputdir'],$this->proInfo['result']);
// $this->analyzeEach($old['fisname'],$this->proInfo['oldoutputdir'],$this->proInfo['result']);
// }
/**
* 获取define数据不同的地方
*/
// public function getDefineDiff(){
// $fisname_new = self::getConfig('new','fisname');
// $fisname_old = self::getConfig('old','fisname');
// $resultDir = $this->proInfo['result'];
// require_once $resultDir."/".$fisname_new."_define.php";
// require_once $resultDir."/".$fisname_old."_define.php";
//
// /** 先比较两个define中文件tpl文件不同的地方 */
// foreach(${$fisname_new."_define"}['tpl'] as $key1 => $value1){
// foreach(${$fisname_old."_define"}['tpl'] as $key2 => $value2){
// if($key1 == $key2 && $value1 == $value2){
// unset(${$fisname_old."_define"}['tpl'][$key2]);
// unset(${$fisname_new."_define"}['tpl'][$key1]);
// }else if($key1 != $key2 && $value1 == $value2){
// $diff_data = array(
// 'name' => str_replace("/","_",$value1),
// 'new' => $value1,
// 'old' => $value1,
// 'ext' => 'tpl',
// );
// array_push($this->Diff,$diff_data);
// unset(${$fisname_old."_define"}['tpl'][$key2]);
// unset(${$fisname_new."_define"}['tpl'][$key1]);
// }
// }
// }
// if(!empty(${$fisname_new."_define"}['tpl'])){
// foreach(${$fisname_new."_define"}['tpl'] as $value){
// $diff_data = array(
// 'name' => str_replace("/","_",$value),
// 'new' => $value,
// 'old' => $value,
// 'ext' => 'tpl'
// );
// array_push($this->Diff,$diff_data);
// }
// }
// if(!empty(${$fisname_old."_define"}['tpl'])){
// foreach(${$fisname_old."_define"}['tpl'] as $value){
// $diff_data = array(
// 'name' => str_replace("/","_",$value),
// 'new' => $value,
// 'old' => $value,
// 'ext' => 'tpl',
// );
// array_push($this->Diff,$diff_data);
// }
// }
// }
/**
* 根据define diff的结果获取,对应文件在新老版本下require的不同值。
*/
// protected function getRequireDiffFromDifine(){
// $fisname_new = self::getConfig('new','fisname');
// $fisname_old = self::getConfig('old','fisname');
// $resultDir = $this->proInfo['result'];
// require_once $resultDir."/"."$fisname_new"."_require.php";
// require_once $resultDir."/"."$fisname_old"."_require.php";
// $requireDiff=array();
// foreach($this->Diff as $diff){
// $diffs = array();
// foreach(${$fisname_new."_require"} as $class_values){
// $diffnew = File::normalize($this->proInfo['newoutputdir'] .$diff['new']);
// //先获取md5值不一样的文件名,然后根据这个文件名去require的数组中获取对应的key值。
// $keys = array_keys($class_values, $diffnew);
// if(!empty($keys)){
// $diffs['new']=$keys;
// }
// }
// //再从老版本里面拿require的信息
// foreach(${$fisname_old."_require"} as $class_values){
// $diffold = File::normalize($this->proInfo['oldoutputdir'] .$diff['old']);
// //先获取md5值不一样的文件名,然后根据这个文件名去require的数组中获取对应的key值。
// $keys=array_keys($class_values, $diffold);
// if(!empty($keys)){
// $diffs['old']=$keys;
// }
// }
// while(!empty($diffs['new'])){
// $diff=array();
// $new = array_pop($diffs['new']);
// $old = array_pop($diffs['old']);
// $diff['name'] = str_replace("/","_",$new);
// $diff['new'] = $new;
// $diff['old'] = $old;
//
// $newfile = new FILE($this->proInfo['newoutputdir'].$new);
// $diff['ext'] = $newfile->ext;
// $cmd = "diff ".$this->proInfo['newoutputdir'].$new." ".$this->proInfo['oldoutputdir'].$old." 2>&1";
// exec($cmd,$output,$retdiff);
// if($retdiff && $diff['ext']!='tpl' && $diff['ext']!="html"){
// array_push($requireDiff,$diff);
// }
// }
// }
// foreach($requireDiff as $value){
// if(!in_array($this->Diff,$value)){
// array_push($this->Diff,$value);
// }
// }
// return $requireDiff;
// }
/**
* 获取文件依赖分析结果
*/
// public function getLog(){
// $fisname_new = self::getConfig('new','fisname');
// $fisname_old = self::getConfig('old','fisname');
// $resultDir = $this->proInfo['result'];
// include $resultDir."/"."$fisname_new"."_log.php";
// include $resultDir."/"."$fisname_old"."_log.php";
//
// $this->loginfo['new'] = ${$fisname_new."_log"};
// $this->loginfo['old'] = ${$fisname_old."_log"};
// }
/**
* 获取前后编译时间不同
*/
// public function getBuildTime(){
// $fisname_new = self::getConfig('new','fisname');
// $fisname_old = self::getConfig('old','fisname');
// $resultDir = $this->proInfo['result'];
// include $resultDir."/"."$fisname_new"."_buildtime.php";
// include $resultDir."/"."$fisname_old"."_buildtime.php";
//
// $buildtimes['new']=${$fisname_new."_buildtime"};
// $buildtimes['old']=${$fisname_old."_buildtime"};
//
// foreach($buildtimes['new'] as $key => $value){
// $buildtime = array();
// $buildtime['module'] = $key;
// $buildtime['new'] = $value;
// $buildtime['old'] = $buildtimes['old'][$key];
// $buildtime['change']=round(($buildtime['new'] - $buildtime['old'])/$buildtime['old'],4)*100;
// var_dump($buildtime['change']);
// array_push($this->buildtimeinfo,$buildtime);
// }
// }
/**
* 获取文件夹下文件路径
*/
protected function getFiles($path){
if(is_dir($path)){
$dp=dir($path);
while($file=$dp->read())
if($file!='.'&&$file!='..')
$this->getFiles($path.'/'.$file);
$dp->close();
}
else {
array_push($this->proInfo['files'],$path);
}
}
protected function isImg($file_name,$pass_type=array('jpg','jpeg','gif','bmp','png')){
$yx_file = $pass_type;
$kzm = substr(strrchr($file_name,"."),1);
$is_img = in_array(strtolower($kzm),$yx_file);
if($is_img){
return true;
}else{
return false;
}
}
/**
* 展示文件内容不同
*/
public function DisplayDetails(){
$smartyConfig = self::getConfig('smarty');
$outputConfig = self::getConfig('output');
$diffdatas = array();
$this->proInfo['files'] = array();
$this->getFiles($this->proInfo['oldoutputdir']);
foreach($this->proInfo['files'] as $path){
$old = $path;
$new = str_replace($outputConfig['oldoutputdir'],$outputConfig['newoutputdir'],$old);
if(filesize($old) != filesize($new) && !preg_match("/fis_version.txt$/", $old)){
$this->smarty->assign('new',$new);
$this->smarty->assign('old',$old);
$newtext = file_get_contents($new);
$oldtext = file_get_contents($old);
$nl = '#**!)@#';
$diffdata = inline_diff($oldtext, $newtext, $nl);
array_push($diffdatas,$diffdata);
//文件内容diff的结果展示
$this->smarty->assign('diffdata',mb_convert_encoding($diffdata,'utf-8', 'gbk'));
$html = $this->smarty->fetch($smartyConfig['templatedir']."diffdetails.tpl");
$htmlpath = $smartyConfig['templatedir'].$this->proInfo['name']."_".str_replace("/", "_",substr($old,strrpos($old,$outputConfig['oldoutputdir']) + 13)).".html";
$fileurl = self::getConfig('url').str_replace("/home/work/repos/","",$htmlpath);
$diff = array(
'url' => $fileurl,
'name' => str_replace("/", "_",substr($old,strrpos($old,$outputConfig['oldoutputdir']) + 13)),
'old' => substr($old,strrpos($old,$outputConfig['oldoutputdir']) + 13),
'new' => substr($old,strrpos($new,$outputConfig['newoutputdir']) + 13)
);
array_push($this->Diff, $diff);
File::write($htmlpath,$html);
}
}
// $this->generateReport($diffdatas);
}
/**
* 分别展示不同的地方
*/
public function Display(){
// $this->getDefineDiff(); //tpl文件不同的地方
// $this->getRequireDiffFromDifine(); //静态资源不同的地方
// $this->getLog(); //获取文件依赖关系分析信息
// $this->getBuildTime();//获取编译时间的diff
$smartyConfig = self::getConfig('smarty');
require_once $smartyConfig['dir'].'/Smarty.class.php';
$this->smarty = new Smarty();
$this->smarty->setTemplateDir($smartyConfig['templatedir']);
//展示文件内容的不同
$this->DisplayDetails();
// //文件依赖关系分析内容
// //老的
// $this->smarty->assign('difflog',$this->loginfo['old']);
// $html = $this->smarty->fetch($smartyConfig['templatedir']."/analyzeinfo.tpl");
// File::write($smartyConfig['templatedir'].$this->proInfo['name']."_old_log.html",$html);
// $oldlogurl = self::getConfig('url').str_replace("/home/work/repos/","",
// $smartyConfig['templatedir'].$this->proInfo['name']."_old_log.html");
//
// //新的
// $this->smarty->assign('difflog',$this->loginfo['new']);
// $html = $this->smarty->fetch($smartyConfig['templatedir']."/analyzeinfo.tpl");
// File::write($smartyConfig['templatedir'].$this->proInfo['name']."_new_log.html",$html);
// $newlogurl = self::getConfig('url').str_replace("/home/work/repos/","",
// $smartyConfig['templatedir'].$this->proInfo['name']."_new_log.html");
//不同文件的list展示
$this->smarty->assign('newversion',file_get_contents($this->proInfo['newoutputdir']."/fis_version.txt"));
$this->smarty->assign('oldversion',file_get_contents($this->proInfo['oldoutputdir']."/fis_version.txt"));
$this->smarty->assign('productname',$this->proInfo['name']);
$this->smarty->assign('difflist',$this->Diff);
$html = $this->smarty->fetch($smartyConfig['templatedir']."/difflist.tpl");
File::write($smartyConfig['templatedir'].$this->proInfo['name']."_difflist.html",$html);
// $diffData = array(
// $this->proInfo['name'] =>$this->buildtimeinfo
// );
// $this->createDiffDate($diffData);
$this->generateReport($this->Diff);
}
/**
* 从php文件加载config,并合并到当前的配置数据中,加载成功则返回true,否则为false
* @static
* @param string $path php配置文件的路径,该文件内部代码为<?php $config=array(..);?>,配置数据必须是名为$config的变量
* @return bool
*/
public static function loadConfig($path) {
if(is_file($path)){
$config = array();
try {
include $path;
} catch (Exception $e) {
Log::error("配置文件[{$path}]解析失败:" . $e->getMessage());
}
self::merge($config);
return self::$config;
} else {
Log::warning("配置文件[{$path}]不存在.");
}
return false;
}
/**
* 将数据递归合并到当前配置数据上
* @static
* @param mixed $data 要合并的关联数组数据
*/
public static function merge($data){
self::_merge(self::$config, $data);
}
/**
* 递归合并数据函数
* @static
* @param array $source
* @param mixed $data
*/
private static function _merge(&$source, $data){
if(is_array($data)){
foreach($data as $key => $value){
if(array_key_exists($key, $source)){
self::_merge($source[$key], $value);
} else {
$source[$key] = $value;
}
}
} else {
$source = $data;
}
}
/**
* @static
* @param $key
* @param $key1
* @return null 返回配置信息
*/
public static function getConfig($key,$key1=null){
if(!$key){
return null;
}else if($key1){
return self::$config[$key][$key1];
}else{
return self::$config[$key];
}
}
/*
*生成测试报告
*
*/
private function generateReport($diffdatas){
$smartyConfig = self::getConfig('smarty');
$dom = new DOMDocument("1.0","utf-8");
$xmlFile = $smartyConfig['templatedir']."report.xml";
$tatalCount = 0;
$tatalFailure = 0;
if(file_exists($xmlFile)){
$dom->load($xmlFile);
$testsuite = $dom->getElementsByTagName("testsuite")->item(0);
$tatalCount = $testsuite->getAttribute("tests");
$tatalFailure = $testsuite->getAttribute("failures");
}else{
$testsuites = $dom->createElement("testsuites");
$dom->appendChild($testsuites);
$testsuite = $dom->createElement("testsuite");
$testsuites->appendChild($testsuite);
}
if(is_array($diffdatas) && count($diffdatas)>0){
$count = count($diffdatas);
$tatalCount += $count;
$tatalFailure += $count;
$testsuite->setAttribute("name","diff* ");
$testsuite->setAttribute("tests",$tatalCount);
$testsuite->setAttribute("time",$tatalCount);
$testsuite->setAttribute("failures",$tatalFailure);
$testsuite->setAttribute("total",$tatalCount);
foreach($diffdatas as $diffdata){
$testcase=$dom->createElement("testcase");
$testsuite->appendChild($testcase);
$testcase->setAttribute("name",$diffdata["name"]);
$testcase->setAttribute("time","1");
$testcase->setAttribute("failures","1");
$testcase->setAttribute("total","1");
$failure = $dom->createElement("failure");
$testcase->appendChild($failure);
$failure->setAttribute("type","junit.framework.AssertionFailedError");
$msgText = $dom->createTextNode("diff");
$failure->appendChild($msgText);
}
}else if(!file_exists($xmlFile)){
$testsuite->setAttribute("name","diff* ");
$testsuite->setAttribute("tests",1);
$testsuite->setAttribute("time",1);
$testsuite->setAttribute("failures",0);
$testsuite->setAttribute("total",1);
$testcase=$dom->createElement("testcase");
$testsuite->appendChild($testcase);
$testcase->setAttribute("name","testcase0");
$testcase->setAttribute("time","1");
$testcase->setAttribute("failures","0");
$testcase->setAttribute("total","1");
}
$dom->save($xmlFile);
}
/*
* 生成diff文件,提供给report工具使用
* */
private function createDiffDate($diffdatas){
$smartyConfig = self::getConfig('smarty');
$saveFile = $smartyConfig['templatedir']."diffDate.php";
$fileData = array();
if(file_exists($saveFile)){
$fileData = include $saveFile;
}
//$res = array_diff($fileData,$diffdatas);
//$fileData = $res;
$fileData = array_merge($fileData,$diffdatas);
$str_content = "<?php return ".var_export($fileData,TRUE).";";
file_put_contents($saveFile,$str_content);
}
}
//$diff = new Diff('pic');
//$diff->build();
//$diff->analyze();
//$diff->Display();
//$diff->Display("require");