Download safe_executor.py from 8bits-ai/ZOD-Mini-2D-Road-Scenes: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/resolve/main/safe_executor.py
- Command line
-
hf download hf://datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/safe_executor.py
-
curl -L -o safe_executor.py https://huggingface.co/datasets/8bits-ai/ZOD-Mini-2D-Road-Scenes/resolve/main/safe_executor.py
1.27 kB
| import os | |
| import shutil | |
| class SafeExecutor: | |
| def __init__(self, paths_to_cleanup): | |
| self.paths_to_cleanup = paths_to_cleanup | |
| self.original_state = {} | |
| def __enter__(self): | |
| for path in self.paths_to_cleanup: | |
| if os.path.exists(path): | |
| self.original_state[path] = os.listdir(path) | |
| else: | |
| self.original_state[path] = None | |
| def __exit__(self, exc_type, exc_value, traceback): | |
| if exc_type is not None: | |
| print("An error occurred, reverting changes...") | |
| for path in self.paths_to_cleanup: | |
| if self.original_state[path] is None: | |
| if os.path.exists(path): | |
| shutil.rmtree(path) | |
| else: | |
| current_files = os.listdir(path) | |
| for file in current_files: | |
| if file not in self.original_state[path]: | |
| file_path = os.path.join(path, file) | |
| if os.path.isfile(file_path): | |
| os.remove(file_path) | |
| else: | |
| shutil.rmtree(file_path) | |
| return False # Propagate the exception | |
| return True | |