* task#562.

This commit is contained in:
wangchunsheng
2011-10-18 14:44:27 +00:00
parent f58a55214b
commit 761960a8b7
2 changed files with 263 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
<?php
/**
* client: svn客户端执行文件的路径,windows下面考虑安装Slik-Subversion,然后找到svn.exe的路径。linux下面比如/usr/bin/svn
* repos可以是多个,需要设定某一个库的访问路径,已经用户名和密码。
*
* client: the svn client binary path. You can install Slik-Subversion unser windows, then find the path of svn.exe. under linux, try /usr/bin/svn
* Can set multi repos, ervery one should set the path, username and password.
*
* 例子:
* $config->svn->client = '/usr/bin/svn'; // c:\svn\svn.exe
* $config->svn->repos['pms']['path'] = 'http://svn.zentao.net/zentao/trunk/';
* $config->svn->repos['pms']['username'] = 'user';
* $config->svn->repos['pms']['password'] = 'pass';
*
*/
$config->svn->client = '/usr/bin/svn';
$i = 1;
$config->svn->repos[$i]['path'] = 'http://zentaoms.googlecode.com/svn/trunk/';
$config->svn->repos[$i]['username'] = '';
$config->svn->repos[$i]['password'] = '';
/*
$i ++;
$config->svn->repos[$i]['path'] = '';
$config->svn->repos[$i]['username'] = '';
$config->svn->repos[$i]['password'] = '';
*/
$config->zentao->path = 'http://pms.5upm.com/';
$config->zentao->user = 'wwccss';
$config->zentao->password = '123456';
+231
View File
@@ -0,0 +1,231 @@
<?php
/**
* The syncer of svn.
*
* @copyright Copyright 2009-2011 青岛易软天创网络科技有限公司 (QingDao Nature Easy Soft Network Technology Co,LTD www.cnezsoft.com)
* @license LGPL (http://www.gnu.org/licenses/lgpl.html)
* @author Chunsheng Wang <chunsheng@cnezsoft.com>
* @package svn
* @version $Id$
* @link http://www.zentao.net
*/
include './config.php';
include './api.class.php';
$syncer = new syncer($config);
$syncer->run();
class syncer
{
/**
* The svn binary svnClient.
*
* @var string
* @access public
*/
public $svnClient;
/**
* The zentao client.
*
* @var string
* @access public
*/
public $zentaoClient;
/**
* Repos.
*
* @var array
* @access public
*/
public $repos = array();
/**
* The log root.
*
* @var string
* @access public
*/
public $logRoot = '';
/**
* The construct function.
*
* @access public
* @return void
*/
public function __construct($config)
{
$this->setConfig($config);
$this->setRepos();
$this->setLogRoot();
$this->loginZentao();
}
/**
* Set config.
*
* @param object $config
* @access public
* @return void
*/
public function setConfig($config)
{
$this->config = $config;
}
/**
* Set the repos.
*
* @access public
* @return void
*/
public function setRepos()
{
if(!$this->config->svn->repos) die("You must set one svn repo.\n");
$this->repos = $this->config->svn->repos;
}
/**
* Set the log root.
*
* @access public
* @return void
*/
public function setLogRoot()
{
$this->logRoot = './log/';
if(!is_dir($this->logRoot)) mkdir($this->logRoot);
}
/**
* Login to zentao.
*
* @access public
* @return void
*/
public function loginZentao()
{
$zentaoConfig = $this->config->zentao;
$this->zentaoClient = new ztclient($zentaoConfig->path, $zentaoConfig->user, $zentaoConfig->password);
}
/**
* Run.
*
* @access public
* @return void
*/
public function run()
{
foreach($this->repos as $name => $repo)
{
$this->printLog("begin repo $name");
$repo = (object)$repo;
$repo->name = $name;
$this->setRepo($repo);
$savedRevision = $this->getSavedRevision();
$this->printLog("start from revision $savedRevision");
$logs = $this->getRepoLogs($repo, $savedRevision);
$this->printLog("get " . count($logs) . " logs");
$this->printLog('begin posting logs');
print_r($this->zentaoClient->post('svn', 'apiSync', array('logs' => $logs)));
}
}
/**
* Set repo.
*
* @param object $repo
* @access public
* @return void
*/
public function setRepo($repo)
{
$this->setClient($repo);
$this->setLogFile($repo->name);
}
/**
* Set the svn binary svnClient of a repo.
*
* @param object $repo
* @access public
* @return void
*/
public function setClient($repo)
{
if($this->config->svn->client == '') die("You must set the svn svnClient file.\n");
$this->svnClient = $this->config->svn->client . " --non-interactive";
if(isset($repo->username)) $this->svnClient .= " --username '$repo->username' --password '$repo->password' --no-auth-cache";
}
/**
* Set the log file of a repo.
*
* @param string $repoName
* @access public
* @return void
*/
public function setLogFile($repoName)
{
$this->logFile = $this->logRoot . $repoName;
}
/**
* Get repo logs.
*
* @param object $repo
* @param int $fromRevision
* @access public
* @return string
*/
public function getRepoLogs($repo, $fromRevision)
{
$parsedLogs = array();
/* The svn log command. */
$cmd = $this->svnClient . " log -r $fromRevision:HEAD -v --xml $repo->path";
$logs = `$cmd`;
return $logs;
}
/**
* Get the saved revision.
*
* @access public
* @return int
*/
public function getSavedRevision()
{
if(!file_exists($this->logFile)) return 0;
return (int)trim(file_get_contents($this->logFile));
}
/**
* Save the last revision.
*
* @param int $revision
* @access public
* @return void
*/
public function saveLastRevision($revision)
{
file_put_contents($this->logFile, $revision);
}
/**
* Pring log.
*
* @param sting $log
* @access public
* @return void
*/
public function printLog($log)
{
echo date('Y-m-d H:i:s') . " $log\n";
}
}