/
port_already_in_use.py
40 lines (30 loc) · 1.04 KB
/
port_already_in_use.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import re
from subprocess import Popen, PIPE
from thefuck.utils import memoize, which
from thefuck.shells import shell
enabled_by_default = bool(which('lsof'))
patterns = [r"bind on address \('.*', (?P<port>\d+)\)",
r'Unable to bind [^ ]*:(?P<port>\d+)',
r"can't listen on port (?P<port>\d+)",
r'listen EADDRINUSE [^ ]*:(?P<port>\d+)']
@memoize
def _get_pid_by_port(port):
proc = Popen(['lsof', '-i', ':{}'.format(port)], stdout=PIPE)
lines = proc.stdout.read().decode().split('\n')
if len(lines) > 1:
return lines[1].split()[1]
else:
return None
@memoize
def _get_used_port(command):
for pattern in patterns:
matched = re.search(pattern, command.output)
if matched:
return matched.group('port')
def match(command):
port = _get_used_port(command)
return port and _get_pid_by_port(port)
def get_new_command(command):
port = _get_used_port(command)
pid = _get_pid_by_port(port)
return shell.and_(u'kill {}'.format(pid), command.script)