Merge branch 'unzip-clean' of https://github.com/mcarton/thefuck into mcarton-unzip-clean
This commit is contained in:
+4
-4
@@ -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),
|
||||
|
||||
+26
-16
@@ -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.Script(script)
|
||||
|
||||
|
||||
def get_matched_rule(command, rules, settings):
|
||||
"""Returns first matched rule for command."""
|
||||
script_only = isinstance(command, types.Script)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
import zipfile
|
||||
from thefuck import logs
|
||||
|
||||
|
||||
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
|
||||
+2
-1
@@ -2,10 +2,11 @@ from collections import namedtuple
|
||||
|
||||
|
||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||
Script = namedtuple('Script', ('script'))
|
||||
|
||||
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