mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-01 15:42:06 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af40ad84d8 | ||
|
|
63e62fcba3 | ||
|
|
368be788d7 | ||
|
|
cd1468489f | ||
|
|
fbce86b92a | ||
|
|
3f6652df66 | ||
|
|
cf82af8978 | ||
|
|
20f51f5ffe | ||
|
|
8f6d8b1dd1 | ||
|
|
c0002fe6e0 | ||
|
|
6609b8d06a | ||
|
|
5b5df9361d | ||
|
|
fa234fde70 | ||
|
|
867aec83c3 | ||
|
|
2117659c40 | ||
|
|
4985f75d74 | ||
|
|
959d20df78 | ||
|
|
8529461742 | ||
|
|
3173ef10c6 | ||
|
|
1c5fef3a34 | ||
|
|
386e6bf0c3 | ||
|
|
1146ab654c | ||
|
|
4e7eceaa3a | ||
|
|
71bb1994c3 | ||
|
|
bfa3c905a3 |
16
README.md
16
README.md
@@ -139,6 +139,8 @@ using the matched rule and runs it. Rules enabled by default are as follows:
|
||||
* `composer_not_command` – fixes composer command name;
|
||||
* `cp_omitting_directory` – adds `-a` when you `cp` directory;
|
||||
* `cpp11` – adds missing `-std=c++11` to `g++` or `clang++`;
|
||||
* `dirty_untar` – fixes `tar x` command that untarred in the current directory;
|
||||
* `dirty_unzip` – fixes `unzip` command that unzipped in the current directory;
|
||||
* `django_south_ghost` – adds `--delete-ghost-migrations` to failed because ghosts django south migration;
|
||||
* `django_south_merge` – adds `--merge` to inconsistent django south migration;
|
||||
* `docker_not_command` – fixes wrong docker commands like `docker tags`;
|
||||
@@ -185,6 +187,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
|
||||
* `switch_layout` – switches command from your local layout to en;
|
||||
* `systemctl` – correctly orders parameters of confusing `systemctl`;
|
||||
* `test.py` – runs `py.test` instead of `test.py`;
|
||||
* `tsuru_login` – runs `tsuru login` if not authenticated or session expired;
|
||||
* `tmux` – fixes `tmux` commands;
|
||||
* `whois` – fixes `whois` command.
|
||||
|
||||
@@ -211,8 +214,8 @@ get_new_command(command: Command, settings: Settings) -> str
|
||||
```
|
||||
|
||||
Also the rule can contain an optional function
|
||||
`side_effect(command: Command, settings: Settings) -> None` and an
|
||||
optional boolean `enabled_by_default`.
|
||||
`side_effect(command: Command, settings: Settings) -> None` and
|
||||
optional `enabled_by_default`, `requires_output` and `priority` variables.
|
||||
|
||||
`Command` has three attributes: `script`, `stdout` and `stderr`.
|
||||
|
||||
@@ -236,6 +239,8 @@ def side_effect(command, settings):
|
||||
subprocess.call('chmod 777 .', shell=True)
|
||||
|
||||
priority = 1000 # Lower first, default is 1000
|
||||
|
||||
requires_output = True
|
||||
```
|
||||
|
||||
[More examples of rules](https://github.com/nvbn/thefuck/tree/master/thefuck/rules),
|
||||
@@ -304,5 +309,12 @@ Run unit and functional tests (requires docker):
|
||||
FUNCTIONAL=true py.test
|
||||
```
|
||||
|
||||
For sending package to pypi:
|
||||
|
||||
```bash
|
||||
sudo apt-get install pandoc
|
||||
./release.py
|
||||
```
|
||||
|
||||
## License MIT
|
||||
Project License can be found [here](LICENSE.md).
|
||||
|
||||
@@ -1,4 +1,2 @@
|
||||
[bdist_wheel]
|
||||
universal = 1
|
||||
[metadata]
|
||||
description-file = README.md
|
||||
|
||||
13
setup.py
13
setup.py
@@ -2,16 +2,17 @@
|
||||
from setuptools import setup, find_packages
|
||||
import sys
|
||||
|
||||
if sys.version_info < (2, 7):
|
||||
version = sys.version_info[:2]
|
||||
if version < (2, 7):
|
||||
print('thefuck requires Python version 2.7 or later' +
|
||||
' ({}.{} detected).'.format(*sys.version_info[:2]))
|
||||
' ({}.{} detected).'.format(*version))
|
||||
sys.exit(-1)
|
||||
elif (3, 0) < sys.version_info < (3, 3):
|
||||
elif (3, 0) < version < (3, 3):
|
||||
print('thefuck requires Python version 3.3 or later' +
|
||||
' ({}.{} detected).'.format(*sys.version_info[:2]))
|
||||
' ({}.{} detected).'.format(*version))
|
||||
sys.exit(-1)
|
||||
|
||||
VERSION = '2.4'
|
||||
VERSION = '2.5'
|
||||
|
||||
install_requires = ['psutil', 'colorama', 'six']
|
||||
extras_require = {':python_version<"3.4"': ['pathlib']}
|
||||
@@ -25,6 +26,8 @@ setup(name='thefuck',
|
||||
license='MIT',
|
||||
packages=find_packages(exclude=['ez_setup', 'examples',
|
||||
'tests', 'release']),
|
||||
setup_requires=['setuptools-markdown'],
|
||||
long_description_markdown_filename='README.md',
|
||||
include_package_data=True,
|
||||
zip_safe=False,
|
||||
install_requires=install_requires,
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
from pexpect import TIMEOUT
|
||||
|
||||
|
||||
def with_confirmation(proc):
|
||||
"""Ensures that command can be fixed when confirmation enabled."""
|
||||
proc.sendline(u'mkdir -p ~/.thefuck')
|
||||
@@ -6,12 +9,24 @@ def with_confirmation(proc):
|
||||
proc.sendline(u'ehco test')
|
||||
|
||||
proc.sendline(u'fuck')
|
||||
proc.expect(u'echo test')
|
||||
proc.expect(u'enter')
|
||||
proc.expect_exact(u'ctrl+c')
|
||||
assert proc.expect([TIMEOUT, u'echo test'])
|
||||
assert proc.expect([TIMEOUT, u'enter'])
|
||||
assert proc.expect_exact([TIMEOUT, u'ctrl+c'])
|
||||
proc.send('\n')
|
||||
|
||||
proc.expect(u'test')
|
||||
assert proc.expect([TIMEOUT, u'test'])
|
||||
|
||||
|
||||
def history_changed(proc):
|
||||
"""Ensures that history changed."""
|
||||
proc.send('\033[A')
|
||||
assert proc.expect([TIMEOUT, u'echo test'])
|
||||
|
||||
|
||||
def history_not_changed(proc):
|
||||
"""Ensures that history not changed."""
|
||||
proc.send('\033[A')
|
||||
assert proc.expect([TIMEOUT, u'fuck'])
|
||||
|
||||
|
||||
def refuse_with_confirmation(proc):
|
||||
@@ -22,12 +37,12 @@ def refuse_with_confirmation(proc):
|
||||
proc.sendline(u'ehco test')
|
||||
|
||||
proc.sendline(u'fuck')
|
||||
proc.expect(u'echo test')
|
||||
proc.expect(u'enter')
|
||||
proc.expect_exact(u'ctrl+c')
|
||||
assert proc.expect([TIMEOUT, u'echo test'])
|
||||
assert proc.expect([TIMEOUT, u'enter'])
|
||||
assert proc.expect_exact([TIMEOUT, u'ctrl+c'])
|
||||
proc.send('\003')
|
||||
|
||||
proc.expect(u'Aborted')
|
||||
assert proc.expect([TIMEOUT, u'Aborted'])
|
||||
|
||||
|
||||
def without_confirmation(proc):
|
||||
@@ -38,5 +53,5 @@ def without_confirmation(proc):
|
||||
proc.sendline(u'ehco test')
|
||||
|
||||
proc.sendline(u'fuck')
|
||||
proc.expect(u'echo test')
|
||||
proc.expect(u'test')
|
||||
assert proc.expect([TIMEOUT, u'echo test'])
|
||||
assert proc.expect([TIMEOUT, u'test'])
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from tests.functional.plots import with_confirmation, without_confirmation, \
|
||||
refuse_with_confirmation
|
||||
refuse_with_confirmation, history_changed, history_not_changed
|
||||
from tests.functional.utils import spawn, functional, images
|
||||
|
||||
containers = images(('ubuntu-python3-bash', u'''
|
||||
@@ -22,21 +22,30 @@ RUN pip2 install -U pip setuptools
|
||||
@pytest.mark.parametrize('tag, dockerfile', containers)
|
||||
def test_with_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'bash') as proc:
|
||||
proc.sendline(u"export PS1='$ '")
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
with_confirmation(proc)
|
||||
history_changed(proc)
|
||||
|
||||
|
||||
@functional
|
||||
@pytest.mark.parametrize('tag, dockerfile', containers)
|
||||
def test_refuse_with_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'bash') as proc:
|
||||
proc.sendline(u"export PS1='$ '")
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
refuse_with_confirmation(proc)
|
||||
history_not_changed(proc)
|
||||
|
||||
|
||||
@functional
|
||||
@pytest.mark.parametrize('tag, dockerfile', containers)
|
||||
def test_without_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'bash') as proc:
|
||||
proc.sendline(u"export PS1='$ '")
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
without_confirmation(proc)
|
||||
history_changed(proc)
|
||||
|
||||
@@ -49,3 +49,5 @@ def test_without_confirmation(tag, dockerfile):
|
||||
proc.sendline(u'thefuck-alias > ~/.config/fish/config.fish')
|
||||
proc.sendline(u'fish')
|
||||
without_confirmation(proc)
|
||||
|
||||
# TODO: ensure that history changes.
|
||||
|
||||
@@ -43,3 +43,5 @@ def test_without_confirmation(tag, dockerfile):
|
||||
proc.sendline(u'tcsh')
|
||||
proc.sendline(u'eval `thefuck-alias`')
|
||||
without_confirmation(proc)
|
||||
|
||||
# TODO: ensure that history changes.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
from tests.functional.utils import spawn, functional, images
|
||||
from tests.functional.plots import with_confirmation, without_confirmation, \
|
||||
refuse_with_confirmation
|
||||
refuse_with_confirmation, history_changed, history_not_changed
|
||||
|
||||
containers = images(('ubuntu-python3-zsh', u'''
|
||||
FROM ubuntu:latest
|
||||
@@ -23,7 +23,10 @@ RUN pip2 install -U pip setuptools
|
||||
def test_with_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'zsh') as proc:
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'export HISTFILE=~/.zsh_history')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
with_confirmation(proc)
|
||||
history_changed(proc)
|
||||
|
||||
|
||||
@functional
|
||||
@@ -31,7 +34,10 @@ def test_with_confirmation(tag, dockerfile):
|
||||
def test_refuse_with_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'zsh') as proc:
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'export HISTFILE=~/.zsh_history')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
refuse_with_confirmation(proc)
|
||||
history_not_changed(proc)
|
||||
|
||||
|
||||
@functional
|
||||
@@ -39,4 +45,7 @@ def test_refuse_with_confirmation(tag, dockerfile):
|
||||
def test_without_confirmation(tag, dockerfile):
|
||||
with spawn(tag, dockerfile, u'zsh') as proc:
|
||||
proc.sendline(u'eval $(thefuck-alias)')
|
||||
proc.sendline(u'export HISTFILE=~/.zsh_history')
|
||||
proc.sendline(u'touch $HISTFILE')
|
||||
without_confirmation(proc)
|
||||
history_changed(proc)
|
||||
|
||||
62
tests/rules/test_dirty_untar.py
Normal file
62
tests/rules/test_dirty_untar.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import pytest
|
||||
import tarfile
|
||||
from thefuck.rules.dirty_untar import match, get_new_command, side_effect
|
||||
from tests.utils import Command
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tar_error(tmpdir):
|
||||
def fixture(filename):
|
||||
path = os.path.join(str(tmpdir), filename)
|
||||
|
||||
def reset(path):
|
||||
with tarfile.TarFile(path, 'w') as archive:
|
||||
for file in ('a', 'b', 'c'):
|
||||
with open(file, 'w') as f:
|
||||
f.write('*')
|
||||
|
||||
archive.add(file)
|
||||
|
||||
os.remove(file)
|
||||
|
||||
with tarfile.TarFile(path, 'r') as archive:
|
||||
archive.extractall()
|
||||
|
||||
os.chdir(str(tmpdir))
|
||||
reset(path)
|
||||
|
||||
assert(set(os.listdir('.')) == {filename, 'a', 'b', 'c'})
|
||||
|
||||
return fixture
|
||||
|
||||
parametrize_filename = pytest.mark.parametrize('filename', [
|
||||
'foo.tar',
|
||||
'foo.tar.gz',
|
||||
'foo.tgz'])
|
||||
|
||||
parametrize_script = pytest.mark.parametrize('script, fixed', [
|
||||
('tar xvf {}', 'mkdir -p foo && tar xvf {} -C foo'),
|
||||
('tar -xvf {}', 'mkdir -p foo && tar -xvf {} -C foo'),
|
||||
('tar --extract -f {}', 'mkdir -p foo && tar --extract -f {} -C foo')])
|
||||
|
||||
@parametrize_filename
|
||||
@parametrize_script
|
||||
def test_match(tar_error, filename, script, fixed):
|
||||
tar_error(filename)
|
||||
assert match(Command(script=script.format(filename)), None)
|
||||
|
||||
|
||||
@parametrize_filename
|
||||
@parametrize_script
|
||||
def test_side_effect(tar_error, filename, script, fixed):
|
||||
tar_error(filename)
|
||||
side_effect(Command(script=script.format(filename)), None)
|
||||
assert(os.listdir('.') == [filename])
|
||||
|
||||
|
||||
@parametrize_filename
|
||||
@parametrize_script
|
||||
def test_get_new_command(tar_error, filename, script, fixed):
|
||||
tar_error(filename)
|
||||
assert get_new_command(Command(script=script.format(filename)), None) == fixed.format(filename)
|
||||
45
tests/rules/test_dirty_unzip.py
Normal file
45
tests/rules/test_dirty_unzip.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import pytest
|
||||
import zipfile
|
||||
from thefuck.rules.dirty_unzip import match, get_new_command, side_effect
|
||||
from tests.utils import Command
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def zip_error(tmpdir):
|
||||
path = os.path.join(str(tmpdir), 'foo.zip')
|
||||
|
||||
def reset(path):
|
||||
with zipfile.ZipFile(path, 'w') as archive:
|
||||
archive.writestr('a', '1')
|
||||
archive.writestr('b', '2')
|
||||
archive.writestr('c', '3')
|
||||
|
||||
archive.extractall()
|
||||
|
||||
os.chdir(str(tmpdir))
|
||||
reset(path)
|
||||
|
||||
assert(set(os.listdir('.')) == {'foo.zip', 'a', 'b', 'c'})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script', [
|
||||
'unzip foo',
|
||||
'unzip foo.zip'])
|
||||
def test_match(zip_error, script):
|
||||
assert match(Command(script=script), None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script', [
|
||||
'unzip foo',
|
||||
'unzip foo.zip'])
|
||||
def test_side_effect(zip_error, script):
|
||||
side_effect(Command(script=script), None)
|
||||
assert(os.listdir('.') == ['foo.zip'])
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script,fixed', [
|
||||
('unzip foo', 'unzip foo -d foo'),
|
||||
('unzip foo.zip', 'unzip foo.zip -d foo')])
|
||||
def test_get_new_command(zip_error, script, fixed):
|
||||
assert get_new_command(Command(script=script), None) == fixed
|
||||
@@ -11,6 +11,14 @@ def test_match(command):
|
||||
assert match(command, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command(script='mv foo bar/', stderr=""),
|
||||
Command(script='mv foo bar/foo', stderr="mv: permission denied"),
|
||||
])
|
||||
def test_not_match(command):
|
||||
assert not match(command, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command, new_command', [
|
||||
(Command(script='mv foo bar/foo', stderr="mv: cannot move 'foo' to 'bar/foo': No such file or directory"), 'mkdir -p bar && mv foo bar/foo'),
|
||||
(Command(script='mv foo bar/', stderr="mv: cannot move 'foo' to 'bar/': No such file or directory"), 'mkdir -p bar && mv foo bar/'),
|
||||
|
||||
37
tests/rules/test_tsuru_login.py
Normal file
37
tests/rules/test_tsuru_login.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import pytest
|
||||
from thefuck.rules.tsuru_login import match, get_new_command
|
||||
from tests.utils import Command
|
||||
|
||||
|
||||
error_msg = (
|
||||
"Error: you're not authenticated or your session has expired.",
|
||||
("You're not authenticated or your session has expired. "
|
||||
"Please use \"login\" command for authentication."),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command(script='tsuru app-shell', stderr=error_msg[0]),
|
||||
Command(script='tsuru app-log -f', stderr=error_msg[1]),
|
||||
])
|
||||
def test_match(command):
|
||||
assert match(command, {})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command(script='tsuru'),
|
||||
Command(script='tsuru app-restart', stderr=('Error: unauthorized')),
|
||||
Command(script='tsuru app-log -f', stderr=('Error: unparseable data')),
|
||||
])
|
||||
def test_not_match(command):
|
||||
assert not match(command, {})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command, new_command', [
|
||||
(Command('tsuru app-shell', stderr=error_msg[0]),
|
||||
'tsuru login && tsuru app-shell'),
|
||||
(Command('tsuru app-log -f', stderr=error_msg[1]),
|
||||
'tsuru login && tsuru app-log -f'),
|
||||
])
|
||||
def test_get_new_command(command, new_command):
|
||||
assert get_new_command(command, {}) == new_command
|
||||
@@ -14,7 +14,8 @@ def test_load_rule(mocker):
|
||||
return_value=Mock(match=match,
|
||||
get_new_command=get_new_command,
|
||||
enabled_by_default=True,
|
||||
priority=900))
|
||||
priority=900,
|
||||
requires_output=True))
|
||||
assert main.load_rule(Path('/rules/bash.py')) \
|
||||
== Rule('bash', match, get_new_command, priority=900)
|
||||
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
||||
@@ -152,7 +153,7 @@ class TestConfirm(object):
|
||||
|
||||
def test_with_side_effect_and_without_confirmation(self, capsys):
|
||||
assert main.confirm('command', Mock(), Mock(require_confirmation=False))
|
||||
assert capsys.readouterr() == ('', 'command*\n')
|
||||
assert capsys.readouterr() == ('', 'command (+side effect)\n')
|
||||
|
||||
# `stdin` fixture should be applied after `capsys`
|
||||
def test_when_confirmation_required_and_confirmed(self, capsys, stdin):
|
||||
@@ -164,7 +165,7 @@ class TestConfirm(object):
|
||||
def test_when_confirmation_required_and_confirmed_with_side_effect(self, capsys, stdin):
|
||||
assert main.confirm('command', Mock(), Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert capsys.readouterr() == ('', 'command* [enter/ctrl+c]')
|
||||
assert capsys.readouterr() == ('', 'command (+side effect) [enter/ctrl+c]')
|
||||
|
||||
def test_when_confirmation_required_and_aborted(self, capsys, stdin):
|
||||
stdin.side_effect = KeyboardInterrupt
|
||||
|
||||
@@ -10,7 +10,8 @@ def Rule(name='', match=lambda *_: True,
|
||||
get_new_command=lambda *_: '',
|
||||
enabled_by_default=True,
|
||||
side_effect=None,
|
||||
priority=DEFAULT_PRIORITY):
|
||||
priority=DEFAULT_PRIORITY,
|
||||
requires_output=True):
|
||||
return types.Rule(name, match, get_new_command,
|
||||
enabled_by_default, side_effect,
|
||||
priority)
|
||||
priority, requires_output)
|
||||
|
||||
@@ -29,19 +29,19 @@ def rule_failed(rule, exc_info, settings):
|
||||
|
||||
|
||||
def show_command(new_command, side_effect, settings):
|
||||
sys.stderr.write('{bold}{command}{side_effect}{reset}\n'.format(
|
||||
sys.stderr.write('{bold}{command}{reset}{side_effect}\n'.format(
|
||||
command=new_command,
|
||||
side_effect='*' if side_effect else '',
|
||||
side_effect=' (+side effect)' if side_effect else '',
|
||||
bold=color(colorama.Style.BRIGHT, settings),
|
||||
reset=color(colorama.Style.RESET_ALL, settings)))
|
||||
|
||||
|
||||
def confirm_command(new_command, side_effect, settings):
|
||||
sys.stderr.write(
|
||||
'{bold}{command}{side_effect}{reset} '
|
||||
'{bold}{command}{reset}{side_effect} '
|
||||
'[{green}enter{reset}/{red}ctrl+c{reset}]'.format(
|
||||
command=new_command,
|
||||
side_effect='*' if side_effect else '',
|
||||
side_effect=' (+side effect)' if side_effect else '',
|
||||
bold=color(colorama.Style.BRIGHT, settings),
|
||||
green=color(colorama.Fore.GREEN, settings),
|
||||
red=color(colorama.Fore.RED, settings),
|
||||
|
||||
@@ -28,7 +28,8 @@ def load_rule(rule):
|
||||
rule_module.get_new_command,
|
||||
getattr(rule_module, 'enabled_by_default', True),
|
||||
getattr(rule_module, 'side_effect', None),
|
||||
getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY))
|
||||
getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY),
|
||||
getattr(rule_module, 'requires_output', True))
|
||||
|
||||
|
||||
def _get_loaded_rules(rules, settings):
|
||||
@@ -87,13 +88,26 @@ def get_command(settings, args):
|
||||
settings):
|
||||
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE, env=env)
|
||||
if wait_output(settings, result):
|
||||
return types.Command(script, result.stdout.read().decode('utf-8'),
|
||||
result.stderr.read().decode('utf-8'))
|
||||
stdout = result.stdout.read().decode('utf-8')
|
||||
stderr = result.stderr.read().decode('utf-8')
|
||||
|
||||
logs.debug(u'Received stdout: {}'.format(stdout), settings)
|
||||
logs.debug(u'Received stderr: {}'.format(stderr), settings)
|
||||
|
||||
return types.Command(script, stdout, stderr)
|
||||
else:
|
||||
logs.debug(u'Execution timed out!', settings)
|
||||
return types.Command(script, None, None)
|
||||
|
||||
|
||||
def get_matched_rule(command, rules, settings):
|
||||
"""Returns first matched rule for command."""
|
||||
script_only = command.stdout is None and command.stderr is None
|
||||
|
||||
for rule in rules:
|
||||
if script_only and rule.requires_output:
|
||||
continue
|
||||
|
||||
try:
|
||||
with logs.debug_time(u'Trying rule: {};'.format(rule.name),
|
||||
settings):
|
||||
@@ -138,20 +152,16 @@ def main():
|
||||
logs.debug(u'Run with settings: {}'.format(pformat(settings)), settings)
|
||||
|
||||
command = get_command(settings, sys.argv)
|
||||
if command:
|
||||
logs.debug(u'Received stdout: {}'.format(command.stdout), settings)
|
||||
logs.debug(u'Received stderr: {}'.format(command.stderr), settings)
|
||||
rules = get_rules(user_dir, settings)
|
||||
logs.debug(
|
||||
u'Loaded rules: {}'.format(', '.join(rule.name for rule in rules)),
|
||||
settings)
|
||||
|
||||
rules = get_rules(user_dir, settings)
|
||||
logs.debug(
|
||||
u'Loaded rules: {}'.format(', '.join(rule.name for rule in rules)),
|
||||
settings)
|
||||
|
||||
matched_rule = get_matched_rule(command, rules, settings)
|
||||
if matched_rule:
|
||||
logs.debug(u'Matched rule: {}'.format(matched_rule.name), settings)
|
||||
run_rule(matched_rule, command, settings)
|
||||
return
|
||||
matched_rule = get_matched_rule(command, rules, settings)
|
||||
if matched_rule:
|
||||
logs.debug(u'Matched rule: {}'.format(matched_rule.name), settings)
|
||||
run_rule(matched_rule, command, settings)
|
||||
return
|
||||
|
||||
logs.failed('No fuck given', settings)
|
||||
|
||||
|
||||
41
thefuck/rules/dirty_untar.py
Normal file
41
thefuck/rules/dirty_untar.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from thefuck import shells
|
||||
import os
|
||||
import tarfile
|
||||
|
||||
|
||||
def _is_tar_extract(cmd):
|
||||
if '--extract' in cmd:
|
||||
return True
|
||||
|
||||
cmd = cmd.split()
|
||||
|
||||
return len(cmd) > 1 and 'x' in cmd[1]
|
||||
|
||||
|
||||
def _tar_file(cmd):
|
||||
tar_extentions = ('.tar', '.tar.Z', '.tar.bz2', '.tar.gz', '.tar.lz',
|
||||
'.tar.lzma', '.tar.xz', '.taz', '.tb2', '.tbz', '.tbz2',
|
||||
'.tgz', '.tlz', '.txz', '.tz')
|
||||
|
||||
for c in cmd.split():
|
||||
for ext in tar_extentions:
|
||||
if c.endswith(ext):
|
||||
return (c, c[0:len(c)-len(ext)])
|
||||
|
||||
|
||||
def match(command, settings):
|
||||
return (command.script.startswith('tar')
|
||||
and '-C' not in command.script
|
||||
and _is_tar_extract(command.script)
|
||||
and _tar_file(command.script) is not None)
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \
|
||||
.format(dir=_tar_file(command.script)[1], cmd=command.script)
|
||||
|
||||
|
||||
def side_effect(command, settings):
|
||||
with tarfile.TarFile(_tar_file(command.script)[0]) as archive:
|
||||
for file in archive.getnames():
|
||||
os.remove(file)
|
||||
39
thefuck/rules/dirty_unzip.py
Normal file
39
thefuck/rules/dirty_unzip.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
import zipfile
|
||||
|
||||
|
||||
def _is_bad_zip(file):
|
||||
with zipfile.ZipFile(file, 'r') as archive:
|
||||
return len(archive.namelist()) > 1
|
||||
|
||||
|
||||
def _zip_file(command):
|
||||
# unzip works that way:
|
||||
# unzip [-flags] file[.zip] [file(s) ...] [-x file(s) ...]
|
||||
# ^ ^ files to unzip from the archive
|
||||
# archive to unzip
|
||||
for c in command.script.split()[1:]:
|
||||
if not c.startswith('-'):
|
||||
if c.endswith('.zip'):
|
||||
return c
|
||||
else:
|
||||
return '{}.zip'.format(c)
|
||||
|
||||
|
||||
def match(command, settings):
|
||||
return (command.script.startswith('unzip')
|
||||
and '-d' not in command.script
|
||||
and _is_bad_zip(_zip_file(command)))
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
return '{} -d {}'.format(command.script, _zip_file(command)[:-4])
|
||||
|
||||
|
||||
def side_effect(command, settings):
|
||||
with zipfile.ZipFile(_zip_file(command), 'r') as archive:
|
||||
for file in archive.namelist():
|
||||
os.remove(file)
|
||||
|
||||
|
||||
requires_output = False
|
||||
@@ -1,5 +1,4 @@
|
||||
from thefuck import utils
|
||||
from thefuck.shells import and_
|
||||
from thefuck import utils, shells
|
||||
from thefuck.utils import replace_argument
|
||||
|
||||
|
||||
@@ -13,5 +12,5 @@ def match(command, settings):
|
||||
|
||||
@utils.git_support
|
||||
def get_new_command(command, settings):
|
||||
return and_(replace_argument(command.script, 'push', 'pull'),
|
||||
command.script)
|
||||
return shells.and_(replace_argument(command.script, 'push', 'pull'),
|
||||
command.script)
|
||||
|
||||
11
thefuck/rules/tsuru_login.py
Normal file
11
thefuck/rules/tsuru_login.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from thefuck import shells
|
||||
|
||||
|
||||
def match(command, settings):
|
||||
return (command.script.startswith('tsuru')
|
||||
and 'not authenticated' in command.stderr
|
||||
and 'session has expired' in command.stderr)
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
return shells.and_('tsuru login', command.script)
|
||||
@@ -5,7 +5,7 @@ Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||
|
||||
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
||||
'enabled_by_default', 'side_effect',
|
||||
'priority'))
|
||||
'priority', 'requires_output'))
|
||||
|
||||
|
||||
class RulesNamesList(list):
|
||||
|
||||
Reference in New Issue
Block a user