root/hodgestar/PythonCode/SvnTools/svn-filter-logs.py

Revision 298, 2.0 kB (checked in by simon, 4 years ago)

Very basic script for filter svn logs.

  • Property svn:mime-type set to text/python-source
  • Property svn:eol-style set to native
Line 
1"""Small script for filtering entries out of SVN logs.
2   """
3
4class LogEntry(object):
5    def __init__(self, iRevision, sUser,
6                 sDate, iLines, sData):
7        self.iRevision = iRevision
8        self.sUser = sUser
9        self.sDate = sDate
10        self.iLines = iLines
11        self.sData = sData
12
13    @classmethod
14    def from_string(self, s):
15        aLines = s.split("\n")
16
17        aHeaders = [s.strip() for s in aLines[0].split("|")]
18        iRevision = int(aHeaders[0][1:])
19        iLines = int(aHeaders[3].split()[0])
20
21        sData = "\n".join(aLines[2:])
22
23        return LogEntry(iRevision, aHeaders[1], aHeaders[2],
24                        iLines, sData)
25
26    def __str__(self):
27        return """r%d | %s | %s | %i line%s\n""" \
28               % (self.iRevision, self.sUser, self.sDate, self.iLines,
29                  '' if self.iLines == 1 else 's') + \
30               """%s""" % self.sData
31
32    def matches(self, dFilter):
33        bMatch = True
34        if dFilter.get('user',self.sUser) != self.sUser:
35            bMatch = False
36        return bMatch
37
38class SvnLogIter(object):
39    SEP = "---------------------------------"
40
41    def __init__(self, fFile):
42        self._fFile = fFile
43        self._aLines = []
44
45    def __iter__(self):
46        for sLine in self._fFile:
47            if sLine.startswith(self.SEP):
48                if self._aLines:
49                    yield LogEntry.from_string("".join(self._aLines))
50                    self._aLines = []
51            else:
52                self._aLines.append(sLine)
53
54def main(aArgs):
55    sUser = aArgs[1]
56    sLogFile = aArgs[2]
57
58    dFilter = {'user': sUser}
59    fLogFile = file(sLogFile,"r")
60
61    iTotal, iMatched = 0, 0
62
63    for oLog in SvnLogIter(fLogFile):
64        iTotal += 1
65        if oLog.matches(dFilter):
66            iMatched += 1
67            print oLog
68
69    fLogFile.close()
70
71    print SvnLogIter.SEP
72    print "%i / %i matched." % (iMatched, iTotal)
73
74    return 0
75
76if __name__ == "__main__":
77    import sys
78    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the browser.