hook: redirect stdout to stderr for ssh and http servers
authorMatt Mackall <mpm@selenic.com>
Fri, 11 Jan 2008 13:06:38 -0600
changeset 5833 323b9c55b328
parent 5832 2192ed187319
child 5834 5e7a8ea375a6
hook: redirect stdout to stderr for ssh and http servers
mercurial/hgweb/hgweb_mod.py
mercurial/hook.py
mercurial/sshserver.py
--- a/mercurial/hgweb/hgweb_mod.py	Fri Jan 11 06:07:43 2008 +0300
+++ b/mercurial/hgweb/hgweb_mod.py	Fri Jan 11 13:06:38 2008 -0600
@@ -8,7 +8,7 @@
 
 import os, mimetypes, re, mimetools, cStringIO
 from mercurial.node import *
-from mercurial import mdiff, ui, hg, util, archival, patch
+from mercurial import mdiff, ui, hg, util, archival, patch, hook
 from mercurial import revlog, templater
 from common import ErrorResponse, get_mtime, style_map, paritygen, get_contact
 from request import wsgirequest
@@ -85,6 +85,7 @@
         else:
             self.repo = repo
 
+        hook.redirect(True)
         self.mtime = -1
         self.reponame = name
         self.archives = 'zip', 'gz', 'bz2'
--- a/mercurial/hook.py	Fri Jan 11 06:07:43 2008 +0300
+++ b/mercurial/hook.py	Fri Jan 11 13:06:38 2008 -0600
@@ -6,7 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import _
-import util
+import util, os, sys
 
 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
     '''call python hook. hook is callable object, looked up as
@@ -79,8 +79,18 @@
         ui.warn(_('warning: %s hook %s\n') % (name, desc))
     return r
 
+_redirect = False
+def redirect(state):
+    _redirect = state
+
 def hook(ui, repo, name, throw=False, **args):
     r = False
+
+    if _redirect:
+        # temporarily redirect stdout to stderr
+        oldstdout = os.dup(sys.stdout.fileno())
+        os.dup2(sys.stderr.fileno(), sys.stdout.fileno())
+
     hooks = [(hname, cmd) for hname, cmd in ui.configitems("hooks")
              if hname.split(".", 1)[0] == name and cmd]
     hooks.sort()
@@ -94,3 +104,6 @@
             r = _exthook(ui, repo, hname, cmd, args, throw) or r
     return r
 
+    if _redirect:
+        os.dup2(oldstdout, sys.stdout.fileno())
+        os.close(oldstdout)
--- a/mercurial/sshserver.py	Fri Jan 11 06:07:43 2008 +0300
+++ b/mercurial/sshserver.py	Fri Jan 11 13:06:38 2008 -0600
@@ -8,7 +8,7 @@
 
 from i18n import _
 from node import *
-import os, streamclone, sys, tempfile, util
+import os, streamclone, sys, tempfile, util, hook
 
 class sshserver(object):
     def __init__(self, ui, repo):
@@ -18,6 +18,7 @@
         self.fin = sys.stdin
         self.fout = sys.stdout
 
+        hook.redirect(True)
         sys.stdout = sys.stderr
 
         # Prevent insertion/deletion of CRs