Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

/*
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2015~2024 Min-Jhin,Chen

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QBPWCF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
    
*/

/*

目的:
解析兩個目標的差異
第一個參數放舊版本,第二個參數放新版本,第三個參數可以放要把新版本物件複製到的地方.

安裝:
複製到 /usr/bin 底下,給予執行的權限

example log file content input:
# diff -qr coolbee_v1.00.13-6 coolbee_v1.00.13-7 
Files coolbee_v1.00.13-6/application/controllers/admin/rpc_server.php and coolbee_v1.00.13-7/application/controllers/admin/rpc_server.php differ
Files coolbee_v1.00.13-6/application/controllers/app.php and coolbee_v1.00.13-7/application/controllers/app.php differ
Files coolbee_v1.00.13-6/application/controllers/cron.php and coolbee_v1.00.13-7/application/controllers/cron.php differ
Files coolbee_v1.00.13-6/application/controllers/dut.php and coolbee_v1.00.13-7/application/controllers/dut.php differ
Files coolbee_v1.00.13-6/application/controllers/portal.php and coolbee_v1.00.13-7/application/controllers/portal.php differ
Files coolbee_v1.00.13-6/application/controllers/y5busServer.php and coolbee_v1.00.13-7/application/controllers/y5busServer.php differ
Files coolbee_v1.00.13-6/application/core/HL_Model.php and coolbee_v1.00.13-7/application/core/HL_Model.php differ
Files coolbee_v1.00.13-6/application/helpers/vps_helper.php and coolbee_v1.00.13-7/application/helpers/vps_helper.php differ
Files coolbee_v1.00.13-6/application/language/chinese_traditional/setting_lang.php and coolbee_v1.00.13-7/application/language/chinese_traditional/setting_lang.php differ
Files coolbee_v1.00.13-6/application/language/english/setting_lang.php and coolbee_v1.00.13-7/application/language/english/setting_lang.php differ
Files coolbee_v1.00.13-6/application/language/french/setting_lang.php and coolbee_v1.00.13-7/application/language/french/setting_lang.php differ
Files coolbee_v1.00.13-6/application/language/spanish/setting_lang.php and coolbee_v1.00.13-7/application/language/spanish/setting_lang.php differ
Files coolbee_v1.00.13-6/application/language/zh-CN/setting_lang.php and coolbee_v1.00.13-7/application/language/zh-CN/setting_lang.php differ
Files coolbee_v1.00.13-6/application/libraries/HL_Log.php and coolbee_v1.00.13-7/application/libraries/HL_Log.php differ
Files coolbee_v1.00.13-6/application/models/dut_m.php and coolbee_v1.00.13-7/application/models/dut_m.php differ
Only in coolbee_v1.00.13-7: indexExtend.php
Files coolbee_v1.00.13-6/index.php and coolbee_v1.00.13-7/index.php differ
Only in coolbee_v1.00.13-7: logAgent.php
Files coolbee_v1.00.13-6/NOTICE.txt and coolbee_v1.00.13-7/NOTICE.txt differ
Files coolbee_v1.00.13-6/Release Note(ITRI).txt and coolbee_v1.00.13-7/Release Note(ITRI).txt differ
Files coolbee_v1.00.13-6/Release Note(RD).txt and coolbee_v1.00.13-7/Release Note(RD).txt differ
Files coolbee_v1.00.13-6/Release Note.txt and coolbee_v1.00.13-7/Release Note.txt differ
Files coolbee_v1.00.13-6/system/core/Common.php and coolbee_v1.00.13-7/system/core/Common.php differ
Files coolbee_v1.00.13-6/system/libraries/Log.php and coolbee_v1.00.13-7/system/libraries/Log.php differ
*/

//check param
if($_SERVER['argc']<3)
{
        help();
        return false;
}

//有差異的檔案清單於新版本的目錄.
$files=array();

//pasre params
$ov=$_SERVER['argv'][1];
$nv=$_SERVER['argv'][2];

//如果存在要複製新版本檔案到目的地的參數
if(isset($_SERVER['argv'][3]))
{
        $cp2=$_SERVER['argv'][3];
}

$cmd="diff -qr ".$ov." ".$nv;
exec($cmd,$output,$status);
if($status!==1 && $status!==0)
{
        echo "error occur!\r\n";
        print_r($output);
        exit;
}
foreach($output as $line)
{
        //檔案有差異
        //Files coolbee_v1.00.13-6/Release Note(RD).txt and coolbee_v1.00.13-7/Release Note(RD).txt differ
        if(strpos($line,"Files ")!==false && strpos($line,"differ")!==false)
        {
                if(strpos($line,"Files ")!==0)
                {
                        continue;
                }
                
                if(strpos($line," and ")!==false)
                {
                        $spiltedStr=explode(" and ",$line);
                        $spiltedStr[1]=str_replace($nv."/","",$spiltedStr[1]);                  
                        $files[]=str_replace(" differ","",$spiltedStr[1]);                      
                }
        }
        
        //只有某個版本有這檔案
        //Only in coolbee_v1.00.13-7: indexExtend.php
        //Only in patch: application
        $onlyIn=strpos($line,"Only in ");
        if($onlyIn!==false)
        {
                if($onlyIn!==0)
                {
                        continue;
                }
                
                $nvk=strpos($line,$nv);
                if($nvk!==false)
                {
                        if($nvk!==8)
                        {
                                continue;
                        }
                        
                        #剔除開頭的 "Only in ".$nv
                        $key2replace="Only in ".$nv;
                        $line=str_replace($key2replace,'',$line);
                        
                        #剔除開頭的 "/"
                        if(strpos($line,"/")===0)
                        {
                                $line=substr($line,1);  
                        } 
                        
                        #替換開頭的 ": " 為 "/"
                        if(strpos($line,": ")===0)
                        {
                                $line=substr($line,2);  
                        } 
                        
                        #置換 ": " 為 "/"
                        $key2replace=": ";
                        $line=str_replace($key2replace,'/',$line);
                        
                        #取得新檔案
                        $files[]=$line;
                }
        }       
}

//output file, folders should be patch
echo "以下為新版本的物件:\r\n";
foreach($files as $item)
{
        echo $item."\r\n";
}

//copy new version item to $cp2
if(isset($cp2))
{
        echo "複製新版本物件到patch目錄(".$cp2."):\r\n";
        
        #重新建立要複製到的目錄
        if(!file_exists($cp2))
        {
                mkdir($cp2,0770,true);
        }
        else
        {
                while(true)
                {
                        $cp2=$cp2."-".time();
                        if(!file_exists($cp2))
                        {
                                mkdir($cp2,0770,true);
                                break;
                        }
                }
                echo "要複製到的目標已存在,更名為:".$cp2.PHP_EOL;
        }
        
        foreach($files as $item)
        {
                //如果新版本物件是在第一層
                if(strpos($item,"/")===false)
                {
                        //複製新物件
                        
                        //如果是檔案
                        if(is_file($nv."/".$item))
                        {
                                $cmd="/usr/bin/cp '".$nv."/".$item."' '".$cp2."/".$item."'";
                        }
                        //反之如果是目錄
                        else if(is_dir($nv."/".$item))
                        {
                                $cmd="/usr/bin/cp -R '".$nv."/".$item."' '".$cp2."/".$item."'";
                        }
                        exec($cmd,$output,$status);
                        echo "exec ".$cmd."\r\n";
                        if($status!==0)
                        {
                                echo "error occur!\r\n";
                                print_r($output);
                                exit;
                        }
                }
                
                //反之新版本物件在更深層
                else
                {
                        //建立新物件的路徑
                        $path2add=$cp2;
                        $sourcePath=$nv;                                
                        $dirs=explode("/",$item);
                        $layers=count($dirs);
                        $nvf=$dirs[$layers-1];
                        $nvf=str_replace(": ","/",$nvf);
                        
                        for($i=0;$i<$layers-1;$i++)
                        {
                                $path2add=$path2add."/".$dirs[$i];
                                $sourcePath=$sourcePath."/".$dirs[$i];                          
                                @mkdir($path2add,0770,true);
                        }
                        
                        //複製新物件
                        if(is_file($sourcePath."/".$nvf))
                        {
                                $cmd="/usr/bin/cp '".$sourcePath."/".$nvf."' '".$path2add."/".$nvf."'";
                        }
                        else if(is_dir($sourcePath."/".$nvf))
                        {
                                $cmd="/usr/bin/cp -R '".$sourcePath."/".$nvf."' '".$path2add."/".$nvf."'";
                        }
                        else if(!file_exists($sourcePath."/".$nvf))
                        {
                                echo "error occur!\r\n";
                                echo "file ".$sourcePath."/".$nvf." not exist";
                                exit;
                        }
                        
                        exec($cmd,$output,$status);
                        echo "exec ".$cmd."\r\n";
                        if($status!==0)
                        {                               
                                echo "error occur!\r\n";
                                print_r($output);
                                exit;
                        }
                }
        }
}

//help
function help()
{
        echo "usage:\r\n";
        echo __FILE__ ." [old version] [new version] [copy new version item to]\r\n";
}

?>