Module detection cleaner and more reliable self_initializing_hook
authorChristian Ebert <blacktrash@gmx.net>
Thu, 11 Jan 2007 10:37:50 +0100
branchself_initializing_hook
changeset 88 ffec227fced3
parent 87 c193da9eb3e4
child 89 16f1a5ed70ce
Module detection cleaner and more reliable Stolen from mercurial.lsprof
hgkw/keyword.py
--- a/hgkw/keyword.py	Thu Jan 11 02:22:56 2007 +0100
+++ b/hgkw/keyword.py	Thu Jan 11 10:37:50 2007 +0100
@@ -130,15 +130,19 @@
     filelog.filelog = kwfilelog
     repo.__class__ = kwrepo
     # make pretxncommit hook import kwmodule regardless of where it's located
-    moddir = os.path.abspath(os.path.dirname(__file__))
-    if os.path.dirname(moddir) in sys.path:
-        moddir = os.path.split(moddir)[1]+'.'
+    for k, v in sys.modules.iteritems():
+        if v is None:
+            continue
+        if not hasattr(v, '__file__'):
+            continue
+        if v.__file__.startswith(__file__):
+            mod = k
+            break
     else:
-        sys.path.insert(0, moddir)
-        moddir = ''
-    ui.setconfig('hooks', 'pretxncommit.keyword', 'python:%s%s.pretxnkw'
-            % (moddir, os.path.splitext(os.path.basename(__file__))[0]))
-    del moddir
+        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+        mod = os.path.splitext(os.path.basename(__file__))[0]
+    ui.setconfig('hooks', 'pretxncommit.keyword', 'python:%s.pretxnkw' % mod)
+    del mod
 
 
 def pretxnkw(ui, repo, hooktype, **args):