From 4e7eceaa3afafe8585c3b680ec4f3d18b0c0e897 Mon Sep 17 00:00:00 2001 From: mcarton Date: Tue, 21 Jul 2015 20:45:10 +0200 Subject: [PATCH] Add a `dirty_unzip` rule --- thefuck/rules/dirty_unzip.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 thefuck/rules/dirty_unzip.py diff --git a/thefuck/rules/dirty_unzip.py b/thefuck/rules/dirty_unzip.py new file mode 100644 index 00000000..503b2bcf --- /dev/null +++ b/thefuck/rules/dirty_unzip.py @@ -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