##// END OF EJS Templates
extensions: fix load of module/__init__.py on OS X
Brendan Cully -
r4580:b1716a8b default
parent child Browse files
Show More
@@ -1,75 +1,76 b''
1 # extensions.py - extension handling for mercurial
1 # extensions.py - extension handling for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
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 import imp, os
8 import imp, os
9 import commands, hg, util, sys
9 import commands, hg, util, sys
10 from i18n import _
10 from i18n import _
11
11
12 _extensions = {}
12 _extensions = {}
13
13
14 def find(name):
14 def find(name):
15 '''return module with given extension name'''
15 '''return module with given extension name'''
16 try:
16 try:
17 return _extensions[name]
17 return _extensions[name]
18 except KeyError:
18 except KeyError:
19 for k, v in _extensions.iteritems():
19 for k, v in _extensions.iteritems():
20 if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
20 if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
21 return sys.modules[v]
21 return sys.modules[v]
22 raise KeyError(name)
22 raise KeyError(name)
23
23
24 def load(ui, name, path):
24 def load(ui, name, path):
25 if name in _extensions:
25 if name in _extensions:
26 return
26 return
27 if path:
27 if path:
28 # the module will be loaded in sys.modules
28 # the module will be loaded in sys.modules
29 # choose an unique name so that it doesn't
29 # choose an unique name so that it doesn't
30 # conflicts with other modules
30 # conflicts with other modules
31 module_name = "hgext_%s" % name.replace('.', '_')
31 module_name = "hgext_%s" % name.replace('.', '_')
32 if os.path.isdir(path):
32 if os.path.isdir(path):
33 # module/__init__.py style
33 # module/__init__.py style
34 fd, fpath, desc = imp.find_module('', [path])
34 d, f = os.path.split(path)
35 fd, fpath, desc = imp.find_module(f, [d])
35 mod = imp.load_module(module_name, fd, fpath, desc)
36 mod = imp.load_module(module_name, fd, fpath, desc)
36 else:
37 else:
37 mod = imp.load_source(module_name, path)
38 mod = imp.load_source(module_name, path)
38 else:
39 else:
39 def importh(name):
40 def importh(name):
40 mod = __import__(name)
41 mod = __import__(name)
41 components = name.split('.')
42 components = name.split('.')
42 for comp in components[1:]:
43 for comp in components[1:]:
43 mod = getattr(mod, comp)
44 mod = getattr(mod, comp)
44 return mod
45 return mod
45 try:
46 try:
46 mod = importh("hgext.%s" % name)
47 mod = importh("hgext.%s" % name)
47 except ImportError:
48 except ImportError:
48 mod = importh(name)
49 mod = importh(name)
49 _extensions[name] = mod
50 _extensions[name] = mod
50
51
51 uisetup = getattr(mod, 'uisetup', None)
52 uisetup = getattr(mod, 'uisetup', None)
52 if uisetup:
53 if uisetup:
53 uisetup(ui)
54 uisetup(ui)
54 reposetup = getattr(mod, 'reposetup', None)
55 reposetup = getattr(mod, 'reposetup', None)
55 if reposetup:
56 if reposetup:
56 hg.repo_setup_hooks.append(reposetup)
57 hg.repo_setup_hooks.append(reposetup)
57 cmdtable = getattr(mod, 'cmdtable', {})
58 cmdtable = getattr(mod, 'cmdtable', {})
58 overrides = [cmd for cmd in cmdtable if cmd in commands.table]
59 overrides = [cmd for cmd in cmdtable if cmd in commands.table]
59 if overrides:
60 if overrides:
60 ui.warn(_("extension '%s' overrides commands: %s\n")
61 ui.warn(_("extension '%s' overrides commands: %s\n")
61 % (name, " ".join(overrides)))
62 % (name, " ".join(overrides)))
62 commands.table.update(cmdtable)
63 commands.table.update(cmdtable)
63
64
64 def loadall(ui):
65 def loadall(ui):
65 for name, path in ui.extensions():
66 for name, path in ui.extensions():
66 try:
67 try:
67 load(ui, name, path)
68 load(ui, name, path)
68 except (util.SignalInterrupt, KeyboardInterrupt):
69 except (util.SignalInterrupt, KeyboardInterrupt):
69 raise
70 raise
70 except Exception, inst:
71 except Exception, inst:
71 ui.warn(_("*** failed to import extension %s: %s\n") %
72 ui.warn(_("*** failed to import extension %s: %s\n") %
72 (name, inst))
73 (name, inst))
73 if ui.print_exc():
74 if ui.print_exc():
74 return 1
75 return 1
75
76
General Comments 0
You need to be logged in to leave comments. Login now