##// END OF EJS Templates
extensions: load modules in module/__init__.py form....
Brendan Cully -
r4569:622d8ed7 default
parent child Browse files
Show More
@@ -1,69 +1,75 b''
1 # extensions.py - extension handling for mercurial
1 # extensions.py - extension handling for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 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, commands, hg, util, sys
8 import imp, os
9 import commands, hg, util, sys
9 from i18n import _
10 from i18n import _
10
11
11 _extensions = {}
12 _extensions = {}
12
13
13 def find(name):
14 def find(name):
14 '''return module with given extension name'''
15 '''return module with given extension name'''
15 try:
16 try:
16 return _extensions[name]
17 return _extensions[name]
17 except KeyError:
18 except KeyError:
18 for k, v in _extensions.iteritems():
19 for k, v in _extensions.iteritems():
19 if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
20 if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
20 return sys.modules[v]
21 return sys.modules[v]
21 raise KeyError(name)
22 raise KeyError(name)
22
23
23 def load(ui, name, path):
24 def load(ui, name, path):
24 if name in _extensions:
25 if name in _extensions:
25 return
26 return
26 if path:
27 if path:
27 # the module will be loaded in sys.modules
28 # the module will be loaded in sys.modules
28 # choose an unique name so that it doesn't
29 # choose an unique name so that it doesn't
29 # conflicts with other modules
30 # conflicts with other modules
30 module_name = "hgext_%s" % name.replace('.', '_')
31 module_name = "hgext_%s" % name.replace('.', '_')
31 mod = imp.load_source(module_name, path)
32 if os.path.isdir(path):
33 # module/__init__.py style
34 fd, fpath, desc = imp.find_module('', [path])
35 mod = imp.load_module(module_name, fd, fpath, desc)
36 else:
37 mod = imp.load_source(module_name, path)
32 else:
38 else:
33 def importh(name):
39 def importh(name):
34 mod = __import__(name)
40 mod = __import__(name)
35 components = name.split('.')
41 components = name.split('.')
36 for comp in components[1:]:
42 for comp in components[1:]:
37 mod = getattr(mod, comp)
43 mod = getattr(mod, comp)
38 return mod
44 return mod
39 try:
45 try:
40 mod = importh("hgext.%s" % name)
46 mod = importh("hgext.%s" % name)
41 except ImportError:
47 except ImportError:
42 mod = importh(name)
48 mod = importh(name)
43 _extensions[name] = mod
49 _extensions[name] = mod
44
50
45 uisetup = getattr(mod, 'uisetup', None)
51 uisetup = getattr(mod, 'uisetup', None)
46 if uisetup:
52 if uisetup:
47 uisetup(ui)
53 uisetup(ui)
48 reposetup = getattr(mod, 'reposetup', None)
54 reposetup = getattr(mod, 'reposetup', None)
49 if reposetup:
55 if reposetup:
50 hg.repo_setup_hooks.append(reposetup)
56 hg.repo_setup_hooks.append(reposetup)
51 cmdtable = getattr(mod, 'cmdtable', {})
57 cmdtable = getattr(mod, 'cmdtable', {})
52 overrides = [cmd for cmd in cmdtable if cmd in commands.table]
58 overrides = [cmd for cmd in cmdtable if cmd in commands.table]
53 if overrides:
59 if overrides:
54 ui.warn(_("extension '%s' overrides commands: %s\n")
60 ui.warn(_("extension '%s' overrides commands: %s\n")
55 % (name, " ".join(overrides)))
61 % (name, " ".join(overrides)))
56 commands.table.update(cmdtable)
62 commands.table.update(cmdtable)
57
63
58 def loadall(ui):
64 def loadall(ui):
59 for name, path in ui.extensions():
65 for name, path in ui.extensions():
60 try:
66 try:
61 load(ui, name, path)
67 load(ui, name, path)
62 except (util.SignalInterrupt, KeyboardInterrupt):
68 except (util.SignalInterrupt, KeyboardInterrupt):
63 raise
69 raise
64 except Exception, inst:
70 except Exception, inst:
65 ui.warn(_("*** failed to import extension %s: %s\n") %
71 ui.warn(_("*** failed to import extension %s: %s\n") %
66 (name, inst))
72 (name, inst))
67 if ui.print_exc():
73 if ui.print_exc():
68 return 1
74 return 1
69
75
@@ -1,45 +1,55 b''
1 #!/bin/sh
1 #!/bin/sh
2 # Test basic extension support
2 # Test basic extension support
3
3
4 cat > foobar.py <<EOF
4 cat > foobar.py <<EOF
5 import os
5 import os
6 from mercurial import commands
6 from mercurial import commands
7
7
8 def uisetup(ui):
8 def uisetup(ui):
9 ui.write("uisetup called\\n")
9 ui.write("uisetup called\\n")
10 ui.write("ui.parentui is%s None\\n" % (ui.parentui is not None
10 ui.write("ui.parentui is%s None\\n" % (ui.parentui is not None
11 and "not" or ""))
11 and "not" or ""))
12
12
13 def reposetup(ui, repo):
13 def reposetup(ui, repo):
14 ui.write("reposetup called for %s\\n" % os.path.basename(repo.root))
14 ui.write("reposetup called for %s\\n" % os.path.basename(repo.root))
15 ui.write("ui %s= repo.ui\\n" % (ui == repo.ui and "=" or "!"))
15 ui.write("ui %s= repo.ui\\n" % (ui == repo.ui and "=" or "!"))
16
16
17 def foo(ui, *args, **kwargs):
17 def foo(ui, *args, **kwargs):
18 ui.write("Foo\\n")
18 ui.write("Foo\\n")
19
19
20 def bar(ui, *args, **kwargs):
20 def bar(ui, *args, **kwargs):
21 ui.write("Bar\\n")
21 ui.write("Bar\\n")
22
22
23 cmdtable = {
23 cmdtable = {
24 "foo": (foo, [], "hg foo"),
24 "foo": (foo, [], "hg foo"),
25 "bar": (bar, [], "hg bar"),
25 "bar": (bar, [], "hg bar"),
26 }
26 }
27
27
28 commands.norepo += ' bar'
28 commands.norepo += ' bar'
29 EOF
29 EOF
30 abspath=`pwd`/foobar.py
30 abspath=`pwd`/foobar.py
31
31
32 mkdir barfoo
33 cp foobar.py barfoo/__init__.py
34 barfoopath=`pwd`/barfoo
35
32 hg init a
36 hg init a
33 cd a
37 cd a
34 echo foo > file
38 echo foo > file
35 hg add file
39 hg add file
36 hg commit -m 'add file'
40 hg commit -m 'add file'
37
41
38 echo '[extensions]' >> $HGRCPATH
42 echo '[extensions]' >> $HGRCPATH
39 echo "foobar = $abspath" >> $HGRCPATH
43 echo "foobar = $abspath" >> $HGRCPATH
40 hg foo
44 hg foo
41
45
42 cd ..
46 cd ..
43 hg clone a b
47 hg clone a b
44
48
45 hg bar
49 hg bar
50
51 echo '% module/__init__.py-style'
52 echo '[extensions]' > $HGRCPATH
53 echo "barfoo = $barfoopath" >> $HGRCPATH
54 cd a
55 hg foo
@@ -1,15 +1,21 b''
1 uisetup called
1 uisetup called
2 ui.parentui is None
2 ui.parentui is None
3 reposetup called for a
3 reposetup called for a
4 ui == repo.ui
4 ui == repo.ui
5 Foo
5 Foo
6 uisetup called
6 uisetup called
7 ui.parentui is None
7 ui.parentui is None
8 reposetup called for a
8 reposetup called for a
9 ui == repo.ui
9 ui == repo.ui
10 reposetup called for b
10 reposetup called for b
11 ui == repo.ui
11 ui == repo.ui
12 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
12 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
13 uisetup called
13 uisetup called
14 ui.parentui is None
14 ui.parentui is None
15 Bar
15 Bar
16 % module/__init__.py-style
17 uisetup called
18 ui.parentui is None
19 reposetup called for a
20 ui == repo.ui
21 Foo
General Comments 0
You need to be logged in to leave comments. Login now