Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hook.py @ 5833:323b9c55b328
hook: redirect stdout to stderr for ssh and http servers
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 11 Jan 2008 13:06:38 -0600 |
parents | fff50306e6dd |
children | 2c565b9598b8 |
comparison
equal
deleted
inserted
replaced
5832:2192ed187319 | 5833:323b9c55b328 |
---|---|
4 # | 4 # |
5 # This software may be used and distributed according to the terms | 5 # This software may be used and distributed according to the terms |
6 # of the GNU General Public License, incorporated herein by reference. | 6 # of the GNU General Public License, incorporated herein by reference. |
7 | 7 |
8 from i18n import _ | 8 from i18n import _ |
9 import util | 9 import util, os, sys |
10 | 10 |
11 def _pythonhook(ui, repo, name, hname, funcname, args, throw): | 11 def _pythonhook(ui, repo, name, hname, funcname, args, throw): |
12 '''call python hook. hook is callable object, looked up as | 12 '''call python hook. hook is callable object, looked up as |
13 name in python module. if callable returns "true", hook | 13 name in python module. if callable returns "true", hook |
14 fails, else passes. if hook raises exception, treated as | 14 fails, else passes. if hook raises exception, treated as |
77 if throw: | 77 if throw: |
78 raise util.Abort(_('%s hook %s') % (name, desc)) | 78 raise util.Abort(_('%s hook %s') % (name, desc)) |
79 ui.warn(_('warning: %s hook %s\n') % (name, desc)) | 79 ui.warn(_('warning: %s hook %s\n') % (name, desc)) |
80 return r | 80 return r |
81 | 81 |
82 _redirect = False | |
83 def redirect(state): | |
84 _redirect = state | |
85 | |
82 def hook(ui, repo, name, throw=False, **args): | 86 def hook(ui, repo, name, throw=False, **args): |
83 r = False | 87 r = False |
88 | |
89 if _redirect: | |
90 # temporarily redirect stdout to stderr | |
91 oldstdout = os.dup(sys.stdout.fileno()) | |
92 os.dup2(sys.stderr.fileno(), sys.stdout.fileno()) | |
93 | |
84 hooks = [(hname, cmd) for hname, cmd in ui.configitems("hooks") | 94 hooks = [(hname, cmd) for hname, cmd in ui.configitems("hooks") |
85 if hname.split(".", 1)[0] == name and cmd] | 95 if hname.split(".", 1)[0] == name and cmd] |
86 hooks.sort() | 96 hooks.sort() |
87 for hname, cmd in hooks: | 97 for hname, cmd in hooks: |
88 if callable(cmd): | 98 if callable(cmd): |
92 args, throw) or r | 102 args, throw) or r |
93 else: | 103 else: |
94 r = _exthook(ui, repo, hname, cmd, args, throw) or r | 104 r = _exthook(ui, repo, hname, cmd, args, throw) or r |
95 return r | 105 return r |
96 | 106 |
107 if _redirect: | |
108 os.dup2(oldstdout, sys.stdout.fileno()) | |
109 os.close(oldstdout) |