Clean up directories on failure. Closes #58.

This commit is contained in:
Anthony Sottile 2014-04-03 22:54:27 -07:00
parent 443b62d56a
commit bcb00726a1
8 changed files with 103 additions and 38 deletions

View file

@ -1,6 +1,9 @@
import contextlib
import functools
import os
import os.path
import shutil
import sys
@ -49,3 +52,14 @@ def entry(func):
argv = sys.argv[1:]
return func(argv)
return wrapper
@contextlib.contextmanager
def clean_path_on_failure(path):
"""Cleans up the directory on an exceptional failure."""
try:
yield
except BaseException:
if os.path.exists(path):
shutil.rmtree(path)
raise