Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
4dfe5069
Commit
4dfe5069
authored
May 16, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify these decorators, since we don't use the classes here anyway.
parent
66fd6573
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
26 deletions
+10
-26
codejail/util.py
+10
-26
No files found.
codejail/util.py
View file @
4dfe5069
...
@@ -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
t
mp
.
t
emp_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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment