From e6f554d80cf13aeeef2c9b77f2b05916f6b98396 Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Thu, 9 Sep 2021 15:44:17 +0800 Subject: [PATCH] feat: extracthere with overwriting and remove __MACOSX automatically --- cli/ranger/commands.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cli/ranger/commands.py b/cli/ranger/commands.py index 8b9fd2c..6b180bc 100644 --- a/cli/ranger/commands.py +++ b/cli/ranger/commands.py @@ -74,6 +74,28 @@ class extracthere(Command): au_flags += self.line.split()[1:] au_flags += ['-e'] + force = '-f' in au_flags + to_delete = set() + # delete target files since atool doesn't support non-interactive overwriting + import subprocess + import re + import os + import shutil + + for copied_file in copied_files: + subproc = subprocess.Popen("atool -l '"+copied_file.path+"'", shell=True, stdout=subprocess.PIPE) + stdout, stdin = subproc.communicate() + if subproc.returncode == 0: + lines = stdout.decode('utf-8').split("\n")[3:-3] + for line in lines: + m = re.search(r'^\s+\d+\s+\d{2,4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}\s+(.+)$', line) + comp_file_path = os.path.join(cwd.path, m.group(1)) + comp_dir_path = os.path.dirname(comp_file_path) + if os.path.basename(comp_dir_path) == '__MACOSX': + to_delete.add(comp_dir_path) + if force and os.path.isfile(comp_file_path): + os.remove(comp_file_path) + self.fm.copy_buffer.clear() self.fm.cut_buffer = False if len(copied_files) == 1: @@ -83,6 +105,10 @@ class extracthere(Command): obj = CommandLoader(args=['aunpack'] + au_flags \ + [f.path for f in copied_files], descr=descr) + # delete "__MACOSX" + for file_to_delete in to_delete: + shutil.rmtree(file_to_delete) + obj.signal_bind('after', refresh) self.fm.loader.add(obj)