2010年1月12日 星期二

使用svn 去產生 Working copy 與 repository 的比對報表

目的: 使用svn 去產生 Working copy 與 repository 的比對報表
解決方案:
1.Ad hoc 方法:
a.安裝支援 CLI(Command Line Interface) 的 svn 軟體,利用Command Line 去產生比對報表

2.利用 程式產生,並使用svn command line :
a. 利用DiffUtils 去產生 Context format ,如果不想看Context format 就用 svn 原始的diff 去產生 unified format
b.GNU32 -DiffUtils 2.8.7 (show differences between files)
http://gnuwin32.sourceforge.net/
c.SVN- Setup-Subversion-1.6.6.msi
http://subversion.tigris.org/

#!/usr/bin/env python
# Python version of the bash shell "svnreport"

"""
SVNREPORT1.EXE.
Used to compare working copy and Repository ,genereate compare report.
It's 100 % Python :)
I use Python 2.6.4 , py2exe to create SVNREPORT1.EXE
Diff report format : Unified diff format
"""

__author__ = ' Ray Yeh <yeh_ray@hotmail.com>'
__version__ = '1.0'
__licence__ = 'GPL V3'


import sys
import os
import re
from datetime import datetime


def Usage():
print """Usage: """ + sys.argv[0] + """ PATH >> Report_file
Usage sample1 : svnreport1.exe d:\project >> d:\svnreport1.txt --> Output report at d:\svnreport.txt
Usage sample 2: svnreport1.exe d:\project --> Output at screen
Batch compare working copy with repository and generate report to PATH

by Ray Yeh version 1.0
"""
sys.exit(1)

#------------------------------- Process the options -------------------------
if len(sys.argv)==2 :
workingdir=sys.argv[1]
else:
Usage()

try:
os.chdir(workingdir)
except OSError:
print "Cannot change dir to " + workingdir
Usage()

#------------------------------- Find out what has changed -------------------

svnstatus=os.popen("svn status").readlines()

added=""
deleted=""
modified=""
commit_message=""
#print svnstatus

for line in svnstatus:

matchObject=re.match(r"^\?\s*(.*)\n",line)
if matchObject:
added = added + "\"" + matchObject.group(1) + "\" "
#print matchObject.group(1)
commit_message += "added file " + matchObject.group(1) + "\n"

matchObject=re.match(r"^\!\s*(.*)\n",line)
if matchObject:
deleted = deleted + "\"" + matchObject.group(1) + "\" "
commit_message += "deleted file " + matchObject.group(1) + "\n"

matchObject=re.match(r"^\M\s*(.*)\n",line)
if matchObject:
modified = modified + "\"" + matchObject.group(1) + "\" "
commit_message += "modified file " + matchObject.group(1) + "\n"

'''if added:
#os.system("svn diff "+added)
if deleted:
print "deleted-"+deleted
#os.system("svn diff "+deleted) '''
if modified:
os.system('svn diff ' +modified )


if not added:
commit_message += "no added files\n"
if not deleted:
commit_message += "no deleted files\n"
if not modified:
commit_message += "no modified files\n"

modifiedcount =0
addedcount = 0
deletedcount=0

addedlist=added.split()
modifiedlist=modified.split()
deletedlist=deleted.split()

i=0
for i in addedlist:
addedcount +=1
i=0
for i in modifiedlist:
modifiedcount +=1
i=0
for i in deletedlist:
deletedcount +=1

dt=datetime.now()
print "======================================================="
print "SVN compare run at:"+ dt.strftime("%A, %d. %B %Y %I:%M%p")
print "Compare Woking directory:"+workingdir
print "Add %d folders/files:\n%s" %(addedcount, added)
print "Modified %d folders/files:\n%s" %(modifiedcount, modified)
print "Deleted %d folders/files:\n%s" %(deletedcount, deleted)
print "======================================================="





3.利用程式產生,不使用svn command line ,
a.pysvn -http://pysvn.tigris.org/
b.python 2.6.4

### -*- coding: utf-8 -*-
#!/usr/bin/env python

"""
PYSVNRPT.EXE.
Used to compare working copy and Repository ,genereate compare report.
It's 100 % Python :)
I use Python 2.6.4 , py2exe,pysvn 1.7.2 to create PYSVNRPT.EXE
Diff report format : Unified diff format
"""

__author__ = ' Ray Yeh <yeh_ray@hotmail.com>'
__version__ = '1.0'
__licence__ = 'GPL V3'


import sys
import os
import re
from datetime import datetime
import pysvn


def Usage():
print """Usage: """ + sys.argv[0] + """ PATH >> Report_file
Usage sample1 : PYSVNRPT.exe d:\project >> d:\svnreport.txt --> Output report at d:\svnreport.txt
Usage sample 2: PYSVNRPT.exe d:\project --> Output at screen
Batch compare working copy with repository and generate report to PATH

by Ray Yeh version 1.0
"""
sys.exit(1)

#------------------------------- Process the options -------------------------
if len(sys.argv)==2 :
workingdir=sys.argv[1]
else:
Usage()

try:
os.chdir(workingdir)
except OSError:
print "Cannot change dir to " + workingdir
Usage()
#------------------------------- Find out what has changed -------------------
client = pysvn.Client()
changes = client.status(workingdir)

#print 'files to be added:'
addedlist =[f.path for f in changes if f.text_status == pysvn.wc_status_kind.added]
#print 'files to be removed:'
deletedlist=[f.path for f in changes if f.text_status == pysvn.wc_status_kind.deleted]
#print 'files that have changed:'
modifiedlist=[f.path for f in changes if f.text_status == pysvn.wc_status_kind.modified]
#print 'files with merge conflicts:'
conflictedlist=[f.path for f in changes if f.text_status == pysvn.wc_status_kind.conflicted]
#print 'unversioned files:'
unversionedlist=[f.path for f in changes if f.text_status == pysvn.wc_status_kind.unversioned]
#print 'missing files:'
missinglist=[f.path for f in changes if f.text_status == pysvn.wc_status_kind.missing]

dt=datetime.now()

print "SVN compare run at:"+ dt.strftime("%A, %d. %B %Y %I:%M%p")
print "Compare Woking directory:"+workingdir + "\n"



print "====================="+u" 產生 Compare 資料開始 "+"==================="
for item in modifiedlist:
difftext=client.diff('t:',item)
print difftext
print "====================="+u" 產生 Compare 資料結束 "+"==================="


deletedcount=0
modifiedcount=0
missingcount=0
addedcount=0
unversionedcount=0

i=0
for i in addedlist:
addedcount +=1
i=0
for i in modifiedlist:
modifiedcount +=1
i=0
for i in missinglist:
missingcount+=1
i=0
for i in unversionedlist:
unversionedcount +=1

print u"比對目錄路徑:"+workingdir
print u"比對檔案清單:"
print u"新增 %d folders/files:\n%s" %(addedcount, addedlist)
print u"差異 %d folders/files:\n%s" %(modifiedcount, modifiedlist)
print u"刪除 %d folders/files:\n%s" %(missingcount, missinglist)
print u"未納版 %d folders/files:\n%s" %(unversionedcount,unversionedlist)
print "=========================================================="


4. 安裝 Pysvn WorkBench Subversion

沒有留言: