Bug 551557: Change hook script to use inline python; Setup master_hook (r=brbaker)
authorChris Peyer <cpeyer@adobe.com>
Thu, 22 Apr 2010 10:42:52 -0700
changeset 36 23a6c22c059e82e438b288260dcc14f16e0b2fe9
parent 35 7ca7b2abb43a2110eba79767734da25dc13e662a
child 37 c72258440ddb0eb1d65bc0076dbc3e36fa4b8843
push id3
push usercpeyer@adobe.com
push dateThu, 22 Apr 2010 17:43:07 +0000
reviewersbrbaker
bugs551557
Bug 551557: Change hook script to use inline python; Setup master_hook (r=brbaker)
tamarin-hook.py
--- a/tamarin-hook.py
+++ b/tamarin-hook.py
@@ -33,52 +33,77 @@
 #  and other provisions required by the GPL or the LGPL. If you do not delete
 #  the provisions above, a recipient may use your version of this file under
 #  the terms of any one of the MPL, the GPL or the LGPL.
 # 
 #  ***** END LICENSE BLOCK ****
 #
 #  Hook script used by tamarin team on tamarin-redux and tamarin-central.
 #
+# For documentation on hook scripts see:
+#   http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html
+#   http://mercurial.selenic.com/wiki/MercurialApi
 
-if __name__ == '__main__':
-    import os, sys, commands, re, platform
+
+import sys, re
+from mercurial import hg, ui, commands, node
+
 
-    HG_NODE=''
-    if os.environ.has_key("HG_NODE"):
-        HG_NODE = os.environ["HG_NODE"]
-    if HG_NODE == '':
-        print("ERROR: unknown HG_NODE")
-        sys.exit(1)
+# _quiet function from hghooklib by Johann Duscher
+# http://code.google.com/p/hghooklib/
+def _quiet(ui, fn):
+    oldQuiet = ui.quiet
+    ui.quiet = True
+    result = fn()
+    ui.quiet = oldQuiet
+    return result
 
-    # Look for known unwanted changeids
-    status, nodes = commands.getstatusoutput("hg log -r %s:tip --template '{node}\n'" % HG_NODE)
-    # if hg log fails, exit but do not block the commit
-    if status != 0:
-        print("hg log failed, hook exiting")
-        sys.exit(0)
-    for node in nodes.split():
-        if node.startswith("126c6ef95f51"):
-            sys.exit("blacklisted changeid found")
-        if node.startswith("66eb823ce125"):
-            sys.exit("blacklisted changeid found")
+def master_hook(ui, repo, **kwargs):
+    ui.debug('running tamarin master_hook\n')
+    ui.debug('kwargs: %s\n' % kwargs)
+    # The mercurial hook script expects the equivalent of an exit code back from
+    # this call:
+    #   False = 0 = No Error : allow push
+    #   True = 1 = Error : abort push
+    error = False
+    error = security_check(ui, repo, **kwargs) or error
+    return error
 
+def security_check(ui, repo, **kwargs):
+    ui.debug('running security_check\n')
+    error = False
+    
+    ui.pushbuffer()
+    _quiet(ui, lambda: commands.log(ui, repo, rev=['%s:tip' % kwargs['node']],
+                                    template='{node}\n', date=None, user=None,
+                                    logfile=None))
+    nodes = ui.popbuffer().split('\n')
+    
+    
+    for node in nodes:
+        if node.startswith('126c6ef95f51') or node.startswith('66eb823ce125'):
+            ui.warn('blacklisted changeid found: node %s is blacklisted\n' % node)
+            error = True   # fail the push
+    
     # Look for blacklisted bugs
     blacklist = [
         548077,548842,547258,548098,441280,550269,535446,524263,517679,507624,
         520912,525521,537979,542383,555540,558175,555446,552192,556543,545652,
         555052,551051,550269,555059,507624,520912,525521,535446,537979,542383,
         521270,524263,555097,503358,548842,517679,547258,510070,548098,491355,
         555608,441280,548077,482278,551170,519269,477891,481162,481934,553648,
         'Bug 555610: Add regression testcase'
         ]
-
+    
     bugs = re.compile('(%s)' % '|'.join([str(bug) for bug in blacklist]))
-    status, descs = commands.getstatusoutput("hg log -r %s:tip --template '{desc}\n'" % HG_NODE)
-    # if hg log fails, exit but do not block the commit
-    if status != 0:
-        print("hg log failed, hook exiting")
-        sys.exit(0)
-    for line in descs.split('\n'):
-        if bugs.search(line):
-            sys.exit("blacklisted bug found")
+    
+    ui.pushbuffer()
+    _quiet(ui, lambda: commands.log(ui, repo, rev=['%s:tip' % kwargs['node']],
+                                    template='{desc}', date=None, user=None,
+                                    logfile=None))
+    descs = ui.popbuffer()
     
-    sys.exit(0)
+    searchDescs = bugs.search(descs)
+    if searchDescs:
+        ui.warn('blacklisted bug found: %s\n' % searchDescs.groups()[0])
+        error = True
+    
+    return error
\ No newline at end of file