|
1 | 1 | import argparse |
| 2 | +from contextlib import ExitStack |
2 | 3 | import base64 |
3 | 4 | import hashlib |
4 | 5 | import json |
|
14 | 15 | from . import polyfile |
15 | 16 | from .fileutils import PathOrStdin |
16 | 17 | from .magic import MagicMatcher, MatchContext |
| 18 | +from .magic_debugger import Debugger |
17 | 19 | from .polyfile import __version__ |
18 | 20 |
|
19 | 21 |
|
@@ -54,6 +56,11 @@ def main(argv=None): |
54 | 56 | help='stop scanning after having found this many matches') |
55 | 57 | parser.add_argument('--debug', '-d', action='store_true', help='print debug information') |
56 | 58 | parser.add_argument('--trace', '-dd', action='store_true', help='print extra verbose debug information') |
| 59 | + parser.add_argument('--debugger', '-db', action='store_true', help='drop into an interactive debugger for libmagic ' |
| 60 | + 'file definition matching') |
| 61 | + parser.add_argument('--no-debug-python', action='store_true', help='by default, the `--debugger` option will break ' |
| 62 | + 'on custom matchers and prompt to debug using ' |
| 63 | + 'PDB. This option will suppress those prompts.') |
57 | 64 | parser.add_argument('--quiet', '-q', action='store_true', help='suppress all log output (overrides --debug)') |
58 | 65 | parser.add_argument('--version', '-v', action='store_true', help='print PolyFile\'s version information to STDERR') |
59 | 66 | parser.add_argument('-dumpversion', action='store_true', |
@@ -110,7 +117,11 @@ def main(argv=None): |
110 | 117 | exit(1) |
111 | 118 | return # this is here because linters are dumb and will complain about the next line without it |
112 | 119 |
|
113 | | - with path_or_stdin as file_path: |
| 120 | + with path_or_stdin as file_path, ExitStack() as stack: |
| 121 | + if args.debugger: |
| 122 | + stack.enter_context(Debugger(break_on_submatching=not args.no_debug_python)) |
| 123 | + elif args.no_debug_python: |
| 124 | + log.warning("Ignoring `--no-debug-python`; it can only be used with the --debugger option.") |
114 | 125 | matches = [] |
115 | 126 | try: |
116 | 127 | if args.only_match_mime: |
|
0 commit comments