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