From 2b750bac8b2cffa17963832f13836e02be46a215 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 19:14:03 +0200 Subject: [PATCH 1/8] Log rule import times --- thefuck/corrector.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/thefuck/corrector.py b/thefuck/corrector.py index 4171358..b7647e4 100644 --- a/thefuck/corrector.py +++ b/thefuck/corrector.py @@ -1,21 +1,23 @@ -import sys +from . import conf, logs +from .utils import eager from imp import load_source from pathlib import Path -from . import conf, types, logs -from .utils import eager +from thefuck.types import CorrectedCommand, Rule +import sys def load_rule(rule, settings): """Imports rule module and returns it.""" name = rule.name[:-3] - rule_module = load_source(name, str(rule)) - priority = getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY) - return types.Rule(name, rule_module.match, - rule_module.get_new_command, - getattr(rule_module, 'enabled_by_default', True), - getattr(rule_module, 'side_effect', None), - settings.priority.get(name, priority), - getattr(rule_module, 'requires_output', True)) + with logs.debug_time(u'Importing rule: {};'.format(name), settings): + rule_module = load_source(name, str(rule)) + priority = getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY) + return Rule(name, rule_module.match, + rule_module.get_new_command, + getattr(rule_module, 'enabled_by_default', True), + getattr(rule_module, 'side_effect', None), + settings.priority.get(name, priority), + getattr(rule_module, 'requires_output', True)) def get_loaded_rules(rules, settings): @@ -59,11 +61,11 @@ def make_corrected_commands(command, rules, settings): for rule in rules: new_commands = rule.get_new_command(command, settings) if not isinstance(new_commands, list): - new_commands = [new_commands] + new_commands = (new_commands,) for n, new_command in enumerate(new_commands): - yield types.CorrectedCommand(script=new_command, - side_effect=rule.side_effect, - priority=(n + 1) * rule.priority) + yield CorrectedCommand(script=new_command, + side_effect=rule.side_effect, + priority=(n + 1) * rule.priority) def remove_duplicates(corrected_commands): From b0702d309fdc6547ad41682f8dbb629cc13a7505 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 19:46:23 +0200 Subject: [PATCH 2/8] Improve brew* rules import time --- tests/rules/test_brew_install.py | 6 +-- tests/rules/test_brew_unknown_command.py | 4 +- thefuck/rules/brew_install.py | 36 ++++++++-------- thefuck/rules/brew_unknown_command.py | 54 +++++++++--------------- thefuck/specific/brew.py | 15 +++++++ 5 files changed, 56 insertions(+), 59 deletions(-) create mode 100644 thefuck/specific/brew.py diff --git a/tests/rules/test_brew_install.py b/tests/rules/test_brew_install.py index 2ea58f1..e8eae86 100644 --- a/tests/rules/test_brew_install.py +++ b/tests/rules/test_brew_install.py @@ -1,6 +1,6 @@ import pytest 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 @@ -20,9 +20,7 @@ def brew_already_installed(): def _is_not_okay_to_test(): - if 'elasticsearch' not in brew_formulas: - return True - return False + return 'elasticsearch' not in _get_formulas() @pytest.mark.skipif(_is_not_okay_to_test(), diff --git a/tests/rules/test_brew_unknown_command.py b/tests/rules/test_brew_unknown_command.py index b33f740..17adb47 100644 --- a/tests/rules/test_brew_unknown_command.py +++ b/tests/rules/test_brew_unknown_command.py @@ -1,6 +1,6 @@ import pytest 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 @@ -16,7 +16,7 @@ def brew_unknown_cmd2(): def test_match(brew_unknown_cmd): 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) diff --git a/thefuck/rules/brew_install.py b/thefuck/rules/brew_install.py index 751c1f7..a1432b0 100644 --- a/thefuck/rules/brew_install.py +++ b/thefuck/rules/brew_install.py @@ -1,38 +1,38 @@ import os import re -from subprocess import check_output -from thefuck.utils import get_closest, replace_argument +from thefuck.utils import get_closest, replace_argument, which +from thefuck.specific.brew import get_brew_path_prefix -# Formulars are base on each local system's status -brew_formulas = [] -try: - brew_path_prefix = check_output(['brew', '--prefix'], - universal_newlines=True).strip() - brew_formula_path = brew_path_prefix + '/Library/Formula' +enabled_by_default = bool(which('brew')) - for file_name in os.listdir(brew_formula_path): - if file_name.endswith('.rb'): - brew_formulas.append(file_name.replace('.rb', '')) -except: - pass + +def _get_formulas(): + # Formulas are based on each local system's status + try: + 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): - return get_closest(formula_name, brew_formulas, 1, 0.85) + return get_closest(formula_name, _get_formulas(), 1, 0.85) def match(command, settings): is_proper_command = ('brew install' in command.script and 'No available formula' in command.stderr) - has_possible_formulas = False if is_proper_command: formula = re.findall(r'Error: No available formula for ([a-z]+)', command.stderr)[0] - has_possible_formulas = bool(_get_similar_formula(formula)) - - return has_possible_formulas + return bool(_get_similar_formula(formula)) + return False def get_new_command(command, settings): diff --git a/thefuck/rules/brew_unknown_command.py b/thefuck/rules/brew_unknown_command.py index 9b80f88..d798a7b 100644 --- a/thefuck/rules/brew_unknown_command.py +++ b/thefuck/rules/brew_unknown_command.py @@ -1,30 +1,19 @@ import os import re -import subprocess from thefuck.utils import get_closest, replace_command +from thefuck.specific.brew import get_brew_path_prefix BREW_CMD_PATH = '/Library/Homebrew/cmd' TAP_PATH = '/Library/Taps' 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): """To get brew default commands on local environment""" brew_cmd_path = brew_path_prefix + BREW_CMD_PATH - commands = [name.replace('.rb', '') for name in os.listdir(brew_cmd_path) - if name.endswith('.rb')] - - return commands + return [name[:-3] for name in os.listdir(brew_cmd_path) + if name.endswith('.rb')] 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): - if name.startswith('brew-') and name.endswith('.rb'): - return True - - return False + return name.startswith('brew-') and name.endswith('.rb') 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))] -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) -brew_commands = ['info', 'home', 'options', 'install', 'uninstall', - 'search', 'list', 'update', 'upgrade', 'pin', 'unpin', - '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 + # Failback commands for testing (Based on Homebrew 0.9.5) + return ['info', 'home', 'options', 'install', 'uninstall', + 'search', 'list', 'update', 'upgrade', 'pin', 'unpin', + 'doctor', 'create', 'edit'] def match(command, settings): is_proper_command = ('brew' in command.script and 'Unknown command' in command.stderr) - has_possible_commands = False if is_proper_command: broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', command.stderr)[0] - has_possible_commands = bool(get_closest(broken_cmd, brew_commands)) - - return has_possible_commands + return bool(get_closest(broken_cmd, _brew_commands())) + return False def get_new_command(command, settings): broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', command.stderr)[0] - return replace_command(command, broken_cmd, brew_commands) + return replace_command(command, broken_cmd, _brew_commands()) diff --git a/thefuck/specific/brew.py b/thefuck/specific/brew.py new file mode 100644 index 0000000..66d0159 --- /dev/null +++ b/thefuck/specific/brew.py @@ -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 From 51f1f44162f1e09764d70ca15f2c28b2d7323c81 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 19:55:40 +0200 Subject: [PATCH 3/8] Memoize `thefuck.utils.which` It is used by some rules to determine if they should be enabled by default and searches in the $PATH, which can be quiet slow. --- thefuck/utils.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/thefuck/utils.py b/thefuck/utils.py index 8dd777b..0c3266b 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -17,6 +17,23 @@ else: 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): """Returns `program` path or `None`.""" @@ -55,22 +72,6 @@ def wrap_settings(params): return decorator -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): """Returns closest match or just first from possibilities.""" possibilities = list(possibilities) @@ -94,7 +95,7 @@ def get_all_executables(): tf_alias = thefuck_alias() return [exe.name for path in os.environ.get('PATH', '').split(':') - for exe in _safe(lambda: list(Path(path).iterdir()), []) + for exe in _safe(Path(path).iterdir, []) if not _safe(exe.is_dir, True)] + [ alias for alias in get_aliases() if alias != tf_alias] From 9fc2bc904c530ae6f26b908bd2238aaad4064abb Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 19:57:34 +0200 Subject: [PATCH 4/8] Slightly improve the `fix_file` rule --- thefuck/rules/fix_file.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/thefuck/rules/fix_file.py b/thefuck/rules/fix_file.py index caa59f0..7af1fb3 100644 --- a/thefuck/rules/fix_file.py +++ b/thefuck/rules/fix_file.py @@ -35,17 +35,17 @@ patterns = ( # for the sake of readability do not use named groups above def _make_pattern(pattern): - pattern = pattern.replace('{file}', '(?P[^:\n]+)') - pattern = pattern.replace('{line}', '(?P[0-9]+)') - pattern = pattern.replace('{col}', '(?P[0-9]+)') + pattern = pattern.replace('{file}', '(?P[^:\n]+)') \ + .replace('{line}', '(?P[0-9]+)') \ + .replace('{col}', '(?P[0-9]+)') return re.compile(pattern, re.MULTILINE) -patterns = [_make_pattern(p) for p in patterns] +patterns = [_make_pattern(p).search for p in patterns] @memoize def _search(stderr): for pattern in patterns: - m = re.search(pattern, stderr) + m = pattern(stderr) if m and os.path.isfile(m.group('file')): return m From e5ce000399b6760fc885471ba404d97075a18bb8 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 20:03:33 +0200 Subject: [PATCH 5/8] Improve the `ssh_known_hosts` rule import time --- thefuck/rules/ssh_known_hosts.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/thefuck/rules/ssh_known_hosts.py b/thefuck/rules/ssh_known_hosts.py index df908d0..6d666fc 100644 --- a/thefuck/rules/ssh_known_hosts.py +++ b/thefuck/rules/ssh_known_hosts.py @@ -1,25 +1,19 @@ import re -patterns = [ - 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'] - def match(command, settings): if not command.script: return False - if not command.script.split()[0] in commands: + if not command.script.startswith(('ssh', 'scp')): return False - if not any([re.findall(pattern, command.stderr) for pattern in patterns]): - return False - return True + + patterns = ( + 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): @@ -27,6 +21,9 @@ def get_new_command(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) for filepath, lineno in offending: with open(filepath, 'r') as fh: From 3bd2c8d4c84f90f676990a9f562b78388ee3ae87 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 20:55:47 +0200 Subject: [PATCH 6/8] Remove unused imports --- thefuck/rules/git_add.py | 2 +- thefuck/rules/git_branch_delete.py | 1 - thefuck/rules/git_branch_list.py | 2 +- thefuck/rules/git_diff_staged.py | 1 - thefuck/rules/git_pull.py | 2 +- thefuck/rules/git_pull_clone.py | 1 - thefuck/rules/git_push.py | 1 - thefuck/rules/git_push_force.py | 1 - thefuck/rules/git_push_pull.py | 2 +- thefuck/rules/git_stash.py | 2 +- thefuck/rules/unknown_command.py | 2 +- 11 files changed, 6 insertions(+), 11 deletions(-) diff --git a/thefuck/rules/git_add.py b/thefuck/rules/git_add.py index f90f283..617491f 100644 --- a/thefuck/rules/git_add.py +++ b/thefuck/rules/git_add.py @@ -1,5 +1,5 @@ import re -from thefuck import utils, shells +from thefuck import shells from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_branch_delete.py b/thefuck/rules/git_branch_delete.py index 14a9685..d334c08 100644 --- a/thefuck/rules/git_branch_delete.py +++ b/thefuck/rules/git_branch_delete.py @@ -1,4 +1,3 @@ -from thefuck import utils from thefuck.utils import replace_argument from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_branch_list.py b/thefuck/rules/git_branch_list.py index 07fb81e..25e4aaa 100644 --- a/thefuck/rules/git_branch_list.py +++ b/thefuck/rules/git_branch_list.py @@ -1,4 +1,4 @@ -from thefuck import utils, shells +from thefuck import shells from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_diff_staged.py b/thefuck/rules/git_diff_staged.py index d755a5c..bdb3ba6 100644 --- a/thefuck/rules/git_diff_staged.py +++ b/thefuck/rules/git_diff_staged.py @@ -1,4 +1,3 @@ -from thefuck import utils from thefuck.utils import replace_argument from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_pull.py b/thefuck/rules/git_pull.py index 9c2d828..426e342 100644 --- a/thefuck/rules/git_pull.py +++ b/thefuck/rules/git_pull.py @@ -1,4 +1,4 @@ -from thefuck import shells, utils +from thefuck import shells from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_pull_clone.py b/thefuck/rules/git_pull_clone.py index a5c27f1..a2d1cfb 100644 --- a/thefuck/rules/git_pull_clone.py +++ b/thefuck/rules/git_pull_clone.py @@ -1,4 +1,3 @@ -from thefuck import utils from thefuck.utils import replace_argument from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_push.py b/thefuck/rules/git_push.py index adea017..badce6d 100644 --- a/thefuck/rules/git_push.py +++ b/thefuck/rules/git_push.py @@ -1,4 +1,3 @@ -from thefuck import utils from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_push_force.py b/thefuck/rules/git_push_force.py index 52ecfe3..45a3085 100644 --- a/thefuck/rules/git_push_force.py +++ b/thefuck/rules/git_push_force.py @@ -1,4 +1,3 @@ -from thefuck import utils from thefuck.utils import replace_argument from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_push_pull.py b/thefuck/rules/git_push_pull.py index 85ec7cb..897406b 100644 --- a/thefuck/rules/git_push_pull.py +++ b/thefuck/rules/git_push_pull.py @@ -1,4 +1,4 @@ -from thefuck import utils, shells +from thefuck import shells from thefuck.utils import replace_argument from thefuck.specific.git import git_support diff --git a/thefuck/rules/git_stash.py b/thefuck/rules/git_stash.py index ee92a04..c0bad41 100644 --- a/thefuck/rules/git_stash.py +++ b/thefuck/rules/git_stash.py @@ -1,4 +1,4 @@ -from thefuck import shells, utils +from thefuck import shells from thefuck.specific.git import git_support diff --git a/thefuck/rules/unknown_command.py b/thefuck/rules/unknown_command.py index d863225..3cd9a9a 100644 --- a/thefuck/rules/unknown_command.py +++ b/thefuck/rules/unknown_command.py @@ -1,5 +1,5 @@ import re -from thefuck.utils import (replace_command, get_all_matched_commands) +from thefuck.utils import replace_command def match(command, settings): return (re.search(r"([^:]*): Unknown command.*", command.stderr) != None From 514bb7df81e9206408616abc8871ecd234502f7f Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 23:38:33 +0200 Subject: [PATCH 7/8] Don't run a shell just to run another shell --- thefuck/shells.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/thefuck/shells.py b/thefuck/shells.py index cce2e4a..505dbed 100644 --- a/thefuck/shells.py +++ b/thefuck/shells.py @@ -86,8 +86,7 @@ class Bash(Generic): @memoize def get_aliases(self): - proc = Popen('bash -ic alias', stdout=PIPE, stderr=DEVNULL, - shell=True) + proc = Popen(['bash', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL) return dict( self._parse_alias(alias) for alias in proc.stdout.read().decode('utf-8').split('\n') @@ -131,8 +130,7 @@ class Fish(Generic): @memoize def get_aliases(self): overridden = self._get_overridden_aliases() - proc = Popen('fish -ic functions', stdout=PIPE, stderr=DEVNULL, - shell=True) + proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL) functions = proc.stdout.read().decode('utf-8').strip().split('\n') return {func: func for func in functions if func not in overridden} @@ -172,8 +170,7 @@ class Zsh(Generic): @memoize def get_aliases(self): - proc = Popen('zsh -ic alias', stdout=PIPE, stderr=DEVNULL, - shell=True) + proc = Popen(['zsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL) return dict( self._parse_alias(alias) for alias in proc.stdout.read().decode('utf-8').split('\n') @@ -205,8 +202,7 @@ class Tcsh(Generic): @memoize def get_aliases(self): - proc = Popen('tcsh -ic alias', stdout=PIPE, stderr=DEVNULL, - shell=True) + proc = Popen(['tcsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL) return dict( self._parse_alias(alias) for alias in proc.stdout.read().decode('utf-8').split('\n') From 27c14a44afe72ccd8dd4e18d5ab0e78d53c8c44e Mon Sep 17 00:00:00 2001 From: mcarton Date: Thu, 27 Aug 2015 10:12:31 +0200 Subject: [PATCH 8/8] Fix tests Thanks to [scorphus] for his [help]. [scorphus]: https://github.com/scorphus [help]: https://github.com/nvbn/thefuck/pull/352#issuecomment-135248982 --- tests/rules/test_brew_unknown_command.py | 5 +++-- thefuck/utils.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/rules/test_brew_unknown_command.py b/tests/rules/test_brew_unknown_command.py index 17adb47..42e6c7f 100644 --- a/tests/rules/test_brew_unknown_command.py +++ b/tests/rules/test_brew_unknown_command.py @@ -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), None) == ['brew list', 'brew install', 'brew uninstall'] - assert get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2), - None) == ['brew install', 'brew uninstall', 'brew list'] + cmds = get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2), None) + assert 'brew install' in cmds + assert 'brew uninstall' in cmds diff --git a/thefuck/utils.py b/thefuck/utils.py index 0c3266b..e874efc 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -95,7 +95,7 @@ def get_all_executables(): tf_alias = thefuck_alias() return [exe.name for path in os.environ.get('PATH', '').split(':') - for exe in _safe(Path(path).iterdir, []) + for exe in _safe(lambda: list(Path(path).iterdir()), []) if not _safe(exe.is_dir, True)] + [ alias for alias in get_aliases() if alias != tf_alias]