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