1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-04-15 07:10:52 +01:00

Merge pull request #352 from mcarton/slow

Fix slowness problems II
This commit is contained in:
Vladimir Iakovlev 2015-09-02 09:36:48 +03:00
commit 50207d8180
20 changed files with 117 additions and 125 deletions

View File

@ -1,6 +1,6 @@
import pytest import pytest
from thefuck.rules.brew_install import match, get_new_command from thefuck.rules.brew_install import match, get_new_command
from thefuck.rules.brew_install import brew_formulas from thefuck.rules.brew_install import _get_formulas
from tests.utils import Command from tests.utils import Command
@ -20,9 +20,7 @@ def brew_already_installed():
def _is_not_okay_to_test(): def _is_not_okay_to_test():
if 'elasticsearch' not in brew_formulas: return 'elasticsearch' not in _get_formulas()
return True
return False
@pytest.mark.skipif(_is_not_okay_to_test(), @pytest.mark.skipif(_is_not_okay_to_test(),

View File

@ -1,6 +1,6 @@
import pytest import pytest
from thefuck.rules.brew_unknown_command import match, get_new_command from thefuck.rules.brew_unknown_command import match, get_new_command
from thefuck.rules.brew_unknown_command import brew_commands from thefuck.rules.brew_unknown_command import _brew_commands
from tests.utils import Command from tests.utils import Command
@ -16,7 +16,7 @@ def brew_unknown_cmd2():
def test_match(brew_unknown_cmd): def test_match(brew_unknown_cmd):
assert match(Command('brew inst', stderr=brew_unknown_cmd), None) assert match(Command('brew inst', stderr=brew_unknown_cmd), None)
for command in brew_commands: for command in _brew_commands():
assert not match(Command('brew ' + command), None) assert not match(Command('brew ' + command), None)
@ -24,5 +24,6 @@ def test_get_new_command(brew_unknown_cmd, brew_unknown_cmd2):
assert get_new_command(Command('brew inst', stderr=brew_unknown_cmd), assert get_new_command(Command('brew inst', stderr=brew_unknown_cmd),
None) == ['brew list', 'brew install', 'brew uninstall'] None) == ['brew list', 'brew install', 'brew uninstall']
assert get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2), cmds = get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2), None)
None) == ['brew install', 'brew uninstall', 'brew list'] assert 'brew install' in cmds
assert 'brew uninstall' in cmds

View File

@ -1,20 +1,22 @@
import sys from . import conf, types, logs
from imp import load_source from imp import load_source
from pathlib import Path from pathlib import Path
from . import conf, types, logs from thefuck.types import CorrectedCommand, Rule
import sys
def load_rule(rule, settings): def load_rule(rule, settings):
"""Imports rule module and returns it.""" """Imports rule module and returns it."""
name = rule.name[:-3] name = rule.name[:-3]
rule_module = load_source(name, str(rule)) with logs.debug_time(u'Importing rule: {};'.format(name), settings):
priority = getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY) rule_module = load_source(name, str(rule))
return types.Rule(name, rule_module.match, priority = getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY)
rule_module.get_new_command, return Rule(name, rule_module.match,
getattr(rule_module, 'enabled_by_default', True), rule_module.get_new_command,
getattr(rule_module, 'side_effect', None), getattr(rule_module, 'enabled_by_default', True),
settings.priority.get(name, priority), getattr(rule_module, 'side_effect', None),
getattr(rule_module, 'requires_output', True)) settings.priority.get(name, priority),
getattr(rule_module, 'requires_output', True))
def get_loaded_rules(rules, settings): def get_loaded_rules(rules, settings):
@ -55,7 +57,7 @@ def is_rule_match(command, rule, settings):
def make_corrected_commands(command, rule, settings): def make_corrected_commands(command, rule, settings):
new_commands = rule.get_new_command(command, settings) new_commands = rule.get_new_command(command, settings)
if not isinstance(new_commands, list): if not isinstance(new_commands, list):
new_commands = [new_commands] new_commands = (new_commands,)
for n, new_command in enumerate(new_commands): for n, new_command in enumerate(new_commands):
yield types.CorrectedCommand(script=new_command, yield types.CorrectedCommand(script=new_command,
side_effect=rule.side_effect, side_effect=rule.side_effect,

View File

@ -1,38 +1,38 @@
import os import os
import re import re
from subprocess import check_output from thefuck.utils import get_closest, replace_argument, which
from thefuck.utils import get_closest, replace_argument from thefuck.specific.brew import get_brew_path_prefix
# Formulars are base on each local system's status
brew_formulas = [] enabled_by_default = bool(which('brew'))
try:
brew_path_prefix = check_output(['brew', '--prefix'],
universal_newlines=True).strip()
brew_formula_path = brew_path_prefix + '/Library/Formula'
for file_name in os.listdir(brew_formula_path):
if file_name.endswith('.rb'): def _get_formulas():
brew_formulas.append(file_name.replace('.rb', '')) # Formulas are based on each local system's status
except: try:
pass brew_path_prefix = get_brew_path_prefix()
brew_formula_path = brew_path_prefix + '/Library/Formula'
for file_name in os.listdir(brew_formula_path):
if file_name.endswith('.rb'):
yield file_name[:-3]
except:
pass
def _get_similar_formula(formula_name): def _get_similar_formula(formula_name):
return get_closest(formula_name, brew_formulas, 1, 0.85) return get_closest(formula_name, _get_formulas(), 1, 0.85)
def match(command, settings): def match(command, settings):
is_proper_command = ('brew install' in command.script and is_proper_command = ('brew install' in command.script and
'No available formula' in command.stderr) 'No available formula' in command.stderr)
has_possible_formulas = False
if is_proper_command: if is_proper_command:
formula = re.findall(r'Error: No available formula for ([a-z]+)', formula = re.findall(r'Error: No available formula for ([a-z]+)',
command.stderr)[0] command.stderr)[0]
has_possible_formulas = bool(_get_similar_formula(formula)) return bool(_get_similar_formula(formula))
return False
return has_possible_formulas
def get_new_command(command, settings): def get_new_command(command, settings):

View File

@ -1,30 +1,19 @@
import os import os
import re import re
import subprocess
from thefuck.utils import get_closest, replace_command from thefuck.utils import get_closest, replace_command
from thefuck.specific.brew import get_brew_path_prefix
BREW_CMD_PATH = '/Library/Homebrew/cmd' BREW_CMD_PATH = '/Library/Homebrew/cmd'
TAP_PATH = '/Library/Taps' TAP_PATH = '/Library/Taps'
TAP_CMD_PATH = '/%s/%s/cmd' TAP_CMD_PATH = '/%s/%s/cmd'
def _get_brew_path_prefix():
"""To get brew path"""
try:
return subprocess.check_output(['brew', '--prefix'],
universal_newlines=True).strip()
except:
return None
def _get_brew_commands(brew_path_prefix): def _get_brew_commands(brew_path_prefix):
"""To get brew default commands on local environment""" """To get brew default commands on local environment"""
brew_cmd_path = brew_path_prefix + BREW_CMD_PATH brew_cmd_path = brew_path_prefix + BREW_CMD_PATH
commands = [name.replace('.rb', '') for name in os.listdir(brew_cmd_path) return [name[:-3] for name in os.listdir(brew_cmd_path)
if name.endswith('.rb')] if name.endswith('.rb')]
return commands
def _get_brew_tap_specific_commands(brew_path_prefix): def _get_brew_tap_specific_commands(brew_path_prefix):
@ -51,10 +40,7 @@ def _get_brew_tap_specific_commands(brew_path_prefix):
def _is_brew_tap_cmd_naming(name): def _is_brew_tap_cmd_naming(name):
if name.startswith('brew-') and name.endswith('.rb'): return name.startswith('brew-') and name.endswith('.rb')
return True
return False
def _get_directory_names_only(path): def _get_directory_names_only(path):
@ -62,35 +48,33 @@ def _get_directory_names_only(path):
if os.path.isdir(os.path.join(path, d))] if os.path.isdir(os.path.join(path, d))]
brew_path_prefix = _get_brew_path_prefix() def _brew_commands():
brew_path_prefix = get_brew_path_prefix()
if brew_path_prefix:
try:
return _get_brew_commands(brew_path_prefix) \
+ _get_brew_tap_specific_commands(brew_path_prefix)
except OSError:
pass
# Failback commands for testing (Based on Homebrew 0.9.5) # Failback commands for testing (Based on Homebrew 0.9.5)
brew_commands = ['info', 'home', 'options', 'install', 'uninstall', return ['info', 'home', 'options', 'install', 'uninstall',
'search', 'list', 'update', 'upgrade', 'pin', 'unpin', 'search', 'list', 'update', 'upgrade', 'pin', 'unpin',
'doctor', 'create', 'edit'] 'doctor', 'create', 'edit']
if brew_path_prefix:
try:
brew_commands = _get_brew_commands(brew_path_prefix) \
+ _get_brew_tap_specific_commands(brew_path_prefix)
except OSError:
pass
def match(command, settings): def match(command, settings):
is_proper_command = ('brew' in command.script and is_proper_command = ('brew' in command.script and
'Unknown command' in command.stderr) 'Unknown command' in command.stderr)
has_possible_commands = False
if is_proper_command: if is_proper_command:
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
command.stderr)[0] command.stderr)[0]
has_possible_commands = bool(get_closest(broken_cmd, brew_commands)) return bool(get_closest(broken_cmd, _brew_commands()))
return False
return has_possible_commands
def get_new_command(command, settings): def get_new_command(command, settings):
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
command.stderr)[0] command.stderr)[0]
return replace_command(command, broken_cmd, brew_commands) return replace_command(command, broken_cmd, _brew_commands())

View File

@ -35,17 +35,17 @@ patterns = (
# for the sake of readability do not use named groups above # for the sake of readability do not use named groups above
def _make_pattern(pattern): def _make_pattern(pattern):
pattern = pattern.replace('{file}', '(?P<file>[^:\n]+)') pattern = pattern.replace('{file}', '(?P<file>[^:\n]+)') \
pattern = pattern.replace('{line}', '(?P<line>[0-9]+)') .replace('{line}', '(?P<line>[0-9]+)') \
pattern = pattern.replace('{col}', '(?P<col>[0-9]+)') .replace('{col}', '(?P<col>[0-9]+)')
return re.compile(pattern, re.MULTILINE) return re.compile(pattern, re.MULTILINE)
patterns = [_make_pattern(p) for p in patterns] patterns = [_make_pattern(p).search for p in patterns]
@memoize @memoize
def _search(stderr): def _search(stderr):
for pattern in patterns: for pattern in patterns:
m = re.search(pattern, stderr) m = pattern(stderr)
if m and os.path.isfile(m.group('file')): if m and os.path.isfile(m.group('file')):
return m return m

View File

@ -1,5 +1,5 @@
import re import re
from thefuck import utils, shells from thefuck import shells
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,3 @@
from thefuck import utils
from thefuck.utils import replace_argument from thefuck.utils import replace_argument
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,4 @@
from thefuck import utils, shells from thefuck import shells
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,3 @@
from thefuck import utils
from thefuck.utils import replace_argument from thefuck.utils import replace_argument
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,4 @@
from thefuck import shells, utils from thefuck import shells
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,3 @@
from thefuck import utils
from thefuck.utils import replace_argument from thefuck.utils import replace_argument
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,3 @@
from thefuck import utils
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,4 @@
from thefuck import utils, shells from thefuck import shells
from thefuck.utils import replace_argument from thefuck.utils import replace_argument
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,4 +1,4 @@
from thefuck import shells, utils from thefuck import shells
from thefuck.specific.git import git_support from thefuck.specific.git import git_support

View File

@ -1,27 +1,23 @@
import re import re
from thefuck.utils import for_app from thefuck.utils import for_app
patterns = [ commands = ('ssh', 'scp')
r'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!',
r'WARNING: POSSIBLE DNS SPOOFING DETECTED!',
r"Warning: the \S+ host key for '([^']+)' differs from the key for the IP address '([^']+)'",
]
offending_pattern = re.compile(
r'(?:Offending (?:key for IP|\S+ key)|Matching host key) in ([^:]+):(\d+)',
re.MULTILINE)
commands = ['ssh', 'scp']
@for_app(*commands) @for_app(*commands)
def match(command, settings): def match(command, settings):
if not command.script: if not command.script:
return False return False
if not command.script.split()[0] in commands: if not command.script.startswith(commands):
return False return False
if not any([re.findall(pattern, command.stderr) for pattern in patterns]):
return False patterns = (
return True r'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!',
r'WARNING: POSSIBLE DNS SPOOFING DETECTED!',
r"Warning: the \S+ host key for '([^']+)' differs from the key for the IP address '([^']+)'",
)
return any(re.findall(pattern, command.stderr) for pattern in patterns)
def get_new_command(command, settings): def get_new_command(command, settings):
@ -29,6 +25,9 @@ def get_new_command(command, settings):
def side_effect(old_cmd, command, settings): def side_effect(old_cmd, command, settings):
offending_pattern = re.compile(
r'(?:Offending (?:key for IP|\S+ key)|Matching host key) in ([^:]+):(\d+)',
re.MULTILINE)
offending = offending_pattern.findall(old_cmd.stderr) offending = offending_pattern.findall(old_cmd.stderr)
for filepath, lineno in offending: for filepath, lineno in offending:
with open(filepath, 'r') as fh: with open(filepath, 'r') as fh:

View File

@ -1,5 +1,5 @@
import re import re
from thefuck.utils import (replace_command, get_all_matched_commands) from thefuck.utils import replace_command
def match(command, settings): def match(command, settings):
return (re.search(r"([^:]*): Unknown command.*", command.stderr) != None return (re.search(r"([^:]*): Unknown command.*", command.stderr) != None

View File

@ -86,8 +86,7 @@ class Bash(Generic):
@memoize @memoize
def get_aliases(self): def get_aliases(self):
proc = Popen('bash -ic alias', stdout=PIPE, stderr=DEVNULL, proc = Popen(['bash', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
shell=True)
return dict( return dict(
self._parse_alias(alias) self._parse_alias(alias)
for alias in proc.stdout.read().decode('utf-8').split('\n') for alias in proc.stdout.read().decode('utf-8').split('\n')
@ -131,8 +130,7 @@ class Fish(Generic):
@memoize @memoize
def get_aliases(self): def get_aliases(self):
overridden = self._get_overridden_aliases() overridden = self._get_overridden_aliases()
proc = Popen('fish -ic functions', stdout=PIPE, stderr=DEVNULL, proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL)
shell=True)
functions = proc.stdout.read().decode('utf-8').strip().split('\n') functions = proc.stdout.read().decode('utf-8').strip().split('\n')
return {func: func for func in functions if func not in overridden} return {func: func for func in functions if func not in overridden}
@ -172,8 +170,7 @@ class Zsh(Generic):
@memoize @memoize
def get_aliases(self): def get_aliases(self):
proc = Popen('zsh -ic alias', stdout=PIPE, stderr=DEVNULL, proc = Popen(['zsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
shell=True)
return dict( return dict(
self._parse_alias(alias) self._parse_alias(alias)
for alias in proc.stdout.read().decode('utf-8').split('\n') for alias in proc.stdout.read().decode('utf-8').split('\n')
@ -205,8 +202,7 @@ class Tcsh(Generic):
@memoize @memoize
def get_aliases(self): def get_aliases(self):
proc = Popen('tcsh -ic alias', stdout=PIPE, stderr=DEVNULL, proc = Popen(['tcsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
shell=True)
return dict( return dict(
self._parse_alias(alias) self._parse_alias(alias)
for alias in proc.stdout.read().decode('utf-8').split('\n') for alias in proc.stdout.read().decode('utf-8').split('\n')

15
thefuck/specific/brew.py Normal file
View File

@ -0,0 +1,15 @@
import subprocess
from thefuck.utils import memoize, which
enabled_by_default = bool(which('brew'))
@memoize
def get_brew_path_prefix():
"""To get brew path"""
try:
return subprocess.check_output(['brew', '--prefix'],
universal_newlines=True).strip()
except:
return None

View File

@ -18,6 +18,23 @@ else:
from shlex import quote from shlex import quote
def memoize(fn):
"""Caches previous calls to the function."""
memo = {}
@wraps(fn)
def wrapper(*args, **kwargs):
key = pickle.dumps((args, kwargs))
if key not in memo or memoize.disabled:
memo[key] = fn(*args, **kwargs)
return memo[key]
return wrapper
memoize.disabled = False
@memoize
def which(program): def which(program):
"""Returns `program` path or `None`.""" """Returns `program` path or `None`."""
@ -53,22 +70,6 @@ def wrap_settings(params):
return decorator(_wrap_settings) return decorator(_wrap_settings)
def memoize(fn):
"""Caches previous calls to the function."""
memo = {}
@wraps(fn)
def wrapper(*args, **kwargs):
key = pickle.dumps((args, kwargs))
if key not in memo or memoize.disabled:
memo[key] = fn(*args, **kwargs)
return memo[key]
return wrapper
memoize.disabled = False
def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True): def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True):
"""Returns closest match or just first from possibilities.""" """Returns closest match or just first from possibilities."""
possibilities = list(possibilities) possibilities = list(possibilities)