mercurial/commands.py
changeset 4543 6b4e8a75d5fc
parent 4542 af02e6078d08
child 4544 930ed513c864
equal deleted inserted replaced
4542:af02e6078d08 4543:6b4e8a75d5fc
  3147         for k, v in external.iteritems():
  3147         for k, v in external.iteritems():
  3148             if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
  3148             if k.endswith('.' + name) or k.endswith('/' + name) or v == name:
  3149                 return sys.modules[v]
  3149                 return sys.modules[v]
  3150         raise KeyError(name)
  3150         raise KeyError(name)
  3151 
  3151 
       
  3152 def load_extension(ui, name, load):
       
  3153     if name in external:
       
  3154         return
       
  3155     if load:
       
  3156         # the module will be loaded in sys.modules
       
  3157         # choose an unique name so that it doesn't
       
  3158         # conflicts with other modules
       
  3159         module_name = "hgext_%s" % name.replace('.', '_')
       
  3160         mod = imp.load_source(module_name, load)
       
  3161     else:
       
  3162         def importh(name):
       
  3163             mod = __import__(name)
       
  3164             components = name.split('.')
       
  3165             for comp in components[1:]:
       
  3166                 mod = getattr(mod, comp)
       
  3167             return mod
       
  3168         try:
       
  3169             mod = importh("hgext.%s" % name)
       
  3170         except ImportError:
       
  3171             mod = importh(name)
       
  3172     external[name] = mod.__name__
       
  3173 
       
  3174     uisetup = getattr(mod, 'uisetup', None)
       
  3175     if uisetup:
       
  3176         uisetup(ui)
       
  3177     reposetup = getattr(mod, 'reposetup', None)
       
  3178     if reposetup:
       
  3179         hg.repo_setup_hooks.append(reposetup)
       
  3180     cmdtable = getattr(mod, 'cmdtable', {})
       
  3181     overrides = [cmd for cmd in cmdtable if cmd in table]
       
  3182     if overrides:
       
  3183         ui.warn(_("extension '%s' overrides commands: %s\n")
       
  3184                 % (name, " ".join(overrides)))
       
  3185     table.update(cmdtable)
       
  3186 
  3152 def load_extensions(ui):
  3187 def load_extensions(ui):
  3153     added = []
  3188     for name, load in ui.extensions():
  3154     for ext_name, load_from_name in ui.extensions():
       
  3155         if ext_name in external:
       
  3156             continue
       
  3157         try:
  3189         try:
  3158             if load_from_name:
  3190             load_extension(ui, name, load)
  3159                 # the module will be loaded in sys.modules
       
  3160                 # choose an unique name so that it doesn't
       
  3161                 # conflicts with other modules
       
  3162                 module_name = "hgext_%s" % ext_name.replace('.', '_')
       
  3163                 mod = imp.load_source(module_name, load_from_name)
       
  3164             else:
       
  3165                 def importh(name):
       
  3166                     mod = __import__(name)
       
  3167                     components = name.split('.')
       
  3168                     for comp in components[1:]:
       
  3169                         mod = getattr(mod, comp)
       
  3170                     return mod
       
  3171                 try:
       
  3172                     mod = importh("hgext.%s" % ext_name)
       
  3173                 except ImportError:
       
  3174                     mod = importh(ext_name)
       
  3175             external[ext_name] = mod.__name__
       
  3176             added.append((mod, ext_name))
       
  3177         except (util.SignalInterrupt, KeyboardInterrupt):
  3191         except (util.SignalInterrupt, KeyboardInterrupt):
  3178             raise
  3192             raise
  3179         except Exception, inst:
  3193         except Exception, inst:
  3180             ui.warn(_("*** failed to import extension %s: %s\n") %
  3194             ui.warn(_("*** failed to import extension %s: %s\n") %
  3181                     (ext_name, inst))
  3195                     (name, inst))
  3182             if ui.print_exc():
  3196             if ui.print_exc():
  3183                 return 1
  3197                 return 1
  3184 
       
  3185     for mod, name in added:
       
  3186         uisetup = getattr(mod, 'uisetup', None)
       
  3187         if uisetup:
       
  3188             uisetup(ui)
       
  3189         reposetup = getattr(mod, 'reposetup', None)
       
  3190         if reposetup:
       
  3191             hg.repo_setup_hooks.append(reposetup)
       
  3192         cmdtable = getattr(mod, 'cmdtable', {})
       
  3193         overrides = [cmd for cmd in cmdtable if cmd in table]
       
  3194         if overrides:
       
  3195             ui.warn(_("extension '%s' overrides commands: %s\n")
       
  3196                     % (name, " ".join(overrides)))
       
  3197         table.update(cmdtable)
       
  3198 
  3198 
  3199 def catchterm(*args):
  3199 def catchterm(*args):
  3200     raise util.SignalInterrupt
  3200     raise util.SignalInterrupt
  3201 
  3201 
  3202 def dispatch(args):
  3202 def dispatch(args):