[pypy-svn] buildbot default: turn irc handling to globals

RonnyPfannschmidt commits-noreply at bitbucket.org
Wed Apr 20 15:53:30 CEST 2011


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: 
Changeset: r466:2ddf5cbdec01
Date: 2011-04-19 19:56 +0200
http://bitbucket.org/pypy/buildbot/changeset/2ddf5cbdec01/

Log:	turn irc handling to globals

diff --git a/bitbucket_hook/hook.py b/bitbucket_hook/hook.py
--- a/bitbucket_hook/hook.py
+++ b/bitbucket_hook/hook.py
@@ -78,18 +78,52 @@
         smtp = SMTP(SMTP_SERVER, SMTP_PORT)
         smtp.sendmail(from_, [to], msg.as_string())
 
+
+def send_irc_message(message, test=False):
+    if test:
+        print message + '\n'
+    else:
+        return subprocess.call([
+            app.config['BOT'], app.config['CHANNEL'], message
+        ])
+
+def handle_irc_message(payload, test=False):
+    commits = get_commits('irc', payload)
+    if test:
+        print "#" * 20
+        print "IRC messages:"
+
+    for commit in commits:
+        author = commit['author']
+        branch = commit['branch']
+        node = commit['node']
+        timestamp = commit.get('timestamp')
+        print '[%s] %s %s %s' % (time.strftime('%Y-%m-%d %H:%M'), node, timestamp, author)
+
+        files = commit.get('files', [])
+        common_prefix, filenames = getpaths(files, app.config['LISTFILES'])
+        pathlen = len(common_prefix) + len(filenames) + 2
+        common_prefix = '/' + common_prefix
+
+        if app.config['USE_COLOR_CODES']:
+            author = '\x0312%s\x0F' % author   # in blue
+            branch = '\x02%s\x0F'   % branch   # in bold
+            node = '\x0311%s\x0F'   % node     # in azure
+            common_prefix = '\x0315%s\x0F' % common_prefix # in gray
+
+        message = commit['message'].replace('\n', ' ')
+        fields = (author, branch, node, common_prefix, filenames)
+        part1 = '%s %s %s %s%s: ' % fields
+        totallen = 160 + pathlen
+        if len(message) + len(part1) <= totallen:
+            irc_msg = part1 + message
+        else:
+            maxlen = totallen - (len(part1) + 3)
+            irc_msg = part1 + message[:maxlen] + '...'
+        send_irc_message(irc_msg, test)
+
 class BitbucketHookHandler(object):
 
-
-
-    def send_irc_message(self, message, test=False):
-        if test:
-            print message + '\n'
-        else:
-            return subprocess.call([
-                app.config['BOT'], app.config['CHANNEL'], message
-            ])
-
     def handle(self, payload, test=False):
         path = payload['repository']['absolute_url']
         self.payload = payload
@@ -99,43 +133,9 @@
             print >> sys.stderr, 'Ignoring unknown repo', path
             return
         hg('pull', '-R', self.local_repo)
-        self.handle_irc_message(test)
+        handle_irc_message(payload, test)
         self.handle_diff_email(test)
 
-    def handle_irc_message(self, test=False):
-        commits = get_commits('irc', self.payload)
-        if test:
-            print "#" * 20
-            print "IRC messages:"
-
-        for commit in commits:
-            author = commit['author']
-            branch = commit['branch']
-            node = commit['node']
-            timestamp = commit.get('timestamp')
-            print '[%s] %s %s %s' % (time.strftime('%Y-%m-%d %H:%M'), node, timestamp, author)
-
-            files = commit.get('files', [])
-            common_prefix, filenames = getpaths(files, app.config['LISTFILES'])
-            pathlen = len(common_prefix) + len(filenames) + 2
-            common_prefix = '/' + common_prefix
-
-            if app.config['USE_COLOR_CODES']:
-                author = '\x0312%s\x0F' % author   # in blue
-                branch = '\x02%s\x0F'   % branch   # in bold
-                node = '\x0311%s\x0F'   % node     # in azure
-                common_prefix = '\x0315%s\x0F' % common_prefix # in gray
-
-            message = commit['message'].replace('\n', ' ')
-            fields = (author, branch, node, common_prefix, filenames)
-            part1 = '%s %s %s %s%s: ' % fields
-            totallen = 160 + pathlen
-            if len(message) + len(part1) <= totallen:
-                irc_msg = part1 + message
-            else:
-                maxlen = totallen - (len(part1) + 3)
-                irc_msg = part1 + message[:maxlen] + '...'
-            self.send_irc_message(irc_msg, test)
 
     def handle_diff_email(self, test=False):
         commits = get_commits('email', self.payload)

diff --git a/bitbucket_hook/test/test_hook.py b/bitbucket_hook/test/test_hook.py
--- a/bitbucket_hook/test/test_hook.py
+++ b/bitbucket_hook/test/test_hook.py
@@ -11,10 +11,6 @@
 
     def __init__(self):
         hook.BitbucketHookHandler.__init__(self)
-        self.messages = []
-
-    def send_irc_message(self, message, test=False):
-        self.messages.append(message)
 
 
 def test_non_ascii_encoding_guess_utf8(monkeypatch):
@@ -96,9 +92,8 @@
     return payload, expected
 
 
-def test_irc_message():
-    handler = BaseHandler()
-    handler.payload = {
+def test_irc_message(monkeypatch, messages):
+    payload = {
         'commits': [{'revision': 42,
                      'branch': u'default',
                      'author': u'antocuni',
@@ -115,16 +110,16 @@
                      }
                     ]}
 
-    handler.payload, expected = irc_cases(handler.payload)
-    handler.handle_irc_message()
+    payload, expected = irc_cases(payload)
+    hook.handle_irc_message(payload)
 
-    msg1, msg2 = handler.messages[:2]
+    msg1, msg2 = messages[:2]
 
     assert msg1 == 'antocuni default abcdef /: this is a test'
     x = 'antocuni mybranch xxxyyy /: %s...' % LONG_CUT
     assert msg2 == x
 
-    for got, wanted in zip(handler.messages[2:], expected):
+    for got, wanted in zip(messages[2:], expected):
         assert got == wanted
 
 def noop(*args, **kwargs): pass
@@ -157,7 +152,7 @@
     handler.handle(test_payload, test=True)
 
 
-def test_ignore_duplicate_commits(monkeypatch, mails):
+def test_ignore_duplicate_commits(monkeypatch, mails, messages):
     def hg( *args):
         return '<hg %s>' % ' '.join(map(str, args))
     monkeypatch.setattr(hook, 'hg', hg)
@@ -178,7 +173,7 @@
     #
     num_commits = len(commits['commits'])
     assert len(mails) == num_commits
-    assert len(handler.messages) == num_commits
+    assert len(messages) == num_commits
 
 
 def test_hg():

diff --git a/bitbucket_hook/test/conftest.py b/bitbucket_hook/test/conftest.py
--- a/bitbucket_hook/test/conftest.py
+++ b/bitbucket_hook/test/conftest.py
@@ -3,6 +3,8 @@
 def pytest_funcarg__mails(request):
     return []
 
+def pytest_funcarg__messages(request):
+    return []
 
 def pytest_funcarg__monkeypatch(request):
     mp =  request.getfuncargvalue('monkeypatch')
@@ -11,6 +13,11 @@
         mails.append((from_, to, subject, body))
     mp.setattr(hook, 'send', send)
 
+    messages = request.getfuncargvalue('messages')
+    def send_irc_message(message, test=False):
+        messages.append(message)
+    mp.setattr(hook, 'send_irc_message', send_irc_message)
+
 
     return mp
 


More information about the Pypy-commit mailing list