author | Brendan Cully <brendan@kublai.com> |
Wed, 13 Jun 2007 13:46:40 -0700 | |
changeset 4569 | 622d8ed78b47 |
parent 4558 | 9338be783398 |
child 4580 | b1716a8b32d3 |
permissions | -rw-r--r-- |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 |
# extensions.py - extension handling for mercurial |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 |
# |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 |
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 |
# |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
|
4569
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
8 |
import imp, os |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
9 |
import commands, hg, util, sys |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 |
from i18n import _ |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
|
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 |
_extensions = {} |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 |
|
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
def find(name): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
'''return module with given extension name''' |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 |
try: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
return _extensions[name] |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
except KeyError: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 |
for k, v in _extensions.iteritems(): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 |
if k.endswith('.' + name) or k.endswith('/' + name) or v == name: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 |
return sys.modules[v] |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 |
raise KeyError(name) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 |
|
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 |
def load(ui, name, path): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
if name in _extensions: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 |
return |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 |
if path: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
# the module will be loaded in sys.modules |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 |
# choose an unique name so that it doesn't |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 |
# conflicts with other modules |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 |
module_name = "hgext_%s" % name.replace('.', '_') |
4569
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
32 |
if os.path.isdir(path): |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
33 |
# module/__init__.py style |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
34 |
fd, fpath, desc = imp.find_module('', [path]) |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
35 |
mod = imp.load_module(module_name, fd, fpath, desc) |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
36 |
else: |
622d8ed78b47
extensions: load modules in module/__init__.py form.
Brendan Cully <brendan@kublai.com>
parents:
4558
diff
changeset
|
37 |
mod = imp.load_source(module_name, path) |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
else: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 |
def importh(name): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 |
mod = __import__(name) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
components = name.split('.') |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 |
for comp in components[1:]: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 |
mod = getattr(mod, comp) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 |
return mod |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 |
try: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
mod = importh("hgext.%s" % name) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
except ImportError: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 |
mod = importh(name) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
_extensions[name] = mod |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 |
|
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
uisetup = getattr(mod, 'uisetup', None) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
if uisetup: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 |
uisetup(ui) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
reposetup = getattr(mod, 'reposetup', None) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
if reposetup: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 |
hg.repo_setup_hooks.append(reposetup) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
cmdtable = getattr(mod, 'cmdtable', {}) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 |
overrides = [cmd for cmd in cmdtable if cmd in commands.table] |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
if overrides: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 |
ui.warn(_("extension '%s' overrides commands: %s\n") |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 |
% (name, " ".join(overrides))) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 |
commands.table.update(cmdtable) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
|
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 |
def loadall(ui): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
for name, path in ui.extensions(): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 |
try: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
load(ui, name, path) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
except (util.SignalInterrupt, KeyboardInterrupt): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
raise |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 |
except Exception, inst: |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
ui.warn(_("*** failed to import extension %s: %s\n") % |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
(name, inst)) |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
if ui.print_exc(): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 |
return 1 |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |