debugshell.py
65 lines
| 1.5 KiB
| text/x-python
|
PythonLexer
/ contrib / debugshell.py
|
r11633 | # debugshell extension | ||
"""a python shell with repo, changelog & manifest objects""" | ||||
|
r28476 | from __future__ import absolute_import | ||
import code | ||||
|
r11633 | import mercurial | ||
|
r28476 | import sys | ||
|
r27721 | from mercurial import ( | ||
demandimport, | ||||
|
r41980 | pycompat, | ||
|
r32337 | registrar, | ||
|
r27721 | ) | ||
|
r21243 | |||
cmdtable = {} | ||||
|
r32337 | command = registrar.command(cmdtable) | ||
|
r11633 | |||
|
r43346 | |||
|
r19771 | def pdb(ui, repo, msg, **opts): | ||
|
r11633 | objects = { | ||
'mercurial': mercurial, | ||||
'repo': repo, | ||||
'cl': repo.changelog, | ||||
|
r30375 | 'mf': repo.manifestlog, | ||
|
r11633 | } | ||
|
r19771 | |||
code.interact(msg, local=objects) | ||||
|
r43346 | |||
|
r19772 | def ipdb(ui, repo, msg, **opts): | ||
import IPython | ||||
cl = repo.changelog | ||||
|
r30375 | mf = repo.manifestlog | ||
|
r43346 | cl, mf # use variables to appease pyflakes | ||
|
r19772 | |||
IPython.embed() | ||||
|
r43346 | |||
|
r41980 | @command(b'debugshell|dbsh', []) | ||
|
r19771 | def debugshell(ui, repo, **opts): | ||
|
r43346 | bannermsg = "loaded repo : %s\n" "using source: %s" % ( | ||
pycompat.sysstr(repo.root), | ||||
mercurial.__path__[0], | ||||
) | ||||
|
r19771 | |||
|
r43346 | pdbmap = {'pdb': 'code', 'ipdb': 'IPython'} | ||
|
r19773 | |||
|
r41980 | debugger = ui.config(b"ui", b"debugger") | ||
|
r19773 | if not debugger: | ||
debugger = 'pdb' | ||||
|
r41980 | else: | ||
debugger = pycompat.sysstr(debugger) | ||||
|
r19773 | |||
# if IPython doesn't exist, fallback to code.interact | ||||
try: | ||||
|
r27721 | with demandimport.deactivated(): | ||
__import__(pdbmap[debugger]) | ||||
|
r19773 | except ImportError: | ||
|
r43346 | ui.warn( | ||
b"%s debugger specified but %s module was not found\n" | ||||
% (debugger, pdbmap[debugger]) | ||||
) | ||||
|
r41980 | debugger = b'pdb' | ||
|
r19773 | |||
getattr(sys.modules[__name__], debugger)(ui, repo, bannermsg, **opts) | ||||