Merge branch 'unzip-clean' of https://github.com/mcarton/thefuck into mcarton-unzip-clean
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user