1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-19 04:21:14 +00:00

Create thefuck.archlinux

This commit is contained in:
mcarton 2015-08-12 17:13:32 +02:00
parent 0ad70a1edc
commit 986bbb30a7
4 changed files with 50 additions and 37 deletions

View File

@ -7,6 +7,7 @@ from tests.utils import Command
pacman_cmd = getattr(pacman, 'pacman', 'pacman') 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_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 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', [ @pytest.mark.parametrize('command, return_value', [
(Command(script='vim', stderr='vim: command not found'), PKGFILE_OUTPUT_VIM), (Command(script='vim', stderr='vim: command not found'), PKGFILE_OUTPUT_VIM),
(Command(script='sudo vim', stderr='sudo: 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) @patch.multiple(pacman, create=True, pacman=pacman_cmd)
def test_match_mocked(subp_mock, command, return_value): def test_match_mocked(subp_mock, command, return_value):
subp_mock.check_output.return_value = 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('vim'), vim_possibilities, PKGFILE_OUTPUT_VIM),
(Command('sudo vim'), sudo_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('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)]) (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) @patch.multiple(pacman, create=True, pacman=pacman_cmd)
def test_get_new_command_mocked(subp_mock, command, new_command, return_value): def test_get_new_command_mocked(subp_mock, command, new_command, return_value):
subp_mock.check_output.return_value = return_value subp_mock.check_output.return_value = return_value

41
thefuck/archlinux.py Normal file
View File

@ -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

View File

@ -1,46 +1,16 @@
import subprocess from thefuck.archlinux import archlinux_env, get_pkgfile
from thefuck.utils import DEVNULL, which
from thefuck import shells 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): 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): def get_new_command(command, settings):
packages = __get_pkgfile(command) packages = get_pkgfile(command)
formatme = shells.and_('{} -S {}', '{}') formatme = shells.and_('{} -S {}', '{}')
return [formatme.format(pacman, package, command.script) return [formatme.format(pacman, package, command.script)
for package in packages] for package in packages]
enabled_by_default, pacman = archlinux_env()
if not which('pkgfile'):
enabled_by_default = False
elif which('yaourt'):
pacman = 'yaourt'
elif which('pacman'):
pacman = 'sudo pacman'
else:
enabled_by_default = False

View File

@ -1,3 +1,4 @@
from .types import Command
from difflib import get_close_matches from difflib import get_close_matches
from functools import wraps from functools import wraps
from pathlib import Path from pathlib import Path
@ -6,7 +7,6 @@ import os
import pickle import pickle
import re import re
import six import six
from .types import Command
DEVNULL = open(os.devnull, 'w') DEVNULL = open(os.devnull, 'w')