From 262280406677784e5d3266544fcd8735aadf0dfc Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 7 Nov 2021 15:37:22 +0100 Subject: [PATCH] fix temp cleanup not working on read-only files --- cloudy.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cloudy.py b/cloudy.py index 8b7d546..f550626 100644 --- a/cloudy.py +++ b/cloudy.py @@ -45,11 +45,7 @@ def create_duplicate_dir(folder): print("[*] Trying to create a Duplicate, please wait...") if os.path.exists(dst): - try: - shutil.rmtree(dst, onerror=del_rw) - except PermissionError as e: - print(f"[X] Error: {e}") - exit(1) + shutil.rmtree(dst, onerror=del_rw) shutil.copytree(src, dst) print("[*] Successfully created Duplicate") @@ -57,10 +53,10 @@ def create_duplicate_dir(folder): return dst -# inspired by https://stackoverflow.com/questions/60087965/how-to-zip-a-folder-in-python-with-password def zip_folder(folder): """ Zips the duplicated folder in the tempdir. + src: https://stackoverflow.com/questions/60087965/how-to-zip-a-folder-in-python-with-password """ print("[*] Trying to create corresponding zipfile") @@ -131,10 +127,14 @@ def upload_to_nextcloud(file): print(f"[*] Finished uploading to {url}") -def clean_temp(tempdir): - if os.path.exists(tempdir): - print("Cleaning up the tempdir [", tempdir, "]...") - shutil.rmtree(tempdir, ignore_errors=True) +def clean_temp(files): + for file in files: + if os.path.isfile(file) or os.path.islink(file): + os.remove(file) + elif os.path.isdir(file): + shutil.rmtree(file, onerror=del_rw) + else: + raise ValueError(f"[X] Error: [{file}] is not a file or directory.") def main(): @@ -149,6 +149,7 @@ def main(): dupe = create_duplicate_dir(folder) zipped = zip_folder(dupe) upload_to_nextcloud(zipped) + clean_temp([dupe, zipped]) if __name__ == "__main__":