Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/commands.py @ 4544:930ed513c864
Create a separate module for managing extensions
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 11 Jun 2007 21:09:23 -0500 |
parents | 6b4e8a75d5fc |
children | aea8fd7fb5e2 |
comparison
equal
deleted
inserted
replaced
4543:6b4e8a75d5fc | 4544:930ed513c864 |
---|---|
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 demandimport; demandimport.enable() | 8 import demandimport; demandimport.enable() |
9 from node import * | 9 from node import * |
10 from i18n import _ | 10 from i18n import _ |
11 import bisect, os, re, sys, signal, imp, urllib, pdb, shlex, stat | 11 import bisect, os, re, sys, signal, urllib, pdb, shlex, stat |
12 import fancyopts, ui, hg, util, lock, revlog, bundlerepo | 12 import fancyopts, ui, hg, util, lock, revlog, bundlerepo, extensions |
13 import difflib, patch, time, help, mdiff, tempfile | 13 import difflib, patch, time, help, mdiff, tempfile |
14 import traceback, errno, version, atexit, socket | 14 import traceback, errno, version, atexit, socket |
15 import archival, changegroup, cmdutil, hgweb.server, sshserver | 15 import archival, changegroup, cmdutil, hgweb.server, sshserver |
16 | 16 |
17 class UnknownCommand(Exception): | 17 class UnknownCommand(Exception): |
1369 ui.write("%s\n" % header) | 1369 ui.write("%s\n" % header) |
1370 ui.write("%s\n" % doc.rstrip()) | 1370 ui.write("%s\n" % doc.rstrip()) |
1371 | 1371 |
1372 def helpext(name): | 1372 def helpext(name): |
1373 try: | 1373 try: |
1374 mod = findext(name) | 1374 mod = extensions.find(name) |
1375 except KeyError: | 1375 except KeyError: |
1376 raise UnknownCommand(name) | 1376 raise UnknownCommand(name) |
1377 | 1377 |
1378 doc = (mod.__doc__ or _('No help text available')).splitlines(0) | 1378 doc = (mod.__doc__ or _('No help text available')).splitlines(0) |
1379 ui.write(_('%s extension - %s\n') % (name.split('.')[-1], doc[0])) | 1379 ui.write(_('%s extension - %s\n') % (name.split('.')[-1], doc[0])) |
3135 parsed.append((section, name, value)) | 3135 parsed.append((section, name, value)) |
3136 except (IndexError, ValueError): | 3136 except (IndexError, ValueError): |
3137 raise util.Abort(_('malformed --config option: %s') % cfg) | 3137 raise util.Abort(_('malformed --config option: %s') % cfg) |
3138 return parsed | 3138 return parsed |
3139 | 3139 |
3140 external = {} | |
3141 | |
3142 def findext(name): | |
3143 '''return module with given extension name''' | |
3144 try: | |
3145 return sys.modules[external[name]] | |
3146 except KeyError: | |
3147 for k, v in external.iteritems(): | |
3148 if k.endswith('.' + name) or k.endswith('/' + name) or v == name: | |
3149 return sys.modules[v] | |
3150 raise KeyError(name) | |
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 | |
3187 def load_extensions(ui): | |
3188 for name, load in ui.extensions(): | |
3189 try: | |
3190 load_extension(ui, name, load) | |
3191 except (util.SignalInterrupt, KeyboardInterrupt): | |
3192 raise | |
3193 except Exception, inst: | |
3194 ui.warn(_("*** failed to import extension %s: %s\n") % | |
3195 (name, inst)) | |
3196 if ui.print_exc(): | |
3197 return 1 | |
3198 | |
3199 def catchterm(*args): | 3140 def catchterm(*args): |
3200 raise util.SignalInterrupt | 3141 raise util.SignalInterrupt |
3201 | 3142 |
3202 def dispatch(args): | 3143 def dispatch(args): |
3203 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': | 3144 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': |
3208 u = ui.ui(traceback='--traceback' in sys.argv[1:]) | 3149 u = ui.ui(traceback='--traceback' in sys.argv[1:]) |
3209 except util.Abort, inst: | 3150 except util.Abort, inst: |
3210 sys.stderr.write(_("abort: %s\n") % inst) | 3151 sys.stderr.write(_("abort: %s\n") % inst) |
3211 return -1 | 3152 return -1 |
3212 | 3153 |
3213 load_extensions(u) | 3154 extensions.loadall(u) |
3214 u.addreadhook(load_extensions) | 3155 u.addreadhook(extensions.loadall) |
3215 | 3156 |
3216 try: | 3157 try: |
3217 cmd, func, args, options, cmdoptions = parse(u, args) | 3158 cmd, func, args, options, cmdoptions = parse(u, args) |
3218 if options["encoding"]: | 3159 if options["encoding"]: |
3219 util._encoding = options["encoding"] | 3160 util._encoding = options["encoding"] |