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
e4151f31
Commit
e4151f31
authored
May 10, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a .coveragerc, and tweak to improve coverage results.
parent
50ad8f33
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
16 deletions
+26
-16
.coveragerc
+5
-0
codejail/jail_code.py
+2
-2
codejail/safe_exec.py
+4
-3
codejail/tests/test_safe_exec.py
+8
-2
codejail/util.py
+7
-9
No files found.
.coveragerc
0 → 100644
View file @
e4151f31
[run]
source = codejail
branch = true
omit = codejail/tests/doit.py
codejail/jail_code.py
View file @
e4151f31
...
@@ -130,7 +130,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
...
@@ -130,7 +130,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
if
not
is_configured
(
command
):
if
not
is_configured
(
command
):
raise
Exception
(
"jail_code needs to be configured for
%
r"
%
command
)
raise
Exception
(
"jail_code needs to be configured for
%
r"
%
command
)
with
temp_directory
(
delete_when_done
=
True
)
as
tmpdir
:
with
temp_directory
()
as
tmpdir
:
log
.
debug
(
"Executing jailed code:
%
r"
,
code
)
log
.
debug
(
"Executing jailed code:
%
r"
,
code
)
...
@@ -171,7 +171,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
...
@@ -171,7 +171,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
return
result
return
result
def
set_process_limits
():
def
set_process_limits
():
# pragma: no cover
"""
"""
Set limits on this processs, to be used first in a child process.
Set limits on this processs, to be used first in a child process.
"""
"""
...
...
codejail/safe_exec.py
View file @
e4151f31
...
@@ -119,7 +119,7 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
...
@@ -119,7 +119,7 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
jailed_code
=
""
.
join
(
the_code
)
jailed_code
=
""
.
join
(
the_code
)
# Turn this on to see what's being executed.
# Turn this on to see what's being executed.
if
LOG_ALL_CODE
:
if
LOG_ALL_CODE
:
# pragma: no cover
log
.
debug
(
"Jailed code:
%
s"
,
jailed_code
)
log
.
debug
(
"Jailed code:
%
s"
,
jailed_code
)
log
.
debug
(
"Exec:
%
s"
,
code
)
log
.
debug
(
"Exec:
%
s"
,
code
)
log
.
debug
(
"Stdin:
%
s"
,
stdin
)
log
.
debug
(
"Stdin:
%
s"
,
stdin
)
...
@@ -179,7 +179,7 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
...
@@ -179,7 +179,7 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
g_dict
=
json_safe
(
globals_dict
)
g_dict
=
json_safe
(
globals_dict
)
with
temp_directory
(
delete_when_done
=
True
)
as
tmpdir
:
with
temp_directory
()
as
tmpdir
:
with
change_directory
(
tmpdir
):
with
change_directory
(
tmpdir
):
# Copy the files here.
# Copy the files here.
for
filename
in
files
or
():
for
filename
in
files
or
():
...
@@ -203,5 +203,6 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
...
@@ -203,5 +203,6 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
# Running Python code in the sandbox makes it difficult to debug.
# Running Python code in the sandbox makes it difficult to debug.
if
ALWAYS_BE_UNSAFE
or
not
jail_code
.
is_configured
(
"python"
):
NO_SAFE_PYTHON
=
not
jail_code
.
is_configured
(
"python"
)
if
ALWAYS_BE_UNSAFE
or
NO_SAFE_PYTHON
:
# pragma: no cover
safe_exec
=
not_safe_exec
safe_exec
=
not_safe_exec
codejail/tests/test_safe_exec.py
View file @
e4151f31
...
@@ -22,7 +22,7 @@ class SafeExecTests(unittest.TestCase):
...
@@ -22,7 +22,7 @@ class SafeExecTests(unittest.TestCase):
give the tests something to test.
give the tests something to test.
"""
"""
raise
NotImplementedError
raise
NotImplementedError
# pragma: no cover
def
test_set_values
(
self
):
def
test_set_values
(
self
):
globs
=
{}
globs
=
{}
...
@@ -81,16 +81,22 @@ class SafeExecTests(unittest.TestCase):
...
@@ -81,16 +81,22 @@ class SafeExecTests(unittest.TestCase):
class
TestSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
class
TestSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
"""Run SafeExecTests, with the real safe_exec."""
"""Run SafeExecTests, with the real safe_exec."""
__test__
=
True
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
safe_exec
(
*
args
,
**
kwargs
)
safe_exec
(
*
args
,
**
kwargs
)
class
TestNotSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
class
TestNotSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
"""Run SafeExecTests, with not_safe_exec."""
"""Run SafeExecTests, with not_safe_exec."""
__test__
=
True
def
setUp
(
self
):
def
setUp
(
self
):
# If safe_exec is actually an alias to not_safe_exec, then there's no
# If safe_exec is actually an alias to not_safe_exec, then there's no
# point running these tests.
# point running these tests.
if
safe_exec
is
not_safe_exec
:
if
safe_exec
is
not_safe_exec
:
# pragma: no cover
raise
SkipTest
raise
SkipTest
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
...
...
codejail/util.py
View file @
e4151f31
...
@@ -7,25 +7,23 @@ import tempfile
...
@@ -7,25 +7,23 @@ import tempfile
class
TempDirectory
(
object
):
class
TempDirectory
(
object
):
def
__init__
(
self
,
delete_when_done
=
True
):
def
__init__
(
self
):
self
.
delete_when_done
=
delete_when_done
self
.
temp_dir
=
tempfile
.
mkdtemp
(
prefix
=
"codejail-"
)
self
.
temp_dir
=
tempfile
.
mkdtemp
(
prefix
=
"codejail-"
)
# Make directory readable by other users ('sandbox' user needs to be able to read it)
# Make directory readable by other users ('sandbox' user needs to be able to read it)
os
.
chmod
(
self
.
temp_dir
,
0775
)
os
.
chmod
(
self
.
temp_dir
,
0775
)
def
clean_up
(
self
):
def
clean_up
(
self
):
if
self
.
delete_when_done
:
# if this errors, something is genuinely wrong, so don't ignore errors.
# if this errors, something is genuinely wrong, so don't ignore errors.
shutil
.
rmtree
(
self
.
temp_dir
)
shutil
.
rmtree
(
self
.
temp_dir
)
@contextlib.contextmanager
@contextlib.contextmanager
def
temp_directory
(
delete_when_done
=
True
):
def
temp_directory
():
"""
"""
A context manager to make and use a temp directory.
If `delete_when_done`
A context manager to make and use a temp directory.
is true (the default), t
he directory will be removed when done.
T
he directory will be removed when done.
"""
"""
tmp
=
TempDirectory
(
delete_when_done
)
tmp
=
TempDirectory
()
try
:
try
:
yield
tmp
.
temp_dir
yield
tmp
.
temp_dir
finally
:
finally
:
...
...
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