]> Pileus Git - ~andy/sunrise/blob - scripts/SunriseCIA/SunriseCIA.py
scripts/SunriseCIA: Importing our used bot
[~andy/sunrise] / scripts / SunriseCIA / SunriseCIA.py
1 import xml.sax 
2 from xml.sax import saxutils
3 from xml.sax import make_parser
4 # import time
5 # from pprint import pprint
6
7 class CommitParserHandler(saxutils.DefaultHandler):
8         def __init__(self):
9                 self.inRevision = 0
10                 self.inAuthor = 0
11                 self.inLog = 0
12                 self.inFile = 0
13                 self.filelist = []
14                 self.author = ""
15                 self.revision = ""
16                 self.logmessage = ""
17                 
18         def startElement(self, name, attrs):
19                 if name == "revision":
20                         self.buffer = ""
21                         self.inRevision = 1
22                 if name == "author":
23                         self.buffer = ""
24                         self.inAuthor = 1
25                 if name == "log":
26                         self.buffer = ""
27                         self.inLog = 1
28                 if name == "file":
29                         self.buffer = ""
30                         self.inFile = 1
31                 
32         def characters(self, data):
33                 if self.inRevision == 1 or self.inAuthor == 1 or self.inLog == 1 or self.inFile == 1:
34                         self.buffer += data
35
36         def endElement(self, name):     
37                 if name == "revision":
38                         self.inRevision = 0
39                         self.revision = self.buffer
40                 if name == "author":
41                         self.inAuthor = 0
42                         self.author = self.buffer
43                 if name == "log":
44                         self.inLog = 0
45                         self.logmessage = self.buffer
46                 if name == "file":
47                         self.inFile = 0
48                         self.filelist.append(self.buffer)
49
50 class CommitParser:
51         def __init__(self):
52                 self.filename = ""
53                 self.author = ""
54                 self.revision = ""
55                 self.pathline = ""
56                 self.logmessage = ""
57                 self.filecount = 0
58                 self.dircount = 0
59                 self.filelist = []
60                 self.path = ""
61         
62         def parse(self):
63                 parser = make_parser( )
64                 handler = CommitParserHandler( )
65                 parser.setContentHandler(handler)
66                 parser.parse(self.filename)
67
68                 # Let's find out what dirs were touched
69                 filelist = handler.filelist
70                 finished = 0
71                 path = ""
72                 multidir = 0
73                 while finished == 0:
74                         ok = 1
75                         i = 0
76                         subdirlinecount = 0
77                         subdirslashpos = 0
78                         maxi = len(filelist)
79                         if (i+1) == maxi:
80                                 ok = 0
81                         while i < maxi and ok == 1:
82                                 search = filelist[i].find("/")
83                                 if search == -1:
84                                         #okay, we have a fil in the "top" dir let's stop it
85                                         ok = 0
86                                         break
87                                 if search > -1:
88                                         # we have a subdir in here, so count ;)
89                                         subdirlinecount += 1
90                                 i += 1
91                         
92                         if ok == 1 and multidir == 0:
93                                 if subdirlinecount == maxi:
94                                         #okay all dirs still have a slash
95                                         # everything in the same dir?
96                                         subdirlinecount = 0
97                                         lastdir = ""
98                                         # count dirs
99                                         filelist.sort()
100                                         for dir in filelist:
101                                                 if dir.find("/") > -1:
102                                                         if dir[:dir.find("/")+1] != lastdir:
103                                                                 subdirlinecount += 1
104                                                                 lastdir = dir[:dir.find("/")+1]
105     
106                                         if subdirlinecount == 1:
107                                                 # okay, all lines have the same slashpos.
108                                                 # strip everything up to it
109                                                 i = 0
110                                                 path += filelist[0][:search+1]
111                                                 while i < maxi:
112                                                         filelist[i] = filelist[i][search+1:]
113                                                         i += 1
114                                                 filelist.sort()
115                                                 for line in filelist:
116                                                         if line == "":
117                                                                 filelist.remove("")
118                                         else:
119                                                 #no we seem to have the topdir now..
120                                                 dircount = subdirlinecount
121                                                 multidir = 1
122                         else:
123                                 finished=1
124                                 filecount = 0
125                                 dircount = 0
126                                 lastdir = ""
127                                 # count dirs
128                                 filelist.sort()
129                                 for dir in filelist:
130                                         if dir.find("/") > -1:
131                                                 if dir[:dir.find("/")+1] != lastdir:
132                                                         dircount += 1
133                                                         lastdir = dir[:dir.find("/")+1]
134                                                 
135                                 filecount = maxi
136                 self.author = handler.author
137                 self.revision = handler.revision
138                 self.logmessage = handler.logmessage
139                 self.dircount = dircount
140                 self.filecount = filecount
141                 self.filelist = filelist
142                 self.path = path        
143         def generate_pathline(self):
144                 self.pathline = ""
145                 if self.filecount == 1 and self.dircount == 0:
146                         self.pathline = self.path + filelist[0]
147                 if (self.filecount > 1 or self.filecount < 4) and self.dircount == 0:
148                         self.pathline = self.path + " ("
149                         for file in self.filelist:
150                                 self.pathline += file + " "
151                         self.pathline = self.pathline[:-1] + ")"
152                 if self.filecount >= 1 and self.dircount >= 1 and self.filecount+self.dircount < 4:
153                         self.pathline = self.path + " ("
154                         for file in self.filelist:
155                                 self.pathline += file + " "
156                         self.pathline = self.pathline[:-1] + ")"
157                 if self.filecount >= 1 and self.dircount >= 1 and self.filecount+self.dircount >= 4:
158                         self.pathline = self.path + " ("
159                         if self.filecount == 1:
160                                 self.pathline += "1 file"
161                         if self.filecount > 1:
162                                 self.pathline += "%i files" % self.filecount
163                         if self.dircount == 1:
164                                 self.pathline += " in 2 dirs"
165                         if self.dircount > 1:
166                                 self.pathline += " in %i dirs" % (self.dircount)
167                         self.pathline += ")"
168         
169         def doit(self):
170                 self.parse()
171                 self.generate_pathline()