Mercurial > public > mercurial-scm > hg
comparison mercurial/localrepo.py @ 4070:961ccb615cf7
Handle functions as the value of a hooks.<name> config variable
This should make it easier for extensions to add a hook when
they're loaded.
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Fri, 09 Feb 2007 03:48:28 -0200 |
parents | 82eb0fafb56d |
children | 35b39097c3e6 |
comparison
equal
deleted
inserted
replaced
4069:3fef134832d8 | 4070:961ccb615cf7 |
---|---|
140 reason for "true" meaning "hook failed" is so that | 140 reason for "true" meaning "hook failed" is so that |
141 unmodified commands (e.g. mercurial.commands.update) can | 141 unmodified commands (e.g. mercurial.commands.update) can |
142 be run as hooks without wrappers to convert return values.''' | 142 be run as hooks without wrappers to convert return values.''' |
143 | 143 |
144 self.ui.note(_("calling hook %s: %s\n") % (hname, funcname)) | 144 self.ui.note(_("calling hook %s: %s\n") % (hname, funcname)) |
145 d = funcname.rfind('.') | 145 obj = funcname |
146 if d == -1: | 146 if not callable(obj): |
147 raise util.Abort(_('%s hook is invalid ("%s" not in a module)') | 147 d = funcname.rfind('.') |
148 % (hname, funcname)) | 148 if d == -1: |
149 modname = funcname[:d] | 149 raise util.Abort(_('%s hook is invalid ("%s" not in ' |
150 try: | 150 'a module)') % (hname, funcname)) |
151 obj = __import__(modname) | 151 modname = funcname[:d] |
152 except ImportError: | |
153 try: | 152 try: |
154 # extensions are loaded with hgext_ prefix | 153 obj = __import__(modname) |
155 obj = __import__("hgext_%s" % modname) | |
156 except ImportError: | 154 except ImportError: |
155 try: | |
156 # extensions are loaded with hgext_ prefix | |
157 obj = __import__("hgext_%s" % modname) | |
158 except ImportError: | |
159 raise util.Abort(_('%s hook is invalid ' | |
160 '(import of "%s" failed)') % | |
161 (hname, modname)) | |
162 try: | |
163 for p in funcname.split('.')[1:]: | |
164 obj = getattr(obj, p) | |
165 except AttributeError, err: | |
157 raise util.Abort(_('%s hook is invalid ' | 166 raise util.Abort(_('%s hook is invalid ' |
158 '(import of "%s" failed)') % | 167 '("%s" is not defined)') % |
159 (hname, modname)) | 168 (hname, funcname)) |
160 try: | 169 if not callable(obj): |
161 for p in funcname.split('.')[1:]: | 170 raise util.Abort(_('%s hook is invalid ' |
162 obj = getattr(obj, p) | 171 '("%s" is not callable)') % |
163 except AttributeError, err: | 172 (hname, funcname)) |
164 raise util.Abort(_('%s hook is invalid ' | |
165 '("%s" is not defined)') % | |
166 (hname, funcname)) | |
167 if not callable(obj): | |
168 raise util.Abort(_('%s hook is invalid ' | |
169 '("%s" is not callable)') % | |
170 (hname, funcname)) | |
171 try: | 173 try: |
172 r = obj(ui=self.ui, repo=self, hooktype=name, **args) | 174 r = obj(ui=self.ui, repo=self, hooktype=name, **args) |
173 except (KeyboardInterrupt, util.SignalInterrupt): | 175 except (KeyboardInterrupt, util.SignalInterrupt): |
174 raise | 176 raise |
175 except Exception, exc: | 177 except Exception, exc: |
203 r = False | 205 r = False |
204 hooks = [(hname, cmd) for hname, cmd in self.ui.configitems("hooks") | 206 hooks = [(hname, cmd) for hname, cmd in self.ui.configitems("hooks") |
205 if hname.split(".", 1)[0] == name and cmd] | 207 if hname.split(".", 1)[0] == name and cmd] |
206 hooks.sort() | 208 hooks.sort() |
207 for hname, cmd in hooks: | 209 for hname, cmd in hooks: |
208 if cmd.startswith('python:'): | 210 if callable(cmd): |
211 r = callhook(hname, cmd) or r | |
212 elif cmd.startswith('python:'): | |
209 r = callhook(hname, cmd[7:].strip()) or r | 213 r = callhook(hname, cmd[7:].strip()) or r |
210 else: | 214 else: |
211 r = runhook(hname, cmd) or r | 215 r = runhook(hname, cmd) or r |
212 return r | 216 return r |
213 | 217 |