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