# HG changeset patch
# User Steve Borho <steve@borho.org>
# Date 2009-08-06 02:45:54
# Node ID 872d49dd577ae52a08716b2bc4456abb42dddb0f
# Parent  fb66a7d3f28f69e96369b8d69fc6d775837b40e4

hook: fix full path imports on Windows (issue1779)

Bottom portion fixes full path imports on source installs on Windows.
The top portion further fixes full path imports on binary installs.

Initial patch by Roman V. Kiseliov

diff --git a/mercurial/hook.py b/mercurial/hook.py
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -27,6 +27,13 @@ def _pythonhook(ui, repo, name, hname, f
             raise util.Abort(_('%s hook is invalid ("%s" not in '
                                'a module)') % (hname, funcname))
         modname = funcname[:d]
+        oldpaths = sys.path[:]
+        if hasattr(sys, "frozen"):
+            # binary installs require sys.path manipulation
+            path, name = os.path.split(modname)
+            if path and name:
+                sys.path.append(path)
+                modname = name
         try:
             obj = __import__(modname)
         except ImportError:
@@ -37,6 +44,7 @@ def _pythonhook(ui, repo, name, hname, f
                 raise util.Abort(_('%s hook is invalid '
                                    '(import of "%s" failed)') %
                                  (hname, modname))
+        sys.path = oldpaths
         try:
             for p in funcname.split('.')[1:]:
                 obj = getattr(obj, p)
@@ -110,9 +118,9 @@ def hook(ui, repo, name, throw=False, **
             if hasattr(cmd, '__call__'):
                 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r
             elif cmd.startswith('python:'):
-                if cmd.count(':') == 2:
-                    path, cmd = cmd[7:].split(':')
-                    mod = extensions.loadpath(path, 'hgkook.%s' % hname)
+                if cmd.count(':') >= 2:
+                    path, cmd = cmd[7:].rsplit(':', 1)
+                    mod = extensions.loadpath(path, 'hghook.%s' % hname)
                     hookfn = getattr(mod, cmd)
                 else:
                     hookfn = cmd[7:].strip()