import xml.etree.ElementTree as ET import sys import os import errno #svndiff.py http://code.taobao.org/svn/elk/trunk/code/python -r100:200 #导出指定svn之间的所有修改的代码文件 if __name__ == "__main__": if len(sys.argv) != 3 : print(' usage: svn_diff <-rfirst:second>') print(' example: svn_diff.py http://code.taobao.org/svn/elk/trunk/code/ -r45:56') exit() url = sys.argv[1] revision = sys.argv[2] #1, svn info and parse Root #svn info http://code.taobao.org/svn/elk/trunk/code/ --xml > svninfo.xml cmd = "svn info " + url + " --xml > svninfo.xml" os.system(cmd) tree = ET.parse('svninfo.xml') root = tree.getroot() svn_root_url = "" for item in root.iter('root'): svn_root_url = item.text #2, svn diff http://code.taobao.org/svn/elk/trunk/code/ -r45:head --summarize --xml cmd = "svn diff "+url + " " + revision + " --summarize --xml > svndiff.xml" os.system(cmd) #3 parse svndiff.xml tree = ET.parse('svndiff.xml') root = tree.getroot() path_set = set() for child in root: for paths in child.iter('paths'): for path in paths: if(path.get('kind')=="file"): path_set.add(path.text) #4 mkdir & svn export http://code.taobao.org/svn/elk/trunk/code/gpb/weblog.proto trunk/code/gpb svn_url_len = len(svn_root_url)+1 for i in path_set: end = i.rfind("/") path = i[svn_url_len:end] try: print("mkdir " + path) os.makedirs(path) except OSError as exc: if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: print("failed:"+ path) raise cmd = "svn export "+ i + " " + path print(cmd) os.system(cmd) print("all export finish") #os.system("svn export "+svn_file + " r"+revision)