1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os, time
17 from cPickle import dump
18
19 from zope.interface import implements
20 from twisted.python import log, runtime
21 from twisted.web import html
22
23 from buildbot import interfaces, util
24 from buildbot.process.properties import Properties
25
27 """I represent a single change to the source tree. This may involve
28 several files, but they are all changed by the same person, and there is
29 a change comment for the group as a whole.
30
31 If the version control system supports sequential repository- (or
32 branch-) wide change numbers (like SVN, P4, and Bzr), then revision=
33 should be set to that number. The highest such number will be used at
34 checkout time to get the correct set of files.
35
36 If it does not (like CVS), when= should be set to the timestamp (seconds
37 since epoch, as returned by time.time()) when the change was made. when=
38 will be filled in for you (to the current time) if you omit it, which is
39 suitable for ChangeSources which have no way of getting more accurate
40 timestamps.
41
42 The revision= and branch= values must be ASCII bytestrings, since they
43 will eventually be used in a ShellCommand and passed to os.exec(), which
44 requires bytestrings. These values will also be stored in a database,
45 possibly as unicode, so they must be safely convertable back and forth.
46 This restriction may be relaxed in the future.
47
48 Changes should be submitted to ChangeMaster.addChange() in
49 chronologically increasing order. Out-of-order changes will probably
50 cause the web status displays to be corrupted."""
51
52 implements(interfaces.IStatusEvent)
53
54 number = None
55
56 branch = None
57 category = None
58 revision = None
59
60 - def __init__(self, who, files, comments, isdir=0, links=None,
61 revision=None, when=None, branch=None, category=None,
62 revlink='', properties={}, repository='', project=''):
63 self.who = who
64 self.comments = comments
65 self.isdir = isdir
66 if links is None:
67 links = []
68 self.links = links
69
70 def none_or_unicode(x):
71 if x is None: return x
72 return unicode(x)
73
74 self.revision = none_or_unicode(revision)
75 now = util.now()
76 if when is None:
77 self.when = now
78 elif when > now:
79
80
81 log.msg("received a Change with when > now; assuming the change happened now")
82 self.when = now
83 else:
84 self.when = when
85 self.branch = none_or_unicode(branch)
86 self.category = none_or_unicode(category)
87 self.revlink = revlink
88 self.properties = Properties()
89 self.properties.update(properties, "Change")
90 self.repository = repository
91 self.project = project
92
93
94 self.files = files[:]
95 self.files.sort()
96
98 self.__dict__ = dict
99
100 if not hasattr(self, 'properties'):
101 self.properties = Properties()
102 if not hasattr(self, 'revlink'):
103 self.revlink = ""
104
106 data = ""
107 data += self.getFileContents()
108 if self.repository:
109 data += "On: %s\n" % self.repository
110 if self.project:
111 data += "For: %s\n" % self.project
112 data += "At: %s\n" % self.getTime()
113 data += "Changed By: %s\n" % self.who
114 data += "Comments: %s" % self.comments
115 data += "Properties: \n%s\n\n" % self.getProperties()
116 return data
117
119 '''returns a dictonary with suitable info for html/mail rendering'''
120 result = {}
121
122 files = []
123 for file in self.files:
124 link = filter(lambda s: s.find(file) != -1, self.links)
125 if len(link) == 1:
126 url = link[0]
127 else:
128 url = None
129 files.append(dict(url=url, name=file))
130
131 files = sorted(files, cmp=lambda a, b: a['name'] < b['name'])
132
133
134 result['number'] = self.number
135 result['branch'] = self.branch
136 result['category'] = self.category
137 result['who'] = self.getShortAuthor()
138 result['comments'] = self.comments
139 result['revision'] = self.revision
140 result['rev'] = self.revision
141 result['when'] = self.when
142 result['at'] = self.getTime()
143 result['files'] = files
144 result['revlink'] = getattr(self, 'revlink', None)
145 result['properties'] = self.properties.asList()
146 result['repository'] = getattr(self, 'repository', None)
147 result['project'] = getattr(self, 'project', None)
148 return result
149
152
154 if not self.when:
155 return "?"
156 return time.strftime("%a %d %b %Y %H:%M:%S",
157 time.localtime(self.when))
158
160 return (self.when, None)
161
163 return [html.escape(self.who)]
166
167 - def getFileContents(self):
168 data = ""
169 if len(self.files) == 1:
170 if self.isdir:
171 data += "Directory: %s\n" % self.files[0]
172 else:
173 data += "File: %s\n" % self.files[0]
174 else:
175 data += "Files:\n"
176 for f in self.files:
177 data += " %s\n" % f
178 return data
179
181 data = ""
182 for prop in self.properties.asList():
183 data += " %s: %s" % (prop[0], prop[1])
184 return data
185
186
188
189
190
191
192
193
195 self.changes = []
196
197 self.nextNumber = 1
198
200 change.number = self.nextNumber
201 self.nextNumber += 1
202 self.changes.append(change)
203
205 filename = os.path.join(self.basedir, "changes.pck")
206 tmpfilename = filename + ".tmp"
207 try:
208 dump(self, open(tmpfilename, "wb"))
209 if runtime.platformType == 'win32':
210
211 if os.path.exists(filename):
212 os.unlink(filename)
213 os.rename(tmpfilename, filename)
214 except Exception:
215 log.msg("unable to save changes")
216 log.err()
217
218
219
221 """Processes the list of changes, with the change attributes re-encoded
222 as UTF-8 bytestrings"""
223 nconvert = 0
224 for c in self.changes:
225
226 if isinstance(c.revision, int):
227 c.revision = unicode(c.revision)
228
229 for attr in ("who", "comments", "revlink", "category", "branch", "revision"):
230 a = getattr(c, attr)
231 if isinstance(a, str):
232 try:
233 setattr(c, attr, a.decode(old_encoding))
234 nconvert += 1
235 except UnicodeDecodeError:
236 raise UnicodeError("Error decoding %s of change #%s as %s:\n%r" %
237 (attr, c.number, old_encoding, a))
238 if not quiet:
239 print "converted %d strings" % nconvert
240
244
245