mirror of
https://github.com/nvbn/thefuck.git
synced 2025-04-14 06:40:49 +01:00
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import tarfile
|
|
import os
|
|
from thefuck import shells
|
|
from thefuck.utils import for_app
|
|
|
|
|
|
tar_extensions = ('.tar', '.tar.Z', '.tar.bz2', '.tar.gz', '.tar.lz',
|
|
'.tar.lzma', '.tar.xz', '.taz', '.tb2', '.tbz', '.tbz2',
|
|
'.tgz', '.tlz', '.txz', '.tz')
|
|
|
|
|
|
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):
|
|
|
|
for c in cmd:
|
|
for ext in tar_extensions:
|
|
if c.endswith(ext):
|
|
return (c, c[0:len(c) - len(ext)])
|
|
|
|
|
|
@for_app('tar')
|
|
def match(command):
|
|
return ('-C' not in command.script
|
|
and _is_tar_extract(command.script)
|
|
and _tar_file(command.script_parts) is not None)
|
|
|
|
|
|
def get_new_command(command):
|
|
dir = shells.quote(_tar_file(command.script_parts)[1])
|
|
return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \
|
|
.format(dir=dir, cmd=command.script)
|
|
|
|
|
|
def side_effect(old_cmd, command):
|
|
with tarfile.TarFile(_tar_file(old_cmd.script_parts)[0]) as archive:
|
|
for file in archive.getnames():
|
|
try:
|
|
os.remove(file)
|
|
except OSError:
|
|
# does not try to remove directories as we cannot know if they
|
|
# already existed before
|
|
pass
|