fix temp cleanup not working on read-only files

This commit is contained in:
Lukas 2021-11-07 15:37:22 +01:00
parent 153c08d37f
commit 2622804066
1 changed files with 11 additions and 10 deletions

View File

@ -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__":