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