From 2e002f666bd6529b97d9a7a53a69359a34534bbc Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 25 Aug 2015 13:55:33 +0300 Subject: [PATCH] Move utility functions from `archlinux` to `utils` --- tests/rules/test_pacman.py | 4 +-- tests/rules/test_pacman_not_found.py | 4 +-- thefuck/archlinux.py | 41 --------------------------- thefuck/rules/pacman.py | 2 +- thefuck/rules/pacman_not_found.py | 3 +- thefuck/utils.py | 42 +++++++++++++++++++++++++++- 6 files changed, 47 insertions(+), 49 deletions(-) delete mode 100644 thefuck/archlinux.py diff --git a/tests/rules/test_pacman.py b/tests/rules/test_pacman.py index 0006e8b..4f42d67 100644 --- a/tests/rules/test_pacman.py +++ b/tests/rules/test_pacman.py @@ -29,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.archlinux.subprocess') +@patch('thefuck.utils.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 @@ -75,7 +75,7 @@ def test_get_new_command(command, new_command, mocker): (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.archlinux.subprocess') +@patch('thefuck.utils.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/tests/rules/test_pacman_not_found.py b/tests/rules/test_pacman_not_found.py index d772b23..d659bd1 100644 --- a/tests/rules/test_pacman_not_found.py +++ b/tests/rules/test_pacman_not_found.py @@ -22,7 +22,7 @@ def test_match(command): Command(script='yaourt -S llc', stderr='error: target not found: llc'), Command(script='pacman llc', stderr='error: target not found: llc'), Command(script='sudo pacman llc', stderr='error: target not found: llc')]) -@patch('thefuck.archlinux.subprocess') +@patch('thefuck.utils.subprocess') def test_match_mocked(subp_mock, command): subp_mock.check_output.return_value = PKGFILE_OUTPUT_LLC assert match(command, None) @@ -42,7 +42,7 @@ def test_get_new_command(command, fixed): (Command(script='yaourt -S llc', stderr='error: target not found: llc'), ['yaourt -S extra/llvm', 'yaourt -S extra/llvm35']), (Command(script='pacman -S llc', stderr='error: target not found: llc'), ['pacman -S extra/llvm', 'pacman -S extra/llvm35']), (Command(script='sudo pacman -S llc', stderr='error: target not found: llc'), ['sudo pacman -S extra/llvm', 'sudo pacman -S extra/llvm35'])]) -@patch('thefuck.archlinux.subprocess') +@patch('thefuck.utils.subprocess') def test_get_new_command_mocked(subp_mock, command, fixed): subp_mock.check_output.return_value = PKGFILE_OUTPUT_LLC assert get_new_command(command, None) == fixed diff --git a/thefuck/archlinux.py b/thefuck/archlinux.py deleted file mode 100644 index 04b9714..0000000 --- a/thefuck/archlinux.py +++ /dev/null @@ -1,41 +0,0 @@ -""" 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.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 04201cd..65a50ef 100644 --- a/thefuck/rules/pacman.py +++ b/thefuck/rules/pacman.py @@ -1,4 +1,4 @@ -from thefuck.archlinux import archlinux_env, get_pkgfile +from thefuck.utils import get_pkgfile, archlinux_env from thefuck import shells diff --git a/thefuck/rules/pacman_not_found.py b/thefuck/rules/pacman_not_found.py index 4ea1b64..d71b74d 100644 --- a/thefuck/rules/pacman_not_found.py +++ b/thefuck/rules/pacman_not_found.py @@ -6,8 +6,7 @@ should be: yaourt -S llvm """ -from thefuck.utils import replace_command -from thefuck.archlinux import archlinux_env, get_pkgfile +from thefuck.utils import replace_command, get_pkgfile, archlinux_env def match(command, settings): diff --git a/thefuck/utils.py b/thefuck/utils.py index 0003e8b..9f29590 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -1,4 +1,5 @@ -from .types import Command +from subprocess import CalledProcessError +import subprocess from difflib import get_close_matches from functools import wraps from pathlib import Path @@ -7,6 +8,7 @@ import os import pickle import re import six +from .types import Command DEVNULL = open(os.devnull, 'w') @@ -185,3 +187,41 @@ def replace_command(command, broken, matched): new_cmds = get_close_matches(broken, matched, cutoff=0.1) return [replace_argument(command.script, broken, new_cmd.strip()) for new_cmd in new_cmds] + + +@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.strip() + + 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 CalledProcessError: + return None + + +def archlinux_env(): + if which('yaourt'): + pacman = 'yaourt' + elif which('pacman'): + pacman = 'sudo pacman' + else: + return False, None + + enabled_by_default = which('pkgfile') + + return enabled_by_default, pacman \ No newline at end of file