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
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
def temp_directory():
"""
A context manager to make and use a temp directory.
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:
yield tmp.temp_dir
yield temp_dir
finally:
tmp.clean_up()
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)
# if this errors, something is genuinely wrong, so don't ignore errors.
shutil.rmtree(temp_dir)
@contextlib.contextmanager
......@@ -45,8 +28,9 @@ def change_directory(new_dir):
"""
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:
yield new_dir
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