mod_hg.py

changeset 144
b3d1b356e544
parent 132
a22c50f52a23
child 145
588aff83bb87
equal deleted inserted replaced
143:d86a81540a71 144:b3d1b356e544
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ''' 27 '''
28 28
29 from hgapi import hgapi, Repo
30 from datetime import datetime 29 from datetime import datetime
31 import hgpoll as HgPoll 30 import hgpoll as HgPoll
32 import re 31 import re
33 import bt as Bt 32 import bt as Bt
33 import subprocess
34 from configfile import Config 34 from configfile import Config
35 from modulecore import command_error 35 from modulecore import command_error
36 36
37 ModuleData = { 37 ModuleData = {
38 'commands': 38 'commands':
52 }, 52 },
53 53
54 { 54 {
55 'name': 'hg', 55 'name': 'hg',
56 'description': 'Executes a hg command', 56 'description': 'Executes a hg command',
57 'args': '<repo> <command...>', 57 'args': '<command...>',
58 'level': 'admin', 58 'level': 'admin',
59 }, 59 },
60 60
61 { 61 {
62 'name': 'resolves', 62 'name': 'resolves',
63 'description': '''Manually cause a ticket to be resolved by a changeset''', 63 'description': '''Manually cause a ticket to be resolved by a changeset''',
64 'args': '<ticket> <changeset>', 64 'args': '<ticket> <changeset>',
65 'level': 'admin', # TODO 65 'level': 'admin', # TODO
66 },
67
68 {
69 'name': 'rebuildcommitsdb',
70 'description': '''Rebuilds commits.db''',
71 'args': None,
72 'level': 'admin',
73 },
74
75 {
76 'name': 'updatezadevtopic',
77 'description': """Updates #zadev topic""",
78 'args': None,
79 'level': 'admin',
80 }, 66 },
81 ] 67 ]
82 } 68 }
83 69
84 def plural (a): 70 def plural (a):
125 111
126 def cmd_cset (bot, args, reply, **rest): 112 def cmd_cset (bot, args, reply, **rest):
127 data = "" 113 data = ""
128 node, reponame = resolve_node (args['key']) 114 node, reponame = resolve_node (args['key'])
129 repourl = HgPoll.get_repo_info (reponame).get_value ('url') 115 repourl = HgPoll.get_repo_info (reponame).get_value ('url')
130 repo = Repo (reponame)
131 delim = '@@@@@@@@@@@@' 116 delim = '@@@@@@@@@@@@'
132 117
133 try: 118 try:
134 data = repo.hg_command ("log", "-l1", "-r", node, "--template", 119 data = subprocess.check_output (['hg', '--cwd', reponame, "log", "-l1", "-r", node,
135 delim.join (["{node|short}", 120 "--template", delim.join (["{node|short}",
136 "{desc}", 121 "{desc}",
137 "{author}", 122 "{author|person}",
138 "{diffstat}", 123 "{diffstat}",
139 "{date|hgdate}", 124 "{date|hgdate}",
140 "{bookmarks}", 125 "{bookmarks}",
141 "{latesttagdistance}", 126 "{latesttagdistance}",
142 "{latesttag}"])).split (delim) 127 "{latesttag}",
143 except hgapi.HgException: 128 "{author|email}"]
129 )
130 ]).split (delim)
131 except Subprocess.CalledProcessError:
144 command_error ('''couldn't find changeset %s in %s''' % (node, reponame)) 132 command_error ('''couldn't find changeset %s in %s''' % (node, reponame))
145 return 133
146 134 node = data[0]
147 try: 135 message = data[1]
148 node = data[0] 136 author = data[2]
149 message = data[1] 137 diffstat = data[3]
150 author = data[2] 138 date = datetime.utcfromtimestamp (int (data[4].split (' ')[0]))
151 diffstat = data[3] 139 bookmarks = data[5]
152 date = datetime.utcfromtimestamp (int (data[4].split (' ')[0])) 140 latesttagdistance = int (data[6])
153 bookmarks = data[5] 141 latesttag = data[7]
154 latesttagdistance = int (data[6]) 142 email = data[8]
155 latesttag = data[7] 143 delta = datetime.utcnow() - date
156 delta = datetime.utcnow() - date 144 datestring = ''
157 datestring = '' 145
158 146 if bookmarks:
159 if bookmarks: 147 bookmarks = HgPoll.prettify_bookmarks (bookmarks)
160 bookmarks = HgPoll.prettify_bookmarks (bookmarks) 148
161 149 # Find out the Zandronum version of this changeset
162 # Find out the Zandronum version of this changeset 150 data = subprocess.check_output (['hg', '--cwd', reponame, 'cat', '--rev', node, 'src/version.h'])
163 repo.hg_command ('revert', '-r', node, 'src/version.h') 151 zanversion = '<unknown version>'
164 zanversion = '<unknown version>' 152
165 153 try:
166 try: 154 regexps = [ \
167 with open (reponame + '/src/version.h') as version_file: 155 re.compile (r'#define\s+GAMEVER_STRING\s+"([^"]+)"'), \
168 regexps = [ \ 156 re.compile (r'#define\s+DOTVERSIONSTR_NOREV\s+"([^"]+)"'), \
169 re.compile (r'#define\s+GAMEVER_STRING\s+"([^"]+)"'), \ 157 re.compile (r'#define\s+DOTVERSIONSTR\s+"([^"]+)"')]
170 re.compile (r'#define\s+DOTVERSIONSTR_NOREV\s+"([^"]+)"'), \ 158
171 re.compile (r'#define\s+DOTVERSIONSTR\s+"([^"]+)"')] 159 for line in data.splitlines():
172 160 for rex in regexps:
173 for line in version_file: 161 match = rex.match (line)
174 for rex in regexps: 162 if match != None:
175 match = rex.match (line) 163 zanversion = match.group (1)
176 if match != None: 164 break
177 zanversion = match.group (1) 165
178 break 166 if match != None:
179 167 break
180 if match != None: 168 except IOError:
181 break 169 pass
182 except IOError: 170
183 pass 171 subprocess.call (['hg', '--cwd', reponame, 'revert', '--all'])
184 172 username = Config.find_developer_by_email (email)
185 repo.hg_command ('revert', '--all') 173
186 174 if username != '':
187 # Remove the email address from the author if possible 175 author = username
188 match = re.compile (r'^(.+) <([^>]+)>$.*').match (author) 176
189 if match: 177 # Try prettify the diffstat
190 author = match.group (1) 178 match = re.match (r'^([0-9]+): \+([0-9]+)/-([0-9]+)$', diffstat)
191 email = match.group (2) 179
192 180 if match:
193 username = Config.find_developer_by_email (email) 181 diffstat = "%s\003:\0033 +%s\003/\0034-%s\003" % (match.group (1), match.group (2), match.group (3))
194 182
195 if username != '': 183 if delta.days < 4:
196 author = username 184 if delta.days == 0:
197 185 if delta.seconds < 60:
198 # Try prettify the diffstat 186 datestring = 'just now'
199 rex = re.compile (r'^([0-9]+): \+([0-9]+)/-([0-9]+)$') 187 elif delta.seconds < 3600:
200 match = rex.match (diffstat) 188 minutes = delta.seconds / 60
201 189 datestring = '%d minute%s ago' % (minutes, plural (minutes))
202 if match:
203 diffstat = "%s\003:\0033 +%s\003/\0034-%s\003" % (match.group (1), match.group (2), match.group (3))
204
205 if delta.days < 4:
206 if delta.days == 0:
207 if delta.seconds < 60:
208 datestring = 'just now'
209 elif delta.seconds < 3600:
210 minutes = delta.seconds / 60
211 datestring = '%d minute%s ago' % (minutes, plural (minutes))
212 else:
213 hours = delta.seconds / 3600
214 datestring = '%d hour%s ago' % (hours, plural (hours))
215 else: 190 else:
216 datestring = '%d day%s ago' % (delta.days, plural (delta.days)) 191 hours = delta.seconds / 3600
192 datestring = '%d hour%s ago' % (hours, plural (hours))
217 else: 193 else:
218 datestring = 'on %s' % (str (date)) 194 datestring = '%d day%s ago' % (delta.days, plural (delta.days))
219 195 else:
220 versionstring = "" 196 datestring = 'on %s' % (str (date))
221 if latesttagdistance != 0: 197
222 versionstring = '%s %s, %d hops from %s' % (zanversion, date.strftime ('%y%m%d-%H%M'), latesttagdistance, latesttag) 198 versionstring = ""
223 else: 199 if latesttagdistance != 0:
224 versionstring = latesttag 200 versionstring = '%s %s, %d hops from %s' % (zanversion, date.strftime ('%y%m%d-%H%M'),
225 201 latesttagdistance, latesttag)
226 reply ('changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \ 202 else:
227 (node, bookmarks, versionstring, author, datestring, diffstat)) 203 versionstring = latesttag
228 204
229 for line in message.split ('\n'): 205 reply ('changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \
230 reply (' ' + line) 206 (node, bookmarks, versionstring, author, datestring, diffstat))
231 207
232 reply ('url: %s/commits/%s' % (repourl, node)) 208 for line in message.split ('\n'):
233 except hgapi.HgException as e: 209 reply (' ' + line)
234 result = HgPoll.decipher_hgapi_error (e) 210
235 211 reply ('url: %s/commits/%s' % (repourl, node))
236 if result[0]:
237 command_error (result[1])
238 else:
239 command_error (`e`)
240 212
241 def cmd_hg (bot, args, reply, **rest): 213 def cmd_hg (bot, args, reply, **rest):
242 try: 214 try:
243 repo = hgapi.Repo (args['repo']) 215 result = subprocess.check_output (['hg'] + args['command'])
244 result = repo.hg_command (*args['command'])
245 reply (result) 216 reply (result)
246 except hgapi.hgapi.HgException as e: 217 except Exception as e:
247 result = HgPoll.decipher_hgapi_error (e) 218 command_error (str (e))
248
249 if result[0]:
250 command_error (result[1])
251 else:
252 command_error (`e`)
253 219
254 def cmd_resolves (bot, args, **rest): 220 def cmd_resolves (bot, args, **rest):
255 HgPoll.announce_ticket_resolved (args['ticket'], args['changeset']) 221 HgPoll.announce_ticket_resolved (args['ticket'], args['changeset'])
256
257 def cmd_rebuildcommitsdb (bot, args, **rest):
258 HgPoll.g_CommitsDb.create_new()
259
260 def cmd_updatezadevtopic (bot, **rest):
261 HgPoll.update_zadev_topic()

mercurial