From 986bbb30a7cfcf6316196c56076a523e30ea9696 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 12 Aug 2015 17:13:32 +0200 Subject: [PATCH] Create thefuck.archlinux --- tests/rules/test_pacman.py | 6 ++++-- thefuck/archlinux.py | 41 ++++++++++++++++++++++++++++++++++++++ thefuck/rules/pacman.py | 38 ++++------------------------------- thefuck/utils.py | 2 +- 4 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 thefuck/archlinux.py diff --git a/tests/rules/test_pacman.py b/tests/rules/test_pacman.py index 73413589..0006e8b6 100644 --- a/tests/rules/test_pacman.py +++ b/tests/rules/test_pacman.py @@ -7,6 +7,7 @@ from tests.utils import Command pacman_cmd = getattr(pacman, 'pacman', 'pacman') +PKGFILE_OUTPUT_SUDO = 'core/sudo 1.8.13-13/usr/bin/sudo' PKGFILE_OUTPUT_CONVERT = 'extra/imagemagick 6.9.1.0-1\t/usr/bin/convert' PKGFILE_OUTPUT_VIM = '''extra/gvim 7.4.712-1 \t/usr/bin/vim @@ -28,7 +29,7 @@ def test_match(command): @pytest.mark.parametrize('command, return_value', [ (Command(script='vim', stderr='vim: command not found'), PKGFILE_OUTPUT_VIM), (Command(script='sudo vim', stderr='sudo: vim: command not found'), PKGFILE_OUTPUT_VIM)]) -@patch('thefuck.rules.pacman.subprocess') +@patch('thefuck.archlinux.subprocess') @patch.multiple(pacman, create=True, pacman=pacman_cmd) def test_match_mocked(subp_mock, command, return_value): subp_mock.check_output.return_value = return_value @@ -72,8 +73,9 @@ def test_get_new_command(command, new_command, mocker): (Command('vim'), vim_possibilities, PKGFILE_OUTPUT_VIM), (Command('sudo vim'), sudo_vim_possibilities, PKGFILE_OUTPUT_VIM), (Command('convert'), ['{} -S extra/imagemagick && convert'.format(pacman_cmd)], PKGFILE_OUTPUT_CONVERT), + (Command('sudo'), ['{} -S core/sudo && sudo'.format(pacman_cmd)], PKGFILE_OUTPUT_SUDO), (Command('sudo convert'), ['{} -S extra/imagemagick && sudo convert'.format(pacman_cmd)], PKGFILE_OUTPUT_CONVERT)]) -@patch('thefuck.rules.pacman.subprocess') +@patch('thefuck.archlinux.subprocess') @patch.multiple(pacman, create=True, pacman=pacman_cmd) def test_get_new_command_mocked(subp_mock, command, new_command, return_value): subp_mock.check_output.return_value = return_value diff --git a/thefuck/archlinux.py b/thefuck/archlinux.py new file mode 100644 index 00000000..a60f063c --- /dev/null +++ b/thefuck/archlinux.py @@ -0,0 +1,41 @@ +""" This file provide some utility functions for Arch Linux specific rules.""" +import thefuck.utils +import subprocess + + +@thefuck.utils.memoize +def get_pkgfile(command): + """ Gets the packages that provide the given command using `pkgfile`. + + If the command is of the form `sudo foo`, searches for the `foo` command + instead. + """ + try: + command = command.script.strip() + + if command.startswith('sudo '): + command = command[5:] + + command = command.split(" ")[0] + + packages = subprocess.check_output( + ['pkgfile', '-b', '-v', command], + universal_newlines=True, stderr=thefuck.utils.DEVNULL + ).splitlines() + + return [package.split()[0] for package in packages] + except subprocess.CalledProcessError: + return None + + +def archlinux_env(): + if thefuck.utils.which('yaourt'): + pacman = 'yaourt' + elif thefuck.utils.which('pacman'): + pacman = 'sudo pacman' + else: + return False, None + + enabled_by_default = thefuck.utils.which('pkgfile') + + return enabled_by_default, pacman diff --git a/thefuck/rules/pacman.py b/thefuck/rules/pacman.py index 2bf64b30..9b0a8873 100644 --- a/thefuck/rules/pacman.py +++ b/thefuck/rules/pacman.py @@ -1,46 +1,16 @@ -import subprocess -from thefuck.utils import DEVNULL, which +from thefuck.archlinux import archlinux_env, get_pkgfile from thefuck import shells -from thefuck.utils import memoize - - -@memoize -def __get_pkgfile(command): - try: - command = command.script - - if command.startswith('sudo'): - command = command[5:] - - command = command.split(" ")[0] - - packages = subprocess.check_output( - ['pkgfile', '-b', '-v', command], - universal_newlines=True, stderr=DEVNULL - ).splitlines() - - return [package.split()[0] for package in packages] - except subprocess.CalledProcessError: - return None def match(command, settings): - return 'not found' in command.stderr and __get_pkgfile(command) + return 'not found' in command.stderr and get_pkgfile(command) def get_new_command(command, settings): - packages = __get_pkgfile(command) + packages = get_pkgfile(command) formatme = shells.and_('{} -S {}', '{}') return [formatme.format(pacman, package, command.script) for package in packages] - -if not which('pkgfile'): - enabled_by_default = False -elif which('yaourt'): - pacman = 'yaourt' -elif which('pacman'): - pacman = 'sudo pacman' -else: - enabled_by_default = False +enabled_by_default, pacman = archlinux_env() diff --git a/thefuck/utils.py b/thefuck/utils.py index dde7c906..0003e8ba 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -1,3 +1,4 @@ +from .types import Command from difflib import get_close_matches from functools import wraps from pathlib import Path @@ -6,7 +7,6 @@ import os import pickle import re import six -from .types import Command DEVNULL = open(os.devnull, 'w')