Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buildbot/buildbot/changes/changes.py
blob: 7d399e0b1d06f282f1dcfe2b75858a0fd0a78eab (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

import sys, os, time
from cPickle import dump

from zope.interface import implements
from twisted.python import log
from twisted.internet import defer
from twisted.application import service
from twisted.web import html

from buildbot import interfaces, util

html_tmpl = """
<p>Changed by: <b>%(who)s</b><br />
Changed at: <b>%(at)s</b><br />
%(branch)s
%(revision)s
<br />

Changed files:
%(files)s

Comments:
%(comments)s
</p>
"""

class Change:
    """I represent a single change to the source tree. This may involve
    several files, but they are all changed by the same person, and there is
    a change comment for the group as a whole.

    If the version control system supports sequential repository- (or
    branch-) wide change numbers (like SVN, P4, and Arch), then revision=
    should be set to that number. The highest such number will be used at
    checkout time to get the correct set of files.

    If it does not (like CVS), when= should be set to the timestamp (seconds
    since epoch, as returned by time.time()) when the change was made. when=
    will be filled in for you (to the current time) if you omit it, which is
    suitable for ChangeSources which have no way of getting more accurate
    timestamps.

    Changes should be submitted to ChangeMaster.addChange() in
    chronologically increasing order. Out-of-order changes will probably
    cause the html.Waterfall display to be corrupted."""

    implements(interfaces.IStatusEvent)

    number = None

    links = []
    branch = None
    revision = None # used to create a source-stamp

    def __init__(self, who, files, comments, isdir=0, links=[],
                 revision=None, when=None, branch=None, category=None):
        self.who = who
        self.comments = comments
        self.isdir = isdir
        self.links = links
        self.revision = revision
        if when is None:
            when = util.now()
        self.when = when
        self.branch = branch
        self.category = category

        # keep a sorted list of the files, for easier display
        self.files = files[:]
        self.files.sort()

    def asText(self):
        data = ""
        data += self.getFileContents() 
        data += "At: %s\n" % self.getTime()
        data += "Changed By: %s\n" % self.who
        data += "Comments: %s\n\n" % self.comments
        return data

    def asHTML(self):
        links = []
        for file in self.files:
            link = filter(lambda s: s.find(file) != -1, self.links)
            if len(link) == 1:
                # could get confused
                links.append('<a href="%s"><b>%s</b></a>' % (link[0], file))
            else:
                links.append('<b>%s</b>' % file)
        revision = ""
        if self.revision:
            revision = "Revision: <b>%s</b><br />\n" % self.revision
        branch = ""
        if self.branch:
            branch = "Branch: <b>%s</b><br />\n" % self.branch

        kwargs = { 'who'     : html.escape(self.who),
                   'at'      : self.getTime(),
                   'files'   : html.UL(links) + '\n',
                   'revision': revision,
                   'branch'  : branch,
                   'comments': html.PRE(self.comments) }
        return html_tmpl % kwargs

    def get_HTML_box(self, url):
        """Return the contents of a TD cell for the waterfall display.

        @param url: the URL that points to an HTML page that will render
        using our asHTML method. The Change is free to use this or ignore it
        as it pleases.

        @return: the HTML that will be put inside the table cell. Typically
        this is just a single href named after the author of the change and
        pointing at the passed-in 'url'.
        """
        who = self.getShortAuthor()
        if self.comments is None:
            title = ""
        else:
            title = html.escape(self.comments)
        return '<a href="%s" title="%s">%s</a>' % (url,
                                                   title,
                                                   html.escape(who))

    def getShortAuthor(self):
        return self.who

    def getTime(self):
        if not self.when:
            return "?"
        return time.strftime("%a %d %b %Y %H:%M:%S",
                             time.localtime(self.when))

    def getTimes(self):
        return (self.when, None)

    def getText(self):
        return [html.escape(self.who)]
    def getLogs(self):
        return {}

    def getFileContents(self):
        data = ""
        if len(self.files) == 1:
            if self.isdir:
                data += "Directory: %s\n" % self.files[0]
            else:
                data += "File: %s\n" % self.files[0]
        else:
            data += "Files:\n"
            for f in self.files:
                data += " %s\n" % f
        return data
        
class ChangeMaster(service.MultiService):

    """This is the master-side service which receives file change
    notifications from CVS. It keeps a log of these changes, enough to
    provide for the HTML waterfall display, and to tell
    temporarily-disconnected bots what they missed while they were
    offline.

    Change notifications come from two different kinds of sources. The first
    is a PB service (servicename='changemaster', perspectivename='change'),
    which provides a remote method called 'addChange', which should be
    called with a dict that has keys 'filename' and 'comments'.

    The second is a list of objects derived from the ChangeSource class.
    These are added with .addSource(), which also sets the .changemaster
    attribute in the source to point at the ChangeMaster. When the
    application begins, these will be started with .start() . At shutdown
    time, they will be terminated with .stop() . They must be persistable.
    They are expected to call self.changemaster.addChange() with Change
    objects.

    There are several different variants of the second type of source:
    
      - L{buildbot.changes.mail.MaildirSource} watches a maildir for CVS
        commit mail. It uses DNotify if available, or polls every 10
        seconds if not.  It parses incoming mail to determine what files
        were changed.

      - L{buildbot.changes.freshcvs.FreshCVSSource} makes a PB
        connection to the CVSToys 'freshcvs' daemon and relays any
        changes it announces.
    
    """

    implements(interfaces.IEventSource)

    debug = False
    # todo: use Maildir class to watch for changes arriving by mail

    def __init__(self):
        service.MultiService.__init__(self)
        self.changes = []
        # self.basedir must be filled in by the parent
        self.nextNumber = 1

    def addSource(self, source):
        assert interfaces.IChangeSource.providedBy(source)
        assert service.IService.providedBy(source)
        if self.debug:
            print "ChangeMaster.addSource", source
        source.setServiceParent(self)

    def removeSource(self, source):
        assert source in self
        if self.debug:
            print "ChangeMaster.removeSource", source, source.parent
        d = defer.maybeDeferred(source.disownServiceParent)
        return d

    def addChange(self, change):
        """Deliver a file change event. The event should be a Change object.
        This method will timestamp the object as it is received."""
        log.msg("adding change, who %s, %d files, rev=%s, branch=%s, "
                "comments %s, category %s" % (change.who, len(change.files),
                                              change.revision, change.branch,
                                              change.comments, change.category))
        change.number = self.nextNumber
        self.nextNumber += 1
        self.changes.append(change)
        self.parent.addChange(change)
        # TODO: call pruneChanges after a while

    def pruneChanges(self):
        self.changes = self.changes[-100:] # or something

    def eventGenerator(self, branches=[]):
        for i in range(len(self.changes)-1, -1, -1):
            c = self.changes[i]
            if not branches or c.branch in branches:
                yield c

    def getChangeNumbered(self, num):
        if not self.changes:
            return None
        first = self.changes[0].number
        if first + len(self.changes)-1 != self.changes[-1].number:
            log.msg(self,
                    "lost a change somewhere: [0] is %d, [%d] is %d" % \
                    (self.changes[0].number,
                     len(self.changes) - 1,
                     self.changes[-1].number))
            for c in self.changes:
                log.msg("c[%d]: " % c.number, c)
            return None
        offset = num - first
        log.msg(self, "offset", offset)
        return self.changes[offset]

    def __getstate__(self):
        d = service.MultiService.__getstate__(self)
        del d['parent']
        del d['services'] # lose all children
        del d['namedServices']
        return d

    def __setstate__(self, d):
        self.__dict__ = d
        # self.basedir must be set by the parent
        self.services = [] # they'll be repopulated by readConfig
        self.namedServices = {}


    def saveYourself(self):
        filename = os.path.join(self.basedir, "changes.pck")
        tmpfilename = filename + ".tmp"
        try:
            dump(self, open(tmpfilename, "wb"))
            if sys.platform == 'win32':
                # windows cannot rename a file on top of an existing one
                if os.path.exists(filename):
                    os.unlink(filename)
            os.rename(tmpfilename, filename)
        except Exception, e:
            log.msg("unable to save changes")
            log.err()

    def stopService(self):
        self.saveYourself()
        return service.MultiService.stopService(self)

class TestChangeMaster(ChangeMaster):
    """A ChangeMaster for use in tests that does not save itself"""
    def stopService(self):
        return service.MultiService.stopService(self)