1import xml.etree.ElementTree as ET
2import sys
3import os
4import errno
5
6#svndiff.py http://code.taobao.org/svn/elk/trunk/code/python -r100:200
7#������������svn������������������������������������
8
9
10if __name__ == "__main__":
11    if len(sys.argv) != 3 :
12        print('  usage: svn_diff <svn_url> <-rfirst:second>')
13        print('  example: svn_diff.py http://code.taobao.org/svn/elk/trunk/code/ -r45:56')
14        exit()
15    url = sys.argv[1]
16    revision = sys.argv[2]
17
18#1, svn info  and parse Root
19#svn info http://code.taobao.org/svn/elk/trunk/code/ --xml > svninfo.xml
20    cmd = "svn info " + url + " --xml > svninfo.xml"
21    os.system(cmd)
22    tree = ET.parse('svninfo.xml')
23    root = tree.getroot()
24    svn_root_url = ""
25    for item in root.iter('root'):
26        svn_root_url = item.text
27
28#2, svn diff http://code.taobao.org/svn/elk/trunk/code/ -r45:head --summarize --xml
29    cmd = "svn diff "+url + " " + revision + "  --summarize --xml > svndiff.xml"
30    os.system(cmd)
31
32#3 parse svndiff.xml
33    tree = ET.parse('svndiff.xml')
34    root = tree.getroot()
35    path_set = set()
36    for child in root:
37        for paths in child.iter('paths'):
38            for path in paths:
39                if(path.get('kind')=="file"):
40                    path_set.add(path.text)
41
42#4 mkdir & svn export http://code.taobao.org/svn/elk/trunk/code/gpb/weblog.proto trunk/code/gpb
43    svn_url_len = len(svn_root_url)+1
44    for i in path_set:
45        end = i.rfind("/")
46        path = i[svn_url_len:end]
47        try:
48            print("mkdir " + path)
49            os.makedirs(path)
50        except OSError as exc:
51            if exc.errno == errno.EEXIST and os.path.isdir(path):
52                pass
53            else:
54                print("failed:"+ path)
55                raise
56        cmd = "svn export "+  i + " " + path
57        print(cmd)
58        os.system(cmd)
59    print("all export finish")
60
61
62#os.system("svn export "+svn_file + "  r"+revision)
63
64