| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Sith attacks (and helps debugging) Jedi. |
| 5 |
|
| 6 |
Randomly search Python files and run Jedi on it. Exception and used |
| 7 |
arguments are recorded to ``./record.json`` (specified by --record):: |
| 8 |
|
| 9 |
./sith.py random /path/to/sourcecode |
| 10 |
|
| 11 |
Redo recorded exception:: |
| 12 |
|
| 13 |
./sith.py redo |
| 14 |
|
| 15 |
Show recorded exception:: |
| 16 |
|
| 17 |
./sith.py show |
| 18 |
|
| 19 |
Run a specific operation |
| 20 |
|
| 21 |
./sith.py run <operation> </path/to/source/file.py> <line> <col> |
| 22 |
|
| 23 |
Where operation is one of complete, goto, infer, get_references or get_signatures. |
| 24 |
|
| 25 |
Note: Line numbers start at 1; columns start at 0 (this is consistent with |
| 26 |
many text editors, including Emacs). |
| 27 |
|
| 28 |
Usage: |
| 29 |
sith.py [--pdb|--ipdb|--pudb] [-d] [-n=<nr>] [-f] [--record=<file>] random [-s] [<path>] |
| 30 |
sith.py [--pdb|--ipdb|--pudb] [-d] [-f] [--record=<file>] redo |
| 31 |
sith.py [--pdb|--ipdb|--pudb] [-d] [-f] run <operation> <path> <line> <column> |
| 32 |
sith.py show [--record=<file>] |
| 33 |
sith.py -h | --help |
| 34 |
|
| 35 |
Options: |
| 36 |
-h --help Show this screen. |
| 37 |
--record=<file> Exceptions are recorded in here [default: record.json]. |
| 38 |
-n, --maxtries=<nr> Maximum of random tries [default: 100] |
| 39 |
-d, --debug Jedi print debugging when an error is raised. |
| 40 |
-s Shows the path/line numbers of every completion before it starts. |
| 41 |
--pdb Launch pdb when error is raised. |
| 42 |
--ipdb Launch ipdb when error is raised. |
| 43 |
--pudb Launch pudb when error is raised. |
| 44 |
""" |
| 45 |
|
| 46 |
from docopt import docopt |
| 47 |
|
| 48 |
import json |
| 49 |
import os |
| 50 |
import random |
| 51 |
import sys |
| 52 |
import traceback |
| 53 |
|
| 54 |
import jedi |
| 55 |
|
| 56 |
|
| 57 |
class SourceFinder(object): |
| 58 |
_files = None |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def fetch(file_path): |
| 62 |
if not os.path.isdir(file_path): |
| 63 |
yield file_path |
| 64 |
return |
| 65 |
for root, dirnames, filenames in os.walk(file_path): |
| 66 |
for name in filenames: |
| 67 |
if name.endswith('.py'): |
| 68 |
yield os.path.join(root, name) |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def files(cls, file_path): |
| 72 |
if cls._files is None: |
| 73 |
cls._files = list(cls.fetch(file_path)) |
| 74 |
return cls._files |
| 75 |
|
| 76 |
|
| 77 |
class TestCase(object): |
| 78 |
def __init__(self, operation, path, line, column, traceback=None): |
| 79 |
if operation not in self.operations: |
| 80 |
raise ValueError("%s is not a valid operation" % operation) |
| 81 |
|
| 82 |
|
| 83 |
self.operation = operation |
| 84 |
self.path = path |
| 85 |
self.line = line |
| 86 |
self.column = column |
| 87 |
self.traceback = traceback |
| 88 |
|
| 89 |
@classmethod |
| 90 |
def from_cache(cls, record): |
| 91 |
with open(record) as f: |
| 92 |
args = json.load(f) |
| 93 |
return cls(*args) |
| 94 |
|
| 95 |
|
| 96 |
operations = ['complete', 'goto', 'infer', 'get_references', 'get_signatures'] |
| 97 |
|
| 98 |
@classmethod |
| 99 |
def generate(cls, file_path): |
| 100 |
operation = random.choice(cls.operations) |
| 101 |
|
| 102 |
path = random.choice(SourceFinder.files(file_path)) |
| 103 |
with open(path) as f: |
| 104 |
source = f.read() |
| 105 |
lines = source.splitlines() |
| 106 |
|
| 107 |
if not lines: |
| 108 |
lines = [''] |
| 109 |
line = random.randint(1, len(lines)) |
| 110 |
line_string = lines[line - 1] |
| 111 |
line_len = len(line_string) |
| 112 |
if line_string.endswith('\r\n'): |
| 113 |
line_len -= 1 |
| 114 |
if line_string.endswith('\n'): |
| 115 |
line_len -= 1 |
| 116 |
column = random.randint(0, line_len) |
| 117 |
return cls(operation, path, line, column) |
| 118 |
|
| 119 |
def run(self, debugger, record=None, print_result=False): |
| 120 |
try: |
| 121 |
with open(self.path) as f: |
| 122 |
self.script = jedi.Script(f.read(), path=self.path) |
| 123 |
kwargs = {} |
| 124 |
if self.operation == 'goto': |
| 125 |
kwargs['follow_imports'] = random.choice([False, True]) |
| 126 |
|
| 127 |
self.objects = getattr(self.script, self.operation)(self.line, self.column, **kwargs) |
| 128 |
if print_result: |
| 129 |
print("{path}: Line {line} column {column}".format(**self.__dict__)) |
| 130 |
self.show_location(self.line, self.column) |
| 131 |
self.show_operation() |
| 132 |
except Exception: |
| 133 |
self.traceback = traceback.format_exc() |
| 134 |
if record is not None: |
| 135 |
call_args = (self.operation, self.path, self.line, self.column, self.traceback) |
| 136 |
with open(record, 'w') as f: |
| 137 |
json.dump(call_args, f) |
| 138 |
self.show_errors() |
| 139 |
if debugger: |
| 140 |
einfo = sys.exc_info() |
| 141 |
pdb = __import__(debugger) |
| 142 |
if debugger == 'pudb': |
| 143 |
pdb.post_mortem(einfo[2], einfo[0], einfo[1]) |
| 144 |
else: |
| 145 |
pdb.post_mortem(einfo[2]) |
| 146 |
exit(1) |
| 147 |
|
| 148 |
def show_location(self, lineno, column, show=3): |
| 149 |
|
| 150 |
lower = lineno - show if lineno - show > 0 else 0 |
| 151 |
prefix = ' |' |
| 152 |
for i, line in enumerate(self.script._code.split('\n')[lower:lineno]): |
| 153 |
print(prefix, lower + i + 1, line) |
| 154 |
print(prefix, ' ' * (column + len(str(lineno))), '^') |
| 155 |
|
| 156 |
def show_operation(self): |
| 157 |
print("%s:\n" % self.operation.capitalize()) |
| 158 |
if self.operation == 'complete': |
| 159 |
self.show_completions() |
| 160 |
else: |
| 161 |
self.show_definitions() |
| 162 |
|
| 163 |
def show_completions(self): |
| 164 |
for completion in self.objects: |
| 165 |
print(completion.name) |
| 166 |
|
| 167 |
def show_definitions(self): |
| 168 |
for completion in self.objects: |
| 169 |
print(completion.full_name) |
| 170 |
if completion.module_path is None: |
| 171 |
continue |
| 172 |
if os.path.abspath(completion.module_path) == os.path.abspath(self.path): |
| 173 |
self.show_location(completion.line, completion.column) |
| 174 |
|
| 175 |
def show_errors(self): |
| 176 |
sys.stderr.write(self.traceback) |
| 177 |
print(("Error with running Script(...).{operation}() with\n" |
| 178 |
"\tpath: {path}\n" |
| 179 |
"\tline: {line}\n" |
| 180 |
"\tcolumn: {column}").format(**self.__dict__)) |
| 181 |
|
| 182 |
|
| 183 |
def main(arguments): |
| 184 |
debugger = 'pdb' if arguments['--pdb'] else \ |
| 185 |
'ipdb' if arguments['--ipdb'] else \ |
| 186 |
'pudb' if arguments['--pudb'] else None |
| 187 |
record = arguments['--record'] |
| 188 |
|
| 189 |
if arguments['--debug']: |
| 190 |
jedi.set_debug_function() |
| 191 |
|
| 192 |
if arguments['redo'] or arguments['show']: |
| 193 |
t = TestCase.from_cache(record) |
| 194 |
if arguments['show']: |
| 195 |
t.show_errors() |
| 196 |
else: |
| 197 |
t.run(debugger) |
| 198 |
elif arguments['run']: |
| 199 |
TestCase( |
| 200 |
arguments['<operation>'], arguments['<path>'], |
| 201 |
int(arguments['<line>']), int(arguments['<column>']) |
| 202 |
).run(debugger, print_result=True) |
| 203 |
else: |
| 204 |
for _ in range(int(arguments['--maxtries'])): |
| 205 |
t = TestCase.generate(arguments['<path>'] or '.') |
| 206 |
if arguments['-s']: |
| 207 |
print('%s %s %s %s ' % (t.operation, t.path, t.line, t.column)) |
| 208 |
sys.stdout.flush() |
| 209 |
else: |
| 210 |
print('.', end='') |
| 211 |
t.run(debugger, record) |
| 212 |
|
| 213 |
sys.stdout.flush() |
| 214 |
print() |
| 215 |
|
| 216 |
|
| 217 |
if __name__ == '__main__': |
| 218 |
arguments = docopt(__doc__) |
| 219 |
main(arguments) |