visual studio 의 vsproj 자동 변환기를 만들다가 두개의 vcproj 파일을 비교할 일이 있어서 검색해보았습니다.
으로 해야 한다는 점입니다.
제작자가 배치파일 하나를 빼먹은 것 같더군요.
ascii 나 utf8 이 아닌 xml 문서는 에러가 나기 때문에 미리 변환을 해줘야 하는데...
입력인자가 파일명이기 때문에 약간 손을 봐줘야 합니다.
원래 코드
def process_files(file1, file2, norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding,
html):
"""
Computes the diff between two files.
"""
from xml.sax import SAXParseException
try:
fh1, fh2 = open(file1, 'r'), open(file2, 'r')
except IOError, msg :
sys.stderr.write(str(msg) + '\n')
return -1
return process_filehandles(fh1, fh2, norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding,
html)
# convert xml files to tree
try:
from xmldiff.input import tree_from_stream
tree1 = tree_from_stream(fh1, norm_sp, ext_ges,
ext_pes, include_comment,
encoding, html)
tree2 = tree_from_stream(fh2, norm_sp, ext_ges,
ext_pes, include_comment,
encoding, html)
fh1.close ()
fh2.close ()
except SAXParseException, msg:
print msg
return -1
수정 코드: process_filehandles 함수 추가
def process_files(file1, file2, norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding,
html):
"""
Computes the diff between two files.
"""
try:
fh1, fh2 = open(file1, 'r'), open(file2, 'r')
except IOError, msg :
sys.stderr.write(str(msg) + '\n')
return -1
return process_filehandles(fh1, fh2, norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding,
html)
def process_filehandles(fh1, fh2, norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding,
html):
# convert xml files to tree
from xml.sax import SAXParseException
try:
from xmldiff.input import tree_from_stream
tree1 = tree_from_stream(fh1, norm_sp, ext_ges,
ext_pes, include_comment,
encoding, html)
tree2 = tree_from_stream(fh2, norm_sp, ext_ges,
ext_pes, include_comment,
encoding, html)
fh1.close ()
fh2.close ()
except SAXParseException, msg:
print msg
return -1
사용예는 다음과 같습니다. 엄청난 개수의 파라미터의 압박에 대비하시는 게 좋습니다.
import re
RE_XML_ENCODING = re.compile("encoding[ \t]*=[ \t]*\"([^\"]+)\"")
def decodeXML(data):
head = data[:100]
headline = head.splitlines()[0]
mo = RE_XML_ENCODING.search(headline)
if mo:
encoding = mo.group(1)
return data[len(headline):].decode(encoding)
else:
return data
import xmldiff.main
from cStringIO import StringIO
lhs = StringIO(decodeXML(open("left.vcproj").read()))
rhs = StringIO(decodeXML(open("right.vcproj").read()))
recursive, html = 0, 0
xupd, ezs, verbose = 0, 0, 0
norm_sp, include_comment, ext_ges, ext_pes = 1, 1, 0, 0
encoding = 'utf8'
print xmldiff.main.process_filehandles(lhs, rhs,
norm_sp, xupd, ezs, verbose,
ext_ges, ext_pes, include_comment, encoding, html)
비교 결과는 아래처럼 나옵니다.