1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-12 07:59:01 +00:00

Improve brew* rules import time

This commit is contained in:
mcarton 2015-08-26 19:46:23 +02:00
parent 2b750bac8b
commit b0702d309f
5 changed files with 56 additions and 59 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)

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'))
def _get_formulas():
# Formulas are based on each local system's status
try: try:
brew_path_prefix = check_output(['brew', '--prefix'], brew_path_prefix = get_brew_path_prefix()
universal_newlines=True).strip()
brew_formula_path = brew_path_prefix + '/Library/Formula' brew_formula_path = brew_path_prefix + '/Library/Formula'
for file_name in os.listdir(brew_formula_path): for file_name in os.listdir(brew_formula_path):
if file_name.endswith('.rb'): if file_name.endswith('.rb'):
brew_formulas.append(file_name.replace('.rb', '')) yield file_name[:-3]
except: except:
pass 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,31 +1,20 @@
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):
"""To get tap's specific commands """To get tap's specific commands
@ -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()
# 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: if brew_path_prefix:
try: try:
brew_commands = _get_brew_commands(brew_path_prefix) \ return _get_brew_commands(brew_path_prefix) \
+ _get_brew_tap_specific_commands(brew_path_prefix) + _get_brew_tap_specific_commands(brew_path_prefix)
except OSError: except OSError:
pass 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): 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())

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