1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 02:01:13 +00:00

Add a dirty_unzip rule

This commit is contained in:
mcarton 2015-07-21 20:45:10 +02:00
parent 71bb1994c3
commit 4e7eceaa3a

View 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