Commit 4dfe5069 by Ned Batchelder

Simplify these decorators, since we don't use the classes here anyway.

parent 66fd6573
...@@ -6,38 +6,21 @@ import shutil ...@@ -6,38 +6,21 @@ import shutil
import tempfile import tempfile
class TempDirectory(object):
def __init__(self):
self.temp_dir = tempfile.mkdtemp(prefix="codejail-")
# Make directory readable by other users ('sandbox' user needs to be
# able to read it).
os.chmod(self.temp_dir, 0775)
def clean_up(self):
# if this errors, something is genuinely wrong, so don't ignore errors.
shutil.rmtree(self.temp_dir)
@contextlib.contextmanager @contextlib.contextmanager
def temp_directory(): def temp_directory():
""" """
A context manager to make and use a temp directory. A context manager to make and use a temp directory.
The directory will be removed when done. The directory will be removed when done.
""" """
tmp = TempDirectory() temp_dir = tempfile.mkdtemp(prefix="codejail-")
# Make directory readable by other users ('sandbox' user needs to be
# able to read it).
os.chmod(temp_dir, 0775)
try: try:
yield tmp.temp_dir yield temp_dir
finally: finally:
tmp.clean_up() # if this errors, something is genuinely wrong, so don't ignore errors.
shutil.rmtree(temp_dir)
class ChangeDirectory(object):
def __init__(self, new_dir):
self.old_dir = os.getcwd()
os.chdir(new_dir)
def clean_up(self):
os.chdir(self.old_dir)
@contextlib.contextmanager @contextlib.contextmanager
...@@ -45,8 +28,9 @@ def change_directory(new_dir): ...@@ -45,8 +28,9 @@ def change_directory(new_dir):
""" """
A context manager to change the directory, and then change it back. A context manager to change the directory, and then change it back.
""" """
cd = ChangeDirectory(new_dir) old_dir = os.getcwd()
os.chdir(new_dir)
try: try:
yield new_dir yield new_dir
finally: finally:
cd.clean_up() os.chdir(old_dir)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment