/
vctimemachine.py
executable file
·205 lines (153 loc) · 6.24 KB
/
vctimemachine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
import cgi
import datetime
import os
import sys
import subprocess
import xml.etree.ElementTree as ET
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from RevisionDetailsWidget import *
def datetimeFromSvnDateString(txt):
return datetime.datetime.strptime(txt[:19], "%Y-%m-%dT%H:%M:%S")
def getUrlRevisions(url):
cmd = ["svn", "log", "-q", "--limit", "10", url]
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
revs = []
for line in pipe.readlines():
if line[0] == 'r':
tokens = line[1:].strip().split('|')
rev = int(tokens[0])
revs.append(rev)
return revs
def getLatestRevisionForUrl(url):
cmd = ["svn", "info", "--xml", url]
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
tree = ET.parse(pipe)
commitElement = tree.getroot().find("entry/commit")
assert commitElement
return int(commitElement.attrib["revision"])
def getUrlAtRevision(url, revision):
cmd = ["svn", "cat", "-r", str(revision), url]
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
return unicode(pipe.read(), "utf-8")
def getRevisionLog(url, revision):
cmd = ["svn", "log", "-v", "-r", str(revision), url]
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
return unicode(pipe.read(), "utf-8")
def formatSource(url, revision):
def fixSpaces(txt):
return txt.replace(" ", " ")
lines = getUrlAtRevision(url, revision).split("\n")
cmd = ["svn", "annotate", "--xml", "-r", str(revision), url]
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
tree = ET.parse(pipe)
sourceLines = []
commits = tree.getroot().findall("target/entry/commit")
for lineNumber, (code, commit) in enumerate(zip(lines, commits)):
rev = commit.attrib["revision"]
author = commit.findtext("author")
date = datetimeFromSvnDateString(commit.findtext("date"))
lineNumberHtml = str(lineNumber + 1).rjust(5)
lineNumberHtml = fixSpaces(lineNumberHtml)
dateHtml = date.strftime("%c")
revHtml = fixSpaces(rev.rjust(8))
if int(rev) == revision:
revClass = "current"
else:
revClass = ""
revHtml = "<a class='%s' href='r%s' title='%s'>%s</a>" % (revClass, rev, date, revHtml)
authorHtml = cgi.escape(author.rjust(10))
authorHtml = fixSpaces(authorHtml)
codeHtml = cgi.escape(code).replace("\t", " "*4)
codeHtml = fixSpaces(codeHtml)
line = "%s, %s, %s, %s<br>" % (lineNumberHtml, revHtml, authorHtml, codeHtml)
sourceLines.append(line)
html = """<html>
<head>
<style>
a.current {
color: red;
}
</style>
</head>
<body>
%s
</body>
</html>"""
sourceHtml = "\n".join(sourceLines)
return html % sourceHtml
class Window(QMainWindow):
def __init__(self, url):
QWidget.__init__(self)
self.url = url
fixedFont = QFont("Fixed")
fixedFont.setStyleHint(QFont.TypeWriter)
# Source browser
self.sourceBrowser = QTextBrowser()
self.sourceBrowser.setFont(fixedFont)
self.sourceBrowser.setOpenLinks(False)
# Revision details
self.revisionDetailsWidget = RevisionDetailsWidget()
self.revisionDetailsWidget.layout().setMargin(0)
# Layout
splitter = QSplitter()
splitter.setOrientation(Qt.Vertical)
splitter.addWidget(self.sourceBrowser)
splitter.addWidget(self.revisionDetailsWidget)
self.revisionDetailsWidget.hide()
self.setCentralWidget(splitter)
# Connections
QObject.connect(self.sourceBrowser, SIGNAL("anchorClicked(const QUrl&)"), self.slotSourceAnchorClicked)
QObject.connect(self.revisionDetailsWidget.goToPreviousButton, SIGNAL("clicked()"), self.goToPrevious)
QObject.connect(self.revisionDetailsWidget.goToCurrentButton, SIGNAL("clicked()"), self.goToCurrent)
QObject.connect(self.revisionDetailsWidget.goToNextButton, SIGNAL("clicked()"), self.goToNext)
QObject.connect(self.revisionDetailsWidget.showDiffButton, SIGNAL("clicked()"), self.showDiff)
revision = getLatestRevisionForUrl(url)
self.goToRevision(revision)
def goToRevision(self, revision):
source = formatSource(self.url, revision)
self.sourceBrowser.setHtml(source)
self.currentRevision = revision
self.setWindowTitle("%s r%d" % (self.url,self.currentRevision))
def goToNext(self):
revision = self.currentRevision + 1
self.goToRevision(revision)
def goToCurrent(self):
self.goToRevision(self.currentRevision)
def goToPrevious(self):
revision = self.currentRevision - 1
self.goToRevision(revision)
def sizeHint(self):
return QSize(800, 600)
def slotSourceAnchorClicked(self, link):
link = unicode(link.toString())
if link[0] == "r":
revision = int(link[1:])
self.showRevisionDetails(revision)
def showRevisionDetails(self, revision):
self.revisionDetailsWidget.show()
self.currentRevision = revision
details = getRevisionLog(self.url, self.currentRevision)
self.revisionDetailsWidget.revisionDetailsBrowser.setText(details)
self.revisionDetailsWidget.detailsLabel.setText("Showing details for r%d" % self.currentRevision)
self.revisionDetailsWidget.goToPreviousButton.setText("Go to r%d" % (self.currentRevision - 1))
self.revisionDetailsWidget.goToCurrentButton.setText("Go to r%d" % self.currentRevision)
self.revisionDetailsWidget.goToNextButton.setText("Go to r%d" % (self.currentRevision + 1))
def showDiff(self):
old = self.currentRevision - 1
new = self.currentRevision
cmd = ["svn", "diff", "-r%d:%d" % (old, new), self.url]
viewerCmd = ["gvim", "-R", "-"]
p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(viewerCmd, stdin=p1.stdout)
p2.communicate()
def main():
app = QApplication(sys.argv)
url = sys.argv[1]
url = os.path.abspath(url)
window = Window(url)
window.show()
app.exec_()
if __name__=="__main__":
main()