Show More
@@ -1,114 +1,115 | |||
|
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, cmdutil |
|
10 | 10 | from i18n import _ |
|
11 | 11 | |
|
12 | 12 | _extensions = {} |
|
13 | 13 | _order = [] |
|
14 | 14 | |
|
15 | 15 | def extensions(): |
|
16 | 16 | for name in _order: |
|
17 | 17 | module = _extensions[name] |
|
18 | 18 | if module: |
|
19 | 19 | yield name, module |
|
20 | 20 | |
|
21 | 21 | def find(name): |
|
22 | 22 | '''return module with given extension name''' |
|
23 | 23 | try: |
|
24 | 24 | return _extensions[name] |
|
25 | 25 | except KeyError: |
|
26 | 26 | for k, v in _extensions.iteritems(): |
|
27 | 27 | if k.endswith('.' + name) or k.endswith('/' + name): |
|
28 | 28 | return v |
|
29 | 29 | raise KeyError(name) |
|
30 | 30 | |
|
31 | 31 | def load(ui, name, path): |
|
32 | 32 | if name.startswith('hgext.') or name.startswith('hgext/'): |
|
33 | 33 | shortname = name[6:] |
|
34 | 34 | else: |
|
35 | 35 | shortname = name |
|
36 | 36 | if shortname in _extensions: |
|
37 | 37 | return |
|
38 | 38 | _extensions[shortname] = None |
|
39 | 39 | if path: |
|
40 | 40 | # the module will be loaded in sys.modules |
|
41 | 41 | # choose an unique name so that it doesn't |
|
42 | 42 | # conflicts with other modules |
|
43 | 43 | module_name = "hgext_%s" % name.replace('.', '_') |
|
44 | 44 | if os.path.isdir(path): |
|
45 | 45 | # module/__init__.py style |
|
46 | 46 | d, f = os.path.split(path) |
|
47 | 47 | fd, fpath, desc = imp.find_module(f, [d]) |
|
48 | 48 | mod = imp.load_module(module_name, fd, fpath, desc) |
|
49 | 49 | else: |
|
50 | 50 | mod = imp.load_source(module_name, path) |
|
51 | 51 | else: |
|
52 | 52 | def importh(name): |
|
53 | 53 | mod = __import__(name) |
|
54 | 54 | components = name.split('.') |
|
55 | 55 | for comp in components[1:]: |
|
56 | 56 | mod = getattr(mod, comp) |
|
57 | 57 | return mod |
|
58 | 58 | try: |
|
59 | 59 | mod = importh("hgext.%s" % name) |
|
60 | 60 | except ImportError: |
|
61 | 61 | mod = importh(name) |
|
62 | 62 | _extensions[shortname] = mod |
|
63 | 63 | _order.append(shortname) |
|
64 | 64 | |
|
65 | 65 | uisetup = getattr(mod, 'uisetup', None) |
|
66 | 66 | if uisetup: |
|
67 | 67 | uisetup(ui) |
|
68 | 68 | |
|
69 | 69 | def loadall(ui): |
|
70 | 70 | result = ui.configitems("extensions") |
|
71 | 71 | for i, (name, path) in enumerate(result): |
|
72 | 72 | if path: |
|
73 | 73 | if path[0] == '!': |
|
74 | 74 | continue |
|
75 | 75 | path = os.path.expanduser(path) |
|
76 | 76 | try: |
|
77 | 77 | load(ui, name, path) |
|
78 | 78 | except (util.SignalInterrupt, KeyboardInterrupt): |
|
79 | 79 | raise |
|
80 | 80 | except Exception, inst: |
|
81 | 81 | if path: |
|
82 | 82 | ui.warn(_("*** failed to import extension %s from %s: %s\n") |
|
83 | 83 | % (name, path, inst)) |
|
84 | 84 | else: |
|
85 | 85 | ui.warn(_("*** failed to import extension %s: %s\n") |
|
86 | 86 | % (name, inst)) |
|
87 | 87 | if ui.print_exc(): |
|
88 | 88 | return 1 |
|
89 | 89 | |
|
90 | 90 | def wrapcommand(table, command, wrapper): |
|
91 | 91 | aliases, entry = cmdutil.findcmd(command, table) |
|
92 | 92 | for alias, e in table.iteritems(): |
|
93 | 93 | if e is entry: |
|
94 | 94 | key = alias |
|
95 | 95 | break |
|
96 | 96 | |
|
97 | 97 | origfn = entry[0] |
|
98 | 98 | def wrap(*args, **kwargs): |
|
99 | 99 | return wrapper(origfn, *args, **kwargs) |
|
100 | 100 | |
|
101 | 101 | wrap.__doc__ = getattr(origfn, '__doc__') |
|
102 | wrap.__module__ = getattr(origfn, '__module__') | |
|
102 | 103 | |
|
103 | 104 | newentry = list(entry) |
|
104 | 105 | newentry[0] = wrap |
|
105 | 106 | table[key] = tuple(newentry) |
|
106 | 107 | return entry |
|
107 | 108 | |
|
108 | 109 | def wrapfunction(container, funcname, wrapper): |
|
109 | 110 | def wrap(*args, **kwargs): |
|
110 | 111 | return wrapper(origfn, *args, **kwargs) |
|
111 | 112 | |
|
112 | 113 | origfn = getattr(container, funcname) |
|
113 | 114 | setattr(container, funcname, wrap) |
|
114 | 115 | return origfn |
General Comments 0
You need to be logged in to leave comments.
Login now