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