contrib/debugshell.py
author Valentin Gatien-Baron <vgatien-baron@janestreet.com>
Wed, 21 Nov 2018 13:08:23 -0500
changeset 40687 dd028bca9221
parent 32337 46ba2cdda476
child 41759 aaad36b88298
permissions -rw-r--r--
tests: make test-check-module-imports more robust It failed for me without this in this way: tests/test-commandserver.t:19: relative import of stdlib module tests/test-lfs-serve.t:108: relative import of stdlib module tests/test-lfs-serve.t:255: relative import of stdlib module tests/test-lfs-serve.t:362: relative import of stdlib module tests/test-lfs-serve.t:406: relative import of stdlib module tests/test-lock.py:9: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-lrucachedict.py:5: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-match.py:5: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-remotefilelog-datapack.py:15: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-remotefilelog-histpack.py:14: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-simplekeyvaluefile.py:4: imports not lexically sorted: silenttestrunner < unittest, True, True tests/test-sshserver.py:6: imports not lexically sorted: silenttestrunner < unittest, True, True This is because every module is considered a stdlib module, because the stdlib_prefixes is /usr, and my repo is in /usr/local/home, which means that sys.path contains a couple of /usr/local/home/../hg/.. entries that count as "in the stdlib". Fix this by preventing any path in sys.path that's inside the mercurial source from being considered "in the stdlib". Differential Revision: https://phab.mercurial-scm.org/D5294
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,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30375
diff changeset
    10
    registrar,
27721
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    11
)
21243
8b5c039f2b4f debugshell: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19794
diff changeset
    12
8b5c039f2b4f debugshell: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19794
diff changeset
    13
cmdtable = {}
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30375
diff changeset
    14
command = registrar.command(cmdtable)
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    15
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    16
def pdb(ui, repo, msg, **opts):
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    17
    objects = {
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    18
        'mercurial': mercurial,
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    19
        'repo': repo,
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    20
        'cl': repo.changelog,
30375
11b8b740d54a manifest: remove last uses of repo.manifest
Durham Goode <durham@fb.com>
parents: 29397
diff changeset
    21
        'mf': repo.manifestlog,
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    22
    }
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    23
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    24
    code.interact(msg, local=objects)
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    25
19772
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    26
def ipdb(ui, repo, msg, **opts):
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    27
    import IPython
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    28
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    29
    cl = repo.changelog
30375
11b8b740d54a manifest: remove last uses of repo.manifest
Durham Goode <durham@fb.com>
parents: 29397
diff changeset
    30
    mf = repo.manifestlog
19794
cccc44304b2c debugshell: appease pyflakes
Matt Mackall <mpm@selenic.com>
parents: 19773
diff changeset
    31
    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
    32
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    33
    IPython.embed()
6ccec36a1fd9 debugshell: add function to embed ipython
Sean Farley <sean.michael.farley@gmail.com>
parents: 19771
diff changeset
    34
21243
8b5c039f2b4f debugshell: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19794
diff changeset
    35
@command('debugshell|dbsh', [])
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    36
def debugshell(ui, repo, **opts):
11633
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    37
    bannermsg = "loaded repo : %s\n" \
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    38
                "using source: %s" % (repo.root,
6b7b99867ada contrib: add debugshell extension
Vishakh H <vsh426@gmail.com>
parents:
diff changeset
    39
                                      mercurial.__path__[0])
19771
3bc675361206 debugshell: abstract out pdb code.interact
Sean Farley <sean.michael.farley@gmail.com>
parents: 11633
diff changeset
    40
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    41
    pdbmap = {
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    42
        'pdb'  : 'code',
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    43
        'ipdb' : 'IPython'
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    44
    }
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    45
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    46
    debugger = ui.config("ui", "debugger")
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    47
    if not debugger:
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    48
        debugger = 'pdb'
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    49
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    50
    # 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
    51
    try:
27721
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    52
        with demandimport.deactivated():
e4b512bb6386 debugshell: disable demand importer when importing debugger
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21243
diff changeset
    53
            __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
    54
    except ImportError:
29397
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28476
diff changeset
    55
        ui.warn(("%s debugger specified but %s module was not found\n")
19773
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    56
                % (debugger, pdbmap[debugger]))
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    57
        debugger = 'pdb'
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    58
51799a965446 debugshell: check ui.debugger for which debugger to use
Sean Farley <sean.michael.farley@gmail.com>
parents: 19772
diff changeset
    59
    getattr(sys.modules[__name__], debugger)(ui, repo, bannermsg, **opts)