2007-06-12 06:09:23 +04:00
|
|
|
# extensions.py - extension handling for mercurial
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
|
2007-06-12 06:09:23 +04:00
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2007-06-12 06:09:23 +04:00
|
|
|
|
2015-08-09 05:13:14 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
wrapfunction: use functools.partial if possible
Every `extensions.bind` call inserts a frame in traceback:
... in closure
return func(*(args + a), **kw)
which makes traceback noisy.
The Python stdlib has a `functools.partial` which is backed by C code and
does not pollute traceback. However it does not support instancemethod and
sets `args` attribute which could be problematic for alias handling.
This patch makes `wrapfunction` use `functools.partial` if we are wrapping a
function directly exported by a module (so it's impossible to be a class or
instance method), and special handles `wrapfunction` results so alias
handling code could handle `args` just fine.
As an example, `hg rebase -s . -d . --traceback` got 6 lines removed in my
setup:
File "hg/mercurial/dispatch.py", line 898, in _dispatch
cmdpats, cmdoptions)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "hg/hgext/journal.py", line 84, in runcommand
return orig(lui, repo, cmd, fullargs, *args)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "fb-hgext/hgext3rd/fbamend/hiddenoverride.py", line 119, in runcommand
result = orig(lui, repo, cmd, fullargs, *args)
File "hg/mercurial/dispatch.py", line 660, in runcommand
ret = _runcommand(ui, options, cmd, d)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "hg/hgext/pager.py", line 69, in pagecmd
return orig(ui, options, cmd, cmdfunc)
....
Differential Revision: https://phab.mercurial-scm.org/D632
2017-09-05 23:37:36 +03:00
|
|
|
import functools
|
2015-08-09 05:13:14 +03:00
|
|
|
import imp
|
2017-03-03 21:27:21 +03:00
|
|
|
import inspect
|
2015-08-09 05:13:14 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
from .i18n import (
|
|
|
|
_,
|
|
|
|
gettext,
|
|
|
|
)
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
cmdutil,
|
2017-06-17 14:48:20 +03:00
|
|
|
configitems,
|
2015-08-09 05:13:14 +03:00
|
|
|
error,
|
2016-12-01 10:42:04 +03:00
|
|
|
pycompat,
|
2015-08-09 05:13:14 +03:00
|
|
|
util,
|
|
|
|
)
|
2007-06-12 06:09:23 +04:00
|
|
|
|
|
|
|
_extensions = {}
|
2016-09-01 23:06:42 +03:00
|
|
|
_disabledextensions = {}
|
2015-02-06 23:07:32 +03:00
|
|
|
_aftercallbacks = {}
|
2007-08-18 00:33:27 +04:00
|
|
|
_order = []
|
2017-07-15 15:16:54 +03:00
|
|
|
_builtin = {
|
|
|
|
'hbisect',
|
|
|
|
'bookmarks',
|
2017-07-15 15:17:35 +03:00
|
|
|
'color',
|
2017-07-15 15:16:54 +03:00
|
|
|
'parentrevspec',
|
|
|
|
'progress',
|
|
|
|
'interhg',
|
|
|
|
'inotify',
|
|
|
|
'hgcia'
|
|
|
|
}
|
2018-01-09 04:01:31 +03:00
|
|
|
_blacklist = {
|
|
|
|
'extlib',
|
|
|
|
}
|
2007-08-18 00:33:27 +04:00
|
|
|
|
2018-01-10 04:06:09 +03:00
|
|
|
# root of the directory, or installed distribution
|
|
|
|
_hgroot = os.path.abspath(os.path.join(__file__, '../../'))
|
|
|
|
_sysroot = os.path.abspath(os.path.join(os.__file__, '../'))
|
|
|
|
|
2018-01-13 03:45:54 +03:00
|
|
|
# List of extensions to always enable by default, unless overwritten by config.
|
|
|
|
#
|
|
|
|
# This allows us to integrate extensions into the codebase while leaving them in
|
|
|
|
# hgext/ -- useful for extensions that need cleaning up, or significant
|
|
|
|
# integration work, to be brought into mercurial/.
|
|
|
|
DEFAULT_EXTENSIONS = [
|
2018-01-23 23:08:57 +03:00
|
|
|
'conflictinfo',
|
2018-01-17 23:54:24 +03:00
|
|
|
'debugshell',
|
2018-01-23 23:08:57 +03:00
|
|
|
'errorredirect',
|
2018-01-13 03:45:54 +03:00
|
|
|
'githelp',
|
2018-01-23 23:08:57 +03:00
|
|
|
'mergedriver',
|
|
|
|
'progressfile',
|
2018-01-24 04:55:25 +03:00
|
|
|
'simplecache',
|
2018-01-13 03:45:54 +03:00
|
|
|
]
|
|
|
|
|
2013-09-21 16:33:29 +04:00
|
|
|
def extensions(ui=None):
|
|
|
|
if ui:
|
|
|
|
def enabled(name):
|
|
|
|
for format in ['%s', 'hgext.%s']:
|
|
|
|
conf = ui.config('extensions', format % name)
|
|
|
|
if conf is not None and not conf.startswith('!'):
|
|
|
|
return True
|
2018-01-23 23:08:44 +03:00
|
|
|
# Check DEFAULT_EXTENSIONS if no config for this extension was
|
|
|
|
# specified.
|
|
|
|
if conf is None and name in DEFAULT_EXTENSIONS:
|
|
|
|
return True
|
2013-09-21 16:33:29 +04:00
|
|
|
else:
|
|
|
|
enabled = lambda name: True
|
2007-08-18 00:33:27 +04:00
|
|
|
for name in _order:
|
|
|
|
module = _extensions[name]
|
2013-09-21 16:33:29 +04:00
|
|
|
if module and enabled(name):
|
2007-08-18 00:33:27 +04:00
|
|
|
yield name, module
|
2007-06-12 06:09:23 +04:00
|
|
|
|
|
|
|
def find(name):
|
|
|
|
'''return module with given extension name'''
|
2011-05-24 00:09:00 +04:00
|
|
|
mod = None
|
2007-06-12 06:09:23 +04:00
|
|
|
try:
|
2015-12-31 11:16:59 +03:00
|
|
|
mod = _extensions[name]
|
2007-06-12 06:09:23 +04:00
|
|
|
except KeyError:
|
|
|
|
for k, v in _extensions.iteritems():
|
2007-06-13 22:15:53 +04:00
|
|
|
if k.endswith('.' + name) or k.endswith('/' + name):
|
2011-05-24 00:09:00 +04:00
|
|
|
mod = v
|
|
|
|
break
|
|
|
|
if not mod:
|
2007-06-12 06:09:23 +04:00
|
|
|
raise KeyError(name)
|
2011-05-24 00:09:00 +04:00
|
|
|
return mod
|
2007-06-12 06:09:23 +04:00
|
|
|
|
2009-03-27 02:28:09 +03:00
|
|
|
def loadpath(path, module_name):
|
2018-01-25 02:34:51 +03:00
|
|
|
"""loads the given extension from the given path
|
|
|
|
|
|
|
|
Note, this cannot be used to load core extensions, since the relative
|
|
|
|
imports they use no longer work within loadpath.
|
|
|
|
"""
|
2009-03-27 02:28:09 +03:00
|
|
|
module_name = module_name.replace('.', '_')
|
2014-03-05 13:31:05 +04:00
|
|
|
path = util.normpath(util.expandpath(path))
|
2016-12-05 04:16:51 +03:00
|
|
|
module_name = pycompat.fsdecode(module_name)
|
|
|
|
path = pycompat.fsdecode(path)
|
2009-03-27 02:28:09 +03:00
|
|
|
if os.path.isdir(path):
|
|
|
|
# module/__init__.py style
|
2014-03-05 13:31:05 +04:00
|
|
|
d, f = os.path.split(path)
|
2009-03-27 02:28:09 +03:00
|
|
|
fd, fpath, desc = imp.find_module(f, [d])
|
|
|
|
return imp.load_module(module_name, fd, fpath, desc)
|
|
|
|
else:
|
2012-07-06 20:41:25 +04:00
|
|
|
try:
|
|
|
|
return imp.load_source(module_name, path)
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as exc:
|
2012-07-06 20:41:25 +04:00
|
|
|
if not exc.filename:
|
|
|
|
exc.filename = path # python does not fill this
|
|
|
|
raise
|
2009-03-27 02:28:09 +03:00
|
|
|
|
2018-01-10 04:06:09 +03:00
|
|
|
def loaddefault(name, reportfunc=None):
|
|
|
|
"""load extensions without a specified path"""
|
|
|
|
try:
|
|
|
|
mod = _importh("hgext.%s" % name)
|
|
|
|
except ImportError as err:
|
|
|
|
if reportfunc:
|
|
|
|
reportfunc(err, "hgext.%s" % name, "hgext3rd.%s" % name)
|
|
|
|
try:
|
|
|
|
mod = _importh("hgext3rd.%s" % name)
|
|
|
|
except ImportError as err:
|
|
|
|
if reportfunc:
|
|
|
|
reportfunc(err, "hgext3rd.%s" % name, name)
|
|
|
|
mod = _importh(name)
|
|
|
|
return mod
|
|
|
|
|
|
|
|
_collectedimports = [] # [(name, path)]
|
|
|
|
|
|
|
|
def _collectimport(orig, name, *args, **kwargs):
|
|
|
|
"""collect imports to _collectedimports"""
|
|
|
|
mod = orig(name, *args, **kwargs)
|
|
|
|
try:
|
|
|
|
path = inspect.getabsfile(_resolvenestedmodules(mod, name))
|
|
|
|
if path:
|
|
|
|
_collectedimports.append((name, path))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return mod
|
|
|
|
|
|
|
|
# When path has "/mercurial/" or "/hgext/" consider it as an hg module.
|
|
|
|
# Used by _checkforeignmodules.
|
|
|
|
_hgmodpathre = util.re.compile('/(?:mercurial|hgext)/')
|
|
|
|
|
|
|
|
def _checkforeignmodules():
|
|
|
|
"""check _collectedimports and complain about importing foreign modules
|
|
|
|
|
|
|
|
Only call this when the extension is imported without an explict path.
|
|
|
|
"""
|
|
|
|
for name, path in _collectedimports:
|
|
|
|
# an hg module should live in the hg (repo) root
|
|
|
|
if _hgmodpathre.search(path) and not path.startswith(_hgroot):
|
|
|
|
raise error.ForeignImportError('%s: %s lives outside %s'
|
|
|
|
% (name, path, _hgroot))
|
|
|
|
|
2016-03-11 13:24:54 +03:00
|
|
|
def _importh(name):
|
|
|
|
"""import and return the <name> module"""
|
2016-12-01 10:42:04 +03:00
|
|
|
mod = __import__(pycompat.sysstr(name))
|
2018-01-10 04:06:09 +03:00
|
|
|
return _resolvenestedmodules(mod, name)
|
|
|
|
|
|
|
|
def _resolvenestedmodules(mod, name):
|
|
|
|
"""resolve nested modules
|
|
|
|
|
|
|
|
__import__('x.y.z') returns module x. This function resolves it and return
|
|
|
|
the module "z".
|
|
|
|
"""
|
2016-03-11 13:24:54 +03:00
|
|
|
components = name.split('.')
|
|
|
|
for comp in components[1:]:
|
|
|
|
mod = getattr(mod, comp)
|
|
|
|
return mod
|
|
|
|
|
2018-01-10 04:06:09 +03:00
|
|
|
def _importext(name, path=None, reportfunc=None, strict=False):
|
2016-10-03 05:37:10 +03:00
|
|
|
if path:
|
|
|
|
# the module will be loaded in sys.modules
|
|
|
|
# choose an unique name so that it doesn't
|
|
|
|
# conflicts with other modules
|
|
|
|
mod = loadpath(path, 'hgext.%s' % name)
|
|
|
|
else:
|
2018-01-10 04:06:09 +03:00
|
|
|
if strict:
|
|
|
|
import __builtin__ as builtins
|
|
|
|
import hgdemandimport
|
|
|
|
_collectedimports[:] = []
|
|
|
|
with hgdemandimport.disabled():
|
|
|
|
with wrappedfunction(builtins, '__import__', _collectimport):
|
|
|
|
mod = loaddefault(name, reportfunc)
|
|
|
|
_checkforeignmodules()
|
|
|
|
else:
|
|
|
|
mod = loaddefault(name, reportfunc)
|
2016-10-03 05:37:10 +03:00
|
|
|
return mod
|
|
|
|
|
2016-03-11 13:28:58 +03:00
|
|
|
def _reportimporterror(ui, err, failed, next):
|
2016-09-30 01:27:35 +03:00
|
|
|
# note: this ui.debug happens before --debug is processed,
|
|
|
|
# Use --config ui.debug=1 to see them.
|
2016-03-11 13:28:58 +03:00
|
|
|
ui.debug('could not import %s (%s): trying %s\n'
|
2017-08-03 17:02:32 +03:00
|
|
|
% (failed, util.forcebytestr(err), next))
|
2016-03-11 13:28:58 +03:00
|
|
|
if ui.debugflag:
|
|
|
|
ui.traceback()
|
|
|
|
|
2017-05-13 09:41:50 +03:00
|
|
|
# attributes set by registrar.command
|
|
|
|
_cmdfuncattrs = ('norepo', 'optionalrepo', 'inferrepo')
|
|
|
|
|
2016-01-09 17:24:52 +03:00
|
|
|
def _validatecmdtable(ui, cmdtable):
|
2017-05-13 09:41:50 +03:00
|
|
|
"""Check if extension commands have required attributes"""
|
|
|
|
for c, e in cmdtable.iteritems():
|
|
|
|
f = e[0]
|
2016-01-09 17:24:52 +03:00
|
|
|
if getattr(f, '_deprecatedregistrar', False):
|
|
|
|
ui.deprecwarn("cmdutil.command is deprecated, use "
|
|
|
|
"registrar.command to register '%s'" % c, '4.6')
|
2017-05-13 09:41:50 +03:00
|
|
|
missing = [a for a in _cmdfuncattrs if not util.safehasattr(f, a)]
|
|
|
|
if not missing:
|
2017-08-29 19:24:51 +03:00
|
|
|
for option in e[1]:
|
|
|
|
default = option[2]
|
|
|
|
if isinstance(default, type(u'')):
|
|
|
|
raise error.ProgrammingError(
|
|
|
|
"option '%s.%s' has a unicode default value"
|
|
|
|
% (c, option[1]),
|
|
|
|
hint=("change the %s.%s default value to a "
|
|
|
|
"non-unicode string" % (c, option[1])))
|
2017-05-13 09:41:50 +03:00
|
|
|
continue
|
|
|
|
raise error.ProgrammingError(
|
|
|
|
'missing attributes: %s' % ', '.join(missing),
|
|
|
|
hint="use @command decorator to register '%s'" % c)
|
|
|
|
|
2007-06-12 06:09:23 +04:00
|
|
|
def load(ui, name, path):
|
2008-09-09 16:43:12 +04:00
|
|
|
if name.startswith('hgext.') or name.startswith('hgext/'):
|
2007-07-31 01:53:03 +04:00
|
|
|
shortname = name[6:]
|
|
|
|
else:
|
|
|
|
shortname = name
|
2015-11-25 03:38:54 +03:00
|
|
|
if shortname in _builtin:
|
2011-02-10 22:46:27 +03:00
|
|
|
return None
|
2007-07-31 01:53:03 +04:00
|
|
|
if shortname in _extensions:
|
2010-10-19 15:43:40 +04:00
|
|
|
return _extensions[shortname]
|
2007-08-09 05:04:06 +04:00
|
|
|
_extensions[shortname] = None
|
2018-02-09 00:21:47 +03:00
|
|
|
# If the entry point is not 'hg', the code was executed in a non-standard
|
|
|
|
# way and we cannot assume the filesystem layout. Be permissive to avoid
|
|
|
|
# false positives.
|
|
|
|
from . import dispatch # avoid cycles
|
2018-01-10 04:06:09 +03:00
|
|
|
strict = (util.safehasattr(ui, 'configbool')
|
2018-02-08 21:29:49 +03:00
|
|
|
and ui.configbool('devel', 'all-warnings')
|
2018-02-09 00:21:47 +03:00
|
|
|
and dispatch.getentrypoint() == 'hg')
|
2018-01-10 04:06:09 +03:00
|
|
|
mod = _importext(name, path, bind(_reportimporterror, ui), strict)
|
extensions: refuse to load extensions if minimum hg version not met
As the author of several 3rd party extensions, I frequently see bug
reports from users attempting to run my extension with an old version
of Mercurial that I no longer support in my extension. Oftentimes, the
extension will import just fine. But as soon as we run extsetup(),
reposetup(), or get into the guts of a wrapped function, we encounter
an exception and abort. Today, Mercurial will print a message about
extensions that don't have a "testedwith" declaring explicit
compatibility with the current version.
The existing mechanism is a good start. But it isn't as robust as I
would like. Specifically, Mercurial assumes compatibility by default.
This means extension authors must perform compatibility checking in
their extsetup() or we wait and see if we encounter an abort at
runtime. And, compatibility checking can involve a lot of code and
lots of error checking. It's a lot of effort for extension authors.
Oftentimes, extension authors know which versions of Mercurial there
extension works on and more importantly where it is broken.
This patch introduces a magic "minimumhgversion" attribute in
extensions. When found, the extension loading mechanism will compare
the declared version against the current Mercurial version. If the
extension explicitly states we require a newer Mercurial version, a
warning is printed and the extension isn't loaded beyond importing
the Python module. This causes a graceful failure while alerting
the user of the compatibility issue.
I would be receptive to the idea of making the failure more fatal.
However, care would need to be taken to not criple every hg command.
e.g. the user may use `hg config` to fix the hgrc and if we aborted
trying to run that, the user would effectively be locked out of `hg`!
A potential future improvement to this functionality would be to catch
ImportError for the extension/module and parse the source code for
"minimumhgversion = 'XXX'" and do similar checking. This way we could
give more information about why the extension failed to load.
2015-11-25 02:16:25 +03:00
|
|
|
|
|
|
|
# Before we do anything with the extension, check against minimum stated
|
|
|
|
# compatibility. This gives extension authors a mechanism to have their
|
|
|
|
# extensions short circuit when loaded with a known incompatible version
|
|
|
|
# of Mercurial.
|
|
|
|
minver = getattr(mod, 'minimumhgversion', None)
|
|
|
|
if minver and util.versiontuple(minver, 2) > util.versiontuple(n=2):
|
|
|
|
ui.warn(_('(third party extension %s requires version %s or newer '
|
|
|
|
'of Mercurial; disabling)\n') % (shortname, minver))
|
|
|
|
return
|
2016-01-09 17:24:52 +03:00
|
|
|
_validatecmdtable(ui, getattr(mod, 'cmdtable', {}))
|
extensions: refuse to load extensions if minimum hg version not met
As the author of several 3rd party extensions, I frequently see bug
reports from users attempting to run my extension with an old version
of Mercurial that I no longer support in my extension. Oftentimes, the
extension will import just fine. But as soon as we run extsetup(),
reposetup(), or get into the guts of a wrapped function, we encounter
an exception and abort. Today, Mercurial will print a message about
extensions that don't have a "testedwith" declaring explicit
compatibility with the current version.
The existing mechanism is a good start. But it isn't as robust as I
would like. Specifically, Mercurial assumes compatibility by default.
This means extension authors must perform compatibility checking in
their extsetup() or we wait and see if we encounter an abort at
runtime. And, compatibility checking can involve a lot of code and
lots of error checking. It's a lot of effort for extension authors.
Oftentimes, extension authors know which versions of Mercurial there
extension works on and more importantly where it is broken.
This patch introduces a magic "minimumhgversion" attribute in
extensions. When found, the extension loading mechanism will compare
the declared version against the current Mercurial version. If the
extension explicitly states we require a newer Mercurial version, a
warning is printed and the extension isn't loaded beyond importing
the Python module. This causes a graceful failure while alerting
the user of the compatibility issue.
I would be receptive to the idea of making the failure more fatal.
However, care would need to be taken to not criple every hg command.
e.g. the user may use `hg config` to fix the hgrc and if we aborted
trying to run that, the user would effectively be locked out of `hg`!
A potential future improvement to this functionality would be to catch
ImportError for the extension/module and parse the source code for
"minimumhgversion = 'XXX'" and do similar checking. This way we could
give more information about why the extension failed to load.
2015-11-25 02:16:25 +03:00
|
|
|
|
2007-07-31 01:53:03 +04:00
|
|
|
_extensions[shortname] = mod
|
2007-08-18 00:33:27 +04:00
|
|
|
_order.append(shortname)
|
2015-02-06 23:07:32 +03:00
|
|
|
for fn in _aftercallbacks.get(shortname, []):
|
|
|
|
fn(loaded=True)
|
2010-10-19 15:43:40 +04:00
|
|
|
return mod
|
2007-06-12 06:09:23 +04:00
|
|
|
|
2016-06-30 12:31:50 +03:00
|
|
|
def _runuisetup(name, ui):
|
|
|
|
uisetup = getattr(_extensions[name], 'uisetup', None)
|
|
|
|
if uisetup:
|
2017-06-06 17:09:48 +03:00
|
|
|
try:
|
|
|
|
uisetup(ui)
|
|
|
|
except Exception as inst:
|
2017-10-17 20:31:44 +03:00
|
|
|
ui.traceback(force=True)
|
2017-08-03 17:02:32 +03:00
|
|
|
msg = util.forcebytestr(inst)
|
2017-06-06 17:09:48 +03:00
|
|
|
ui.warn(_("*** failed to set up extension %s: %s\n") % (name, msg))
|
|
|
|
return False
|
|
|
|
return True
|
2016-06-30 12:31:50 +03:00
|
|
|
|
|
|
|
def _runextsetup(name, ui):
|
|
|
|
extsetup = getattr(_extensions[name], 'extsetup', None)
|
|
|
|
if extsetup:
|
|
|
|
try:
|
2017-06-06 17:09:48 +03:00
|
|
|
try:
|
|
|
|
extsetup(ui)
|
|
|
|
except TypeError:
|
2017-07-26 05:48:46 +03:00
|
|
|
# Try to use getfullargspec (Python 3) first, and fall
|
|
|
|
# back to getargspec only if it doesn't exist so as to
|
|
|
|
# avoid warnings.
|
|
|
|
if getattr(inspect, 'getfullargspec',
|
|
|
|
getattr(inspect, 'getargspec'))(extsetup).args:
|
2017-06-06 17:09:48 +03:00
|
|
|
raise
|
|
|
|
extsetup() # old extsetup with no ui argument
|
|
|
|
except Exception as inst:
|
2017-10-17 20:31:44 +03:00
|
|
|
ui.traceback(force=True)
|
2017-08-03 17:02:32 +03:00
|
|
|
msg = util.forcebytestr(inst)
|
2017-06-06 17:09:48 +03:00
|
|
|
ui.warn(_("*** failed to set up extension %s: %s\n") % (name, msg))
|
|
|
|
return False
|
|
|
|
return True
|
2016-06-30 12:31:50 +03:00
|
|
|
|
2017-05-22 10:51:56 +03:00
|
|
|
def loadall(ui, whitelist=None):
|
2007-06-18 22:24:34 +04:00
|
|
|
result = ui.configitems("extensions")
|
2018-01-13 03:45:54 +03:00
|
|
|
resultkeys = set([name for name, loc in result])
|
|
|
|
|
|
|
|
# Add all extensions in `DEFAULT_EXTENSIONS` that were not defined by
|
|
|
|
# extensions.
|
|
|
|
result += [(name, '') for name in DEFAULT_EXTENSIONS
|
|
|
|
if name not in resultkeys]
|
|
|
|
|
2017-05-22 11:17:49 +03:00
|
|
|
if whitelist is not None:
|
2017-05-22 10:51:56 +03:00
|
|
|
result = [(k, v) for (k, v) in result if k in whitelist]
|
2009-08-29 02:29:16 +04:00
|
|
|
newindex = len(_order)
|
2009-03-23 15:13:11 +03:00
|
|
|
for (name, path) in result:
|
2007-06-18 22:24:34 +04:00
|
|
|
if path:
|
2017-03-03 21:32:10 +03:00
|
|
|
if path[0:1] == '!':
|
2016-09-01 23:06:42 +03:00
|
|
|
_disabledextensions[name] = path[1:]
|
2007-09-19 04:53:01 +04:00
|
|
|
continue
|
2007-06-12 06:09:23 +04:00
|
|
|
try:
|
|
|
|
load(ui, name, path)
|
2018-01-10 04:06:09 +03:00
|
|
|
except error.ForeignImportError:
|
|
|
|
raise
|
2015-06-24 08:20:08 +03:00
|
|
|
except Exception as inst:
|
2017-08-03 17:02:32 +03:00
|
|
|
msg = util.forcebytestr(inst)
|
2008-03-03 20:46:57 +03:00
|
|
|
if path:
|
|
|
|
ui.warn(_("*** failed to import extension %s from %s: %s\n")
|
2017-05-14 09:46:45 +03:00
|
|
|
% (name, path, msg))
|
2008-03-03 20:46:57 +03:00
|
|
|
else:
|
|
|
|
ui.warn(_("*** failed to import extension %s: %s\n")
|
2017-05-14 09:46:45 +03:00
|
|
|
% (name, msg))
|
|
|
|
if isinstance(inst, error.Hint) and inst.hint:
|
|
|
|
ui.warn(_("*** (%s)\n") % inst.hint)
|
2015-03-30 10:23:35 +03:00
|
|
|
ui.traceback()
|
2017-09-05 01:34:13 +03:00
|
|
|
# list of (objname, loadermod, loadername) tuple:
|
|
|
|
# - objname is the name of an object in extension module,
|
|
|
|
# from which extra information is loaded
|
|
|
|
# - loadermod is the module where loader is placed
|
|
|
|
# - loadername is the name of the function,
|
|
|
|
# which takes (ui, extensionname, extraobj) arguments
|
|
|
|
#
|
|
|
|
# This one is for the list of item that must be run before running any setup
|
|
|
|
earlyextraloaders = [
|
|
|
|
('configtable', configitems, 'loadconfigtable'),
|
|
|
|
]
|
|
|
|
_loadextra(ui, newindex, earlyextraloaders)
|
2007-06-12 06:09:23 +04:00
|
|
|
|
2017-06-06 17:09:48 +03:00
|
|
|
broken = set()
|
2009-08-29 02:29:16 +04:00
|
|
|
for name in _order[newindex:]:
|
2017-06-06 17:09:48 +03:00
|
|
|
if not _runuisetup(name, ui):
|
|
|
|
broken.add(name)
|
2009-08-29 02:29:16 +04:00
|
|
|
|
2009-10-28 17:55:23 +03:00
|
|
|
for name in _order[newindex:]:
|
2017-06-06 17:09:48 +03:00
|
|
|
if name in broken:
|
|
|
|
continue
|
|
|
|
if not _runextsetup(name, ui):
|
|
|
|
broken.add(name)
|
|
|
|
|
|
|
|
for name in broken:
|
|
|
|
_extensions[name] = None
|
2009-10-28 17:55:23 +03:00
|
|
|
|
2015-02-06 23:07:32 +03:00
|
|
|
# Call aftercallbacks that were never met.
|
|
|
|
for shortname in _aftercallbacks:
|
|
|
|
if shortname in _extensions:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for fn in _aftercallbacks[shortname]:
|
|
|
|
fn(loaded=False)
|
|
|
|
|
2015-05-06 19:52:10 +03:00
|
|
|
# loadall() is called multiple times and lingering _aftercallbacks
|
|
|
|
# entries could result in double execution. See issue4646.
|
|
|
|
_aftercallbacks.clear()
|
|
|
|
|
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
2017-06-23 20:39:20 +03:00
|
|
|
# delay importing avoids cyclic dependency (especially commands)
|
|
|
|
from . import (
|
|
|
|
color,
|
|
|
|
commands,
|
2017-08-05 19:13:57 +03:00
|
|
|
filemerge,
|
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
2017-06-23 20:39:20 +03:00
|
|
|
fileset,
|
|
|
|
revset,
|
|
|
|
templatefilters,
|
|
|
|
templatekw,
|
|
|
|
templater,
|
|
|
|
)
|
|
|
|
|
|
|
|
# list of (objname, loadermod, loadername) tuple:
|
|
|
|
# - objname is the name of an object in extension module,
|
|
|
|
# from which extra information is loaded
|
|
|
|
# - loadermod is the module where loader is placed
|
|
|
|
# - loadername is the name of the function,
|
|
|
|
# which takes (ui, extensionname, extraobj) arguments
|
|
|
|
extraloaders = [
|
|
|
|
('cmdtable', commands, 'loadcmdtable'),
|
|
|
|
('colortable', color, 'loadcolortable'),
|
|
|
|
('filesetpredicate', fileset, 'loadpredicate'),
|
2017-08-05 19:13:57 +03:00
|
|
|
('internalmerge', filemerge, 'loadinternalmerge'),
|
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
2017-06-23 20:39:20 +03:00
|
|
|
('revsetpredicate', revset, 'loadpredicate'),
|
|
|
|
('templatefilter', templatefilters, 'loadfilter'),
|
|
|
|
('templatefunc', templater, 'loadfunction'),
|
|
|
|
('templatekeyword', templatekw, 'loadkeyword'),
|
|
|
|
]
|
2017-09-05 01:31:59 +03:00
|
|
|
_loadextra(ui, newindex, extraloaders)
|
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
2017-06-23 20:39:20 +03:00
|
|
|
|
2017-09-05 01:31:59 +03:00
|
|
|
def _loadextra(ui, newindex, extraloaders):
|
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
2017-06-23 20:39:20 +03:00
|
|
|
for name in _order[newindex:]:
|
|
|
|
module = _extensions[name]
|
|
|
|
if not module:
|
|
|
|
continue # loading this module failed
|
|
|
|
|
|
|
|
for objname, loadermod, loadername in extraloaders:
|
|
|
|
extraobj = getattr(module, objname, None)
|
|
|
|
if extraobj is not None:
|
|
|
|
getattr(loadermod, loadername)(ui, name, extraobj)
|
|
|
|
|
2015-02-06 23:07:32 +03:00
|
|
|
def afterloaded(extension, callback):
|
|
|
|
'''Run the specified function after a named extension is loaded.
|
|
|
|
|
|
|
|
If the named extension is already loaded, the callback will be called
|
|
|
|
immediately.
|
|
|
|
|
|
|
|
If the named extension never loads, the callback will be called after
|
|
|
|
all extensions have been loaded.
|
|
|
|
|
|
|
|
The callback receives the named argument ``loaded``, which is a boolean
|
|
|
|
indicating whether the dependent extension actually loaded.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if extension in _extensions:
|
2017-06-23 20:59:05 +03:00
|
|
|
# Report loaded as False if the extension is disabled
|
|
|
|
loaded = (_extensions[extension] is not None)
|
|
|
|
callback(loaded=loaded)
|
2015-02-06 23:07:32 +03:00
|
|
|
else:
|
|
|
|
_aftercallbacks.setdefault(extension, []).append(callback)
|
|
|
|
|
2015-04-15 19:18:05 +03:00
|
|
|
def bind(func, *args):
|
|
|
|
'''Partial function application
|
|
|
|
|
|
|
|
Returns a new function that is the partial application of args and kwargs
|
|
|
|
to func. For example,
|
|
|
|
|
|
|
|
f(1, 2, bar=3) === bind(f, 1)(2, bar=3)'''
|
|
|
|
assert callable(func)
|
|
|
|
def closure(*a, **kw):
|
|
|
|
return func(*(args + a), **kw)
|
|
|
|
return closure
|
|
|
|
|
2016-08-10 17:21:42 +03:00
|
|
|
def _updatewrapper(wrap, origfn, unboundwrapper):
|
|
|
|
'''Copy and add some useful attributes to wrapper'''
|
2017-09-10 17:37:14 +03:00
|
|
|
try:
|
|
|
|
wrap.__name__ = origfn.__name__
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-01-09 13:45:10 +03:00
|
|
|
wrap.__module__ = getattr(origfn, '__module__')
|
|
|
|
wrap.__doc__ = getattr(origfn, '__doc__')
|
2016-01-09 14:04:03 +03:00
|
|
|
wrap.__dict__.update(getattr(origfn, '__dict__', {}))
|
2016-08-10 17:21:42 +03:00
|
|
|
wrap._origfunc = origfn
|
|
|
|
wrap._unboundwrapper = unboundwrapper
|
2016-01-09 13:45:10 +03:00
|
|
|
|
2015-02-09 22:02:45 +03:00
|
|
|
def wrapcommand(table, command, wrapper, synopsis=None, docstring=None):
|
2010-07-09 12:57:57 +04:00
|
|
|
'''Wrap the command named `command' in table
|
|
|
|
|
|
|
|
Replace command in the command table with wrapper. The wrapped command will
|
|
|
|
be inserted into the command table specified by the table argument.
|
|
|
|
|
|
|
|
The wrapper will be called like
|
|
|
|
|
|
|
|
wrapper(orig, *args, **kwargs)
|
|
|
|
|
|
|
|
where orig is the original (wrapped) function, and *args, **kwargs
|
|
|
|
are the arguments passed to it.
|
2015-02-09 22:02:45 +03:00
|
|
|
|
|
|
|
Optionally append to the command synopsis and docstring, used for help.
|
|
|
|
For example, if your extension wraps the ``bookmarks`` command to add the
|
|
|
|
flags ``--remote`` and ``--all`` you might call this function like so:
|
|
|
|
|
|
|
|
synopsis = ' [-a] [--remote]'
|
|
|
|
docstring = """
|
|
|
|
|
|
|
|
The ``remotenames`` extension adds the ``--remote`` and ``--all`` (``-a``)
|
|
|
|
flags to the bookmarks command. Either flag will show the remote bookmarks
|
2015-10-17 01:58:46 +03:00
|
|
|
known to the repository; ``--remote`` will also suppress the output of the
|
2015-02-09 22:02:45 +03:00
|
|
|
local bookmarks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
extensions.wrapcommand(commands.table, 'bookmarks', exbookmarks,
|
|
|
|
synopsis, docstring)
|
2010-07-09 12:57:57 +04:00
|
|
|
'''
|
2014-06-23 17:24:06 +04:00
|
|
|
assert callable(wrapper)
|
2008-10-23 02:34:50 +04:00
|
|
|
aliases, entry = cmdutil.findcmd(command, table)
|
|
|
|
for alias, e in table.iteritems():
|
|
|
|
if e is entry:
|
|
|
|
key = alias
|
|
|
|
break
|
|
|
|
|
|
|
|
origfn = entry[0]
|
wrapcommand: use functools.partial
Like the previous patch, this helps remove noises in traceback.
Practically, this removes another 6 lines in `rebase -s . -d .` traceback in
my setup:
....
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
-File "hg/mercurial/extensions.py", line 331, in closure
- return func(*(args + a), **kw)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
File "fb-hgext/hgext3rd/fbhistedit.py", line 283, in _rebase
return orig(ui, repo, **opts)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
-File "hg/mercurial/extensions.py", line 331, in closure
- return func(*(args + a), **kw)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
File "remotenames.py", line 633, in exrebasecmd
ret = orig(ui, repo, **opts)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
-File "hg/mercurial/extensions.py", line 331, in closure
- return func(*(args + a), **kw)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
File "fb-hgext/hgext3rd/fbamend/__init__.py", line 453, in wraprebase
return orig(ui, repo, **opts)
File "hg/mercurial/util.py", line 1118, in check
return func(*args, **kwargs)
....
Differential Revision: https://phab.mercurial-scm.org/D633
2017-09-06 01:18:45 +03:00
|
|
|
wrap = functools.partial(util.checksignature(wrapper),
|
|
|
|
util.checksignature(origfn))
|
2016-08-10 17:21:42 +03:00
|
|
|
_updatewrapper(wrap, origfn, wrapper)
|
2015-02-09 22:02:45 +03:00
|
|
|
if docstring is not None:
|
2016-01-09 13:45:10 +03:00
|
|
|
wrap.__doc__ += docstring
|
2015-02-09 22:02:45 +03:00
|
|
|
|
2008-10-23 02:34:50 +04:00
|
|
|
newentry = list(entry)
|
|
|
|
newentry[0] = wrap
|
2015-02-09 22:02:45 +03:00
|
|
|
if synopsis is not None:
|
|
|
|
newentry[2] += synopsis
|
2008-10-23 02:34:50 +04:00
|
|
|
table[key] = tuple(newentry)
|
|
|
|
return entry
|
|
|
|
|
2017-06-08 17:44:53 +03:00
|
|
|
def wrapfilecache(cls, propname, wrapper):
|
|
|
|
"""Wraps a filecache property.
|
|
|
|
|
|
|
|
These can't be wrapped using the normal wrapfunction.
|
|
|
|
"""
|
2017-07-26 05:49:43 +03:00
|
|
|
propname = pycompat.sysstr(propname)
|
2017-06-08 17:44:53 +03:00
|
|
|
assert callable(wrapper)
|
|
|
|
for currcls in cls.__mro__:
|
|
|
|
if propname in currcls.__dict__:
|
|
|
|
origfn = currcls.__dict__[propname].func
|
|
|
|
assert callable(origfn)
|
|
|
|
def wrap(*args, **kwargs):
|
|
|
|
return wrapper(origfn, *args, **kwargs)
|
|
|
|
currcls.__dict__[propname].func = wrap
|
|
|
|
break
|
|
|
|
|
|
|
|
if currcls is object:
|
2017-08-11 22:09:54 +03:00
|
|
|
raise AttributeError(r"type '%s' has no property '%s'" % (
|
|
|
|
cls, propname))
|
2017-06-08 17:44:53 +03:00
|
|
|
|
2017-08-22 02:46:05 +03:00
|
|
|
class wrappedfunction(object):
|
|
|
|
'''context manager for temporarily wrapping a function'''
|
|
|
|
|
|
|
|
def __init__(self, container, funcname, wrapper):
|
|
|
|
assert callable(wrapper)
|
|
|
|
self._container = container
|
|
|
|
self._funcname = funcname
|
|
|
|
self._wrapper = wrapper
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
wrapfunction(self._container, self._funcname, self._wrapper)
|
|
|
|
|
|
|
|
def __exit__(self, exctype, excvalue, traceback):
|
|
|
|
unwrapfunction(self._container, self._funcname, self._wrapper)
|
|
|
|
|
2008-10-23 02:34:50 +04:00
|
|
|
def wrapfunction(container, funcname, wrapper):
|
2010-06-15 21:04:22 +04:00
|
|
|
'''Wrap the function named funcname in container
|
|
|
|
|
2010-07-09 13:13:45 +04:00
|
|
|
Replace the funcname member in the given container with the specified
|
|
|
|
wrapper. The container is typically a module, class, or instance.
|
2010-06-15 21:04:22 +04:00
|
|
|
|
|
|
|
The wrapper will be called like
|
|
|
|
|
|
|
|
wrapper(orig, *args, **kwargs)
|
|
|
|
|
|
|
|
where orig is the original (wrapped) function, and *args, **kwargs
|
|
|
|
are the arguments passed to it.
|
|
|
|
|
|
|
|
Wrapping methods of the repository object is not recommended since
|
|
|
|
it conflicts with extensions that extend the repository by
|
|
|
|
subclassing. All extensions that need to extend methods of
|
|
|
|
localrepository should use this subclassing trick: namely,
|
|
|
|
reposetup() should look like
|
|
|
|
|
|
|
|
def reposetup(ui, repo):
|
|
|
|
class myrepo(repo.__class__):
|
|
|
|
def whatever(self, *args, **kwargs):
|
|
|
|
[...extension stuff...]
|
|
|
|
super(myrepo, self).whatever(*args, **kwargs)
|
|
|
|
[...extension stuff...]
|
|
|
|
|
|
|
|
repo.__class__ = myrepo
|
|
|
|
|
|
|
|
In general, combining wrapfunction() with subclassing does not
|
|
|
|
work. Since you cannot control what other extensions are loaded by
|
|
|
|
your end users, you should play nicely with others by using the
|
|
|
|
subclass trick.
|
|
|
|
'''
|
2014-06-23 17:24:06 +04:00
|
|
|
assert callable(wrapper)
|
2008-10-23 02:34:50 +04:00
|
|
|
|
|
|
|
origfn = getattr(container, funcname)
|
2014-06-23 17:24:06 +04:00
|
|
|
assert callable(origfn)
|
wrapfunction: use functools.partial if possible
Every `extensions.bind` call inserts a frame in traceback:
... in closure
return func(*(args + a), **kw)
which makes traceback noisy.
The Python stdlib has a `functools.partial` which is backed by C code and
does not pollute traceback. However it does not support instancemethod and
sets `args` attribute which could be problematic for alias handling.
This patch makes `wrapfunction` use `functools.partial` if we are wrapping a
function directly exported by a module (so it's impossible to be a class or
instance method), and special handles `wrapfunction` results so alias
handling code could handle `args` just fine.
As an example, `hg rebase -s . -d . --traceback` got 6 lines removed in my
setup:
File "hg/mercurial/dispatch.py", line 898, in _dispatch
cmdpats, cmdoptions)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "hg/hgext/journal.py", line 84, in runcommand
return orig(lui, repo, cmd, fullargs, *args)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "fb-hgext/hgext3rd/fbamend/hiddenoverride.py", line 119, in runcommand
result = orig(lui, repo, cmd, fullargs, *args)
File "hg/mercurial/dispatch.py", line 660, in runcommand
ret = _runcommand(ui, options, cmd, d)
-File "hg/mercurial/extensions.py", line 333, in closure
- return func(*(args + a), **kw)
File "hg/hgext/pager.py", line 69, in pagecmd
return orig(ui, options, cmd, cmdfunc)
....
Differential Revision: https://phab.mercurial-scm.org/D632
2017-09-05 23:37:36 +03:00
|
|
|
if inspect.ismodule(container):
|
|
|
|
# origfn is not an instance or class method. "partial" can be used.
|
|
|
|
# "partial" won't insert a frame in traceback.
|
|
|
|
wrap = functools.partial(wrapper, origfn)
|
|
|
|
else:
|
|
|
|
# "partial" cannot be safely used. Emulate its effect by using "bind".
|
|
|
|
# The downside is one more frame in traceback.
|
|
|
|
wrap = bind(wrapper, origfn)
|
2016-08-10 17:21:42 +03:00
|
|
|
_updatewrapper(wrap, origfn, wrapper)
|
2016-01-09 13:52:55 +03:00
|
|
|
setattr(container, funcname, wrap)
|
2008-10-23 02:34:50 +04:00
|
|
|
return origfn
|
2009-06-21 18:32:00 +04:00
|
|
|
|
2016-08-10 18:27:33 +03:00
|
|
|
def unwrapfunction(container, funcname, wrapper=None):
|
|
|
|
'''undo wrapfunction
|
|
|
|
|
|
|
|
If wrappers is None, undo the last wrap. Otherwise removes the wrapper
|
|
|
|
from the chain of wrappers.
|
|
|
|
|
|
|
|
Return the removed wrapper.
|
|
|
|
Raise IndexError if wrapper is None and nothing to unwrap; ValueError if
|
|
|
|
wrapper is not None but is not found in the wrapper chain.
|
|
|
|
'''
|
|
|
|
chain = getwrapperchain(container, funcname)
|
|
|
|
origfn = chain.pop()
|
|
|
|
if wrapper is None:
|
|
|
|
wrapper = chain[0]
|
|
|
|
chain.remove(wrapper)
|
|
|
|
setattr(container, funcname, origfn)
|
|
|
|
for w in reversed(chain):
|
|
|
|
wrapfunction(container, funcname, w)
|
|
|
|
return wrapper
|
|
|
|
|
2016-08-10 17:21:42 +03:00
|
|
|
def getwrapperchain(container, funcname):
|
|
|
|
'''get a chain of wrappers of a function
|
|
|
|
|
|
|
|
Return a list of functions: [newest wrapper, ..., oldest wrapper, origfunc]
|
|
|
|
|
|
|
|
The wrapper functions are the ones passed to wrapfunction, whose first
|
|
|
|
argument is origfunc.
|
|
|
|
'''
|
|
|
|
result = []
|
|
|
|
fn = getattr(container, funcname)
|
|
|
|
while fn:
|
|
|
|
assert callable(fn)
|
|
|
|
result.append(getattr(fn, '_unboundwrapper', fn))
|
|
|
|
fn = getattr(fn, '_origfunc', None)
|
|
|
|
return result
|
|
|
|
|
2010-02-07 16:01:43 +03:00
|
|
|
def _disabledpaths(strip_init=False):
|
|
|
|
'''find paths of disabled extensions. returns a dict of {name: path}
|
|
|
|
removes /__init__.py from packages if strip_init is True'''
|
2009-06-21 18:35:44 +04:00
|
|
|
import hgext
|
2017-02-20 16:10:42 +03:00
|
|
|
extpath = os.path.dirname(
|
|
|
|
os.path.abspath(pycompat.fsencode(hgext.__file__)))
|
2009-06-29 21:28:54 +04:00
|
|
|
try: # might not be a filesystem path
|
|
|
|
files = os.listdir(extpath)
|
|
|
|
except OSError:
|
2010-02-07 13:32:08 +03:00
|
|
|
return {}
|
2009-06-29 21:28:54 +04:00
|
|
|
|
2009-06-21 18:32:00 +04:00
|
|
|
exts = {}
|
2009-06-29 21:28:54 +04:00
|
|
|
for e in files:
|
2009-06-21 18:35:44 +04:00
|
|
|
if e.endswith('.py'):
|
|
|
|
name = e.rsplit('.', 1)[0]
|
|
|
|
path = os.path.join(extpath, e)
|
|
|
|
else:
|
|
|
|
name = e
|
|
|
|
path = os.path.join(extpath, e, '__init__.py')
|
2009-06-21 19:18:48 +04:00
|
|
|
if not os.path.exists(path):
|
|
|
|
continue
|
2010-02-07 16:01:43 +03:00
|
|
|
if strip_init:
|
|
|
|
path = os.path.dirname(path)
|
2009-06-21 19:19:34 +04:00
|
|
|
if name in exts or name in _order or name == '__init__':
|
2009-06-21 18:35:44 +04:00
|
|
|
continue
|
2010-02-07 13:32:08 +03:00
|
|
|
exts[name] = path
|
2017-07-07 10:13:53 +03:00
|
|
|
for name, path in _disabledextensions.iteritems():
|
|
|
|
# If no path was provided for a disabled extension (e.g. "color=!"),
|
|
|
|
# don't replace the path we already found by the scan above.
|
|
|
|
if path:
|
|
|
|
exts[name] = path
|
2010-02-07 13:32:08 +03:00
|
|
|
return exts
|
2009-06-21 18:32:00 +04:00
|
|
|
|
2011-05-13 20:04:51 +04:00
|
|
|
def _moduledoc(file):
|
|
|
|
'''return the top-level python documentation for the given file
|
|
|
|
|
|
|
|
Loosely inspired by pydoc.source_synopsis(), but rewritten to
|
|
|
|
handle triple quotes and to return the whole text instead of just
|
|
|
|
the synopsis'''
|
|
|
|
result = []
|
|
|
|
|
|
|
|
line = file.readline()
|
|
|
|
while line[:1] == '#' or not line.strip():
|
|
|
|
line = file.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
|
|
|
|
start = line[:3]
|
|
|
|
if start == '"""' or start == "'''":
|
|
|
|
line = line[3:]
|
|
|
|
while line:
|
|
|
|
if line.rstrip().endswith(start):
|
|
|
|
line = line.split(start)[0]
|
|
|
|
if line:
|
|
|
|
result.append(line)
|
|
|
|
break
|
|
|
|
elif not line:
|
|
|
|
return None # unmatched delimiter
|
|
|
|
result.append(line)
|
|
|
|
line = file.readline()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return ''.join(result)
|
|
|
|
|
2010-02-07 13:32:08 +03:00
|
|
|
def _disabledhelp(path):
|
|
|
|
'''retrieve help synopsis of a disabled extension (without importing)'''
|
|
|
|
try:
|
|
|
|
file = open(path)
|
|
|
|
except IOError:
|
|
|
|
return
|
|
|
|
else:
|
2011-05-13 21:57:27 +04:00
|
|
|
doc = _moduledoc(file)
|
2010-02-07 13:32:08 +03:00
|
|
|
file.close()
|
2009-06-21 18:35:44 +04:00
|
|
|
|
2010-02-07 13:32:08 +03:00
|
|
|
if doc: # extracting localized synopsis
|
2016-11-06 04:24:31 +03:00
|
|
|
return gettext(doc)
|
2010-02-07 13:32:08 +03:00
|
|
|
else:
|
|
|
|
return _('(no help text available)')
|
|
|
|
|
|
|
|
def disabled():
|
2011-06-04 15:01:01 +04:00
|
|
|
'''find disabled extensions from hgext. returns a dict of {name: desc}'''
|
2011-06-04 15:19:30 +04:00
|
|
|
try:
|
|
|
|
from hgext import __index__
|
|
|
|
return dict((name, gettext(desc))
|
|
|
|
for name, desc in __index__.docs.iteritems()
|
2018-01-09 04:01:31 +03:00
|
|
|
if name not in _order and name not in _blacklist)
|
2014-05-05 18:54:15 +04:00
|
|
|
except (ImportError, AttributeError):
|
2011-06-04 15:19:30 +04:00
|
|
|
pass
|
|
|
|
|
2010-02-07 13:32:08 +03:00
|
|
|
paths = _disabledpaths()
|
|
|
|
if not paths:
|
2012-05-13 13:06:07 +04:00
|
|
|
return {}
|
2010-02-07 13:32:08 +03:00
|
|
|
|
|
|
|
exts = {}
|
|
|
|
for name, path in paths.iteritems():
|
|
|
|
doc = _disabledhelp(path)
|
2018-01-09 04:01:31 +03:00
|
|
|
if doc and name not in _blacklist:
|
2016-11-06 04:24:31 +03:00
|
|
|
exts[name] = doc.splitlines()[0]
|
2009-06-21 18:32:00 +04:00
|
|
|
|
2011-05-13 20:04:51 +04:00
|
|
|
return exts
|
2009-06-21 18:32:00 +04:00
|
|
|
|
2010-02-07 16:01:43 +03:00
|
|
|
def disabledext(name):
|
|
|
|
'''find a specific disabled extension from hgext. returns desc'''
|
2011-06-04 15:19:30 +04:00
|
|
|
try:
|
|
|
|
from hgext import __index__
|
|
|
|
if name in _order: # enabled
|
|
|
|
return
|
2018-01-09 04:01:31 +03:00
|
|
|
elif name in _blacklist: # blacklisted
|
|
|
|
return
|
2011-06-04 15:19:30 +04:00
|
|
|
else:
|
|
|
|
return gettext(__index__.docs.get(name))
|
2014-05-05 18:54:15 +04:00
|
|
|
except (ImportError, AttributeError):
|
2011-06-04 15:19:30 +04:00
|
|
|
pass
|
|
|
|
|
2010-02-07 16:01:43 +03:00
|
|
|
paths = _disabledpaths()
|
2018-01-09 04:01:31 +03:00
|
|
|
if name in paths and name not in _blacklist:
|
2010-02-07 16:01:43 +03:00
|
|
|
return _disabledhelp(paths[name])
|
|
|
|
|
2010-12-26 02:43:49 +03:00
|
|
|
def disabledcmd(ui, cmd, strict=False):
|
2010-02-07 16:01:43 +03:00
|
|
|
'''import disabled extensions until cmd is found.
|
2012-05-11 13:33:33 +04:00
|
|
|
returns (cmdname, extname, module)'''
|
2010-02-07 16:01:43 +03:00
|
|
|
|
|
|
|
paths = _disabledpaths(strip_init=True)
|
|
|
|
if not paths:
|
|
|
|
raise error.UnknownCommand(cmd)
|
|
|
|
|
|
|
|
def findcmd(cmd, name, path):
|
|
|
|
try:
|
|
|
|
mod = loadpath(path, 'hgext.%s' % name)
|
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
aliases, entry = cmdutil.findcmd(cmd,
|
|
|
|
getattr(mod, 'cmdtable', {}), strict)
|
|
|
|
except (error.AmbiguousCommand, error.UnknownCommand):
|
|
|
|
return
|
2010-12-26 02:43:49 +03:00
|
|
|
except Exception:
|
|
|
|
ui.warn(_('warning: error finding commands in %s\n') % path)
|
|
|
|
ui.traceback()
|
|
|
|
return
|
2010-02-07 16:01:43 +03:00
|
|
|
for c in aliases:
|
|
|
|
if c.startswith(cmd):
|
|
|
|
cmd = c
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
cmd = aliases[0]
|
|
|
|
return (cmd, name, mod)
|
|
|
|
|
2012-05-11 16:00:51 +04:00
|
|
|
ext = None
|
2010-02-07 16:01:43 +03:00
|
|
|
# first, search for an extension with the same name as the command
|
|
|
|
path = paths.pop(cmd, None)
|
|
|
|
if path:
|
|
|
|
ext = findcmd(cmd, cmd, path)
|
2012-05-11 16:00:51 +04:00
|
|
|
if not ext:
|
|
|
|
# otherwise, interrogate each extension until there's a match
|
|
|
|
for name, path in paths.iteritems():
|
|
|
|
ext = findcmd(cmd, name, path)
|
|
|
|
if ext:
|
|
|
|
break
|
|
|
|
if ext and 'DEPRECATED' not in ext.__doc__:
|
|
|
|
return ext
|
2010-02-07 16:01:43 +03:00
|
|
|
|
|
|
|
raise error.UnknownCommand(cmd)
|
|
|
|
|
2013-09-23 15:23:25 +04:00
|
|
|
def enabled(shortname=True):
|
2011-06-04 15:01:01 +04:00
|
|
|
'''return a dict of {name: desc} of extensions'''
|
2009-06-21 18:32:00 +04:00
|
|
|
exts = {}
|
|
|
|
for ename, ext in extensions():
|
|
|
|
doc = (gettext(ext.__doc__) or _('(no help text available)'))
|
2013-09-23 15:23:25 +04:00
|
|
|
if shortname:
|
|
|
|
ename = ename.split('.')[-1]
|
2009-07-13 04:50:26 +04:00
|
|
|
exts[ename] = doc.splitlines()[0].strip()
|
2009-06-21 18:32:00 +04:00
|
|
|
|
2011-05-13 20:04:51 +04:00
|
|
|
return exts
|
2014-06-10 14:44:37 +04:00
|
|
|
|
2016-02-10 19:59:34 +03:00
|
|
|
def notloaded():
|
|
|
|
'''return short names of extensions that failed to load'''
|
|
|
|
return [name for name, mod in _extensions.iteritems() if mod is None]
|
|
|
|
|
2014-06-10 14:44:37 +04:00
|
|
|
def moduleversion(module):
|
|
|
|
'''return version information from given module as a string'''
|
|
|
|
if (util.safehasattr(module, 'getversion')
|
|
|
|
and callable(module.getversion)):
|
|
|
|
version = module.getversion()
|
|
|
|
elif util.safehasattr(module, '__version__'):
|
|
|
|
version = module.__version__
|
|
|
|
else:
|
|
|
|
version = ''
|
|
|
|
if isinstance(version, (list, tuple)):
|
|
|
|
version = '.'.join(str(o) for o in version)
|
|
|
|
return version
|
2016-02-05 15:20:23 +03:00
|
|
|
|
|
|
|
def ismoduleinternal(module):
|
|
|
|
exttestedwith = getattr(module, 'testedwith', None)
|
2016-08-23 18:26:08 +03:00
|
|
|
return exttestedwith == "ships-with-hg-core"
|