contrib/debugshell.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 10 Feb 2021 23:21:21 +0100
branchstable
changeset 46492 7289eac777ec
parent 43080 86e4daa2d54c
child 48875 6000f5b25c9b
permissions -rw-r--r--
hooks: introduce a `:run-with-plain` option for hooks This option control if HGPLAIN should be set or not for the hooks. This is the first step to give user some control of the HGPLAIN setting for they hooks. Some hooks (eg: consistency checking) deserve to be run with HGPLAIN, some other (eg: user set visual helper) might need to respect the user config and setting. So both usage are valid and we need to restore the ability to run -without- HGPLAIN that got lost in Mercurial 5.7. This does not offer a way to restore the pre-5.7 behavior yet (respect whatever HGPLAIN setting from the shell), this will be dealt with in the next changeset. The option name is a bit verbose because implementing this highlighs the need for another option: `:run-if-plain`. That would make it possible for some hooks to be easily disabled if HG PLAIN is set. However such option would be a new feature, not something introduced to mitigate a behavior change introduced in 5.7, so the `:run-if-plain` option belong to the default branch and is not part of this series. Differential Revision: https://phab.mercurial-scm.org/D9981
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
     1
# debugshell extension
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
     2
"""a python shell with repo, changelog & manifest objects"""
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
     3
28476
e28dc6de38e7 debugshell: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27721
diff changeset
     4
from __future__ import absolute_import
e28dc6de38e7 debugshell: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27721
diff changeset
     5
import code
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
     6
import mercurial
28476
e28dc6de38e7 debugshell: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27721
diff changeset
     7
import sys
27721
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
     8
from mercurial import (
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
     9
    demandimport,
41814
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    10
    pycompat,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30375
diff changeset
    11
    registrar,
27721
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    12
)
21243
8b5c039f2b4f debugshell: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19794
diff changeset
    13
8b5c039f2b4f debugshell: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19794
diff changeset
    14
cmdtable = {}
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30375
diff changeset
    15
command = registrar.command(cmdtable)
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    17
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    18
def pdb(ui, repo, msg, **opts):
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    19
    objects = {
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    20
        'mercurial': mercurial,
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    21
        'repo': repo,
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    22
        'cl': repo.changelog,
30375
11b8b740d54a manifest: remove last uses of repo.manifest
Durham Goode <durham@fb.com>
parents: 29397
diff changeset
    23
        'mf': repo.manifestlog,
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    24
    }
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    25
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    26
    code.interact(msg, local=objects)
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    28
19772
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    29
def ipdb(ui, repo, msg, **opts):
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    30
    import IPython
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    31
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    32
    cl = repo.changelog
30375
11b8b740d54a manifest: remove last uses of repo.manifest
Durham Goode <durham@fb.com>
parents: 29397
diff changeset
    33
    mf = repo.manifestlog
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    34
    cl, mf  # use variables to appease pyflakes
19772
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    35
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    36
    IPython.embed()
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    37
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    38
41814
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    39
@command(b'debugshell|dbsh', [])
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    40
def debugshell(ui, repo, **opts):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    41
    bannermsg = "loaded repo : %s\n" "using source: %s" % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    42
        pycompat.sysstr(repo.root),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    43
        mercurial.__path__[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    44
    )
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    45
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    46
    pdbmap = {'pdb': 'code', 'ipdb': 'IPython'}
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    47
41814
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    48
    debugger = ui.config(b"ui", b"debugger")
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    49
    if not debugger:
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    50
        debugger = 'pdb'
41814
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    51
    else:
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    52
        debugger = pycompat.sysstr(debugger)
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    53
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    54
    # if IPython doesn't exist, fallback to code.interact
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    55
    try:
27721
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    56
        with demandimport.deactivated():
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    57
            __import__(pdbmap[debugger])
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    58
    except ImportError:
43080
86e4daa2d54c cleanup: mark some ui.(status|note|warn|write) calls as not needing i18n
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    59
        ui.warnnoi18n(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    60
            b"%s debugger specified but %s module was not found\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    61
            % (debugger, pdbmap[debugger])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41814
diff changeset
    62
        )
41814
b10bbbe995eb py3: make contrib/debugshell.py work with Python 3
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41759
diff changeset
    63
        debugger = b'pdb'
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    64
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    65
    getattr(sys.modules[__name__], debugger)(ui, repo, bannermsg, **opts)