#N/A: Add port_already_in_use rule
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import re
|
||||
from subprocess import Popen, PIPE
|
||||
from thefuck.utils import memoize
|
||||
from thefuck.shells import shell
|
||||
|
||||
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.stderr)
|
||||
or re.search(pattern, command.stdout))
|
||||
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)
|
||||
Reference in New Issue
Block a user