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