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