Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9c7a390
Clean codejail project and upgrade dependencies
antoineKorbit Dec 10, 2020
4aaae33
Add custom JSON encoder for external library and clean code
antoineKorbit Dec 21, 2020
e8aba01
Change subproc session id sets and pkill command path
antoineKorbit Dec 21, 2020
390e9ac
Code cleaning before merging
antoineKorbit Apr 13, 2021
a7c1364
Merge pull request #1 from antoineKorbit/development
antoineKorbit Apr 14, 2021
b5b69a0
SW-1121-dataset-saving
farfaraji Apr 29, 2021
72fca0e
Merge branch 'development' into SW-1121-dataset-saving
farfaraji Apr 29, 2021
69fe6a9
fix-dataset-extension
farfaraji Apr 29, 2021
4614ae9
fixes
farfaraji Apr 29, 2021
9cad247
fixes
farfaraji Apr 29, 2021
98ff187
Merge pull request #3 from antoineKorbit/SW-1121-dataset-saving
antoineKorbit Apr 29, 2021
7bde8c4
fix-the-bug
farfaraji Apr 29, 2021
e8394ca
remove-prints
farfaraji Apr 29, 2021
debc779
Merge pull request #4 from antoineKorbit/fix-bug-artifacts-save
farfaraji Apr 29, 2021
c6c695a
pull-artifacts-in-validation
farfaraji Apr 30, 2021
19d1eaa
Merge pull request #5 from korbit-ai/pull-artifacts-in-validation
farfaraji May 3, 2021
aa8e30f
Remove artifacts restrictions and add videos folder
antoineKorbit Jul 15, 2021
7dfb11b
Merge pull request #6 from korbit-ai/remove-artifacts-extension-valid…
antoineKorbit Jul 15, 2021
e19b0e8
add custome exception for timeout on safe_exec
antoineKorbit Jan 6, 2022
fd440ad
Merge pull request #7 from antoineKorbit/timeout_exception
antoineKorbit Jan 6, 2022
422cf04
Fix JSON TypeError on dict key can now be a numpy type
antoineKorbit Mar 1, 2022
954acf0
Merge pull request #8 from korbit-ai/fix-json-key-type-crash
antoineKorbit Mar 1, 2022
283fa68
Fix codejail numpy encoder for
antoineKorbit Mar 18, 2022
fde0eb9
Add the extraction of zip file in dataset folder
antoineKorbit Aug 23, 2022
e5e7602
Move proxy_main.py to be included in codejail module
ariellasmo Aug 24, 2022
ae0026b
Merge pull request #9 from korbit-ai/MLA-588-codejail-lib
ariellasmo Aug 25, 2022
2495c5f
Move import to the top: respect pep8 convention
antoineKorbit Sep 5, 2022
af33e5c
Merge pull request #10 from korbit-ai/add-zip-file-datatset
antoineKorbit Sep 5, 2022
1081640
Update not_safe_exec to handle artifacts (#11)
ariellasmo Sep 8, 2022
1d9cf71
Fix custom_encoder imports (#12)
ariellasmo Sep 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ build
coverage.xml
htmlcov
reports/*
.tox/*
.tox
.pytest_cache

# Editor detritus
*~
Expand Down
76 changes: 0 additions & 76 deletions Jenkinsfile

This file was deleted.

32 changes: 32 additions & 0 deletions codejail/custom_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json


class GlobalEncoder(json.JSONEncoder):
def default(self, obj):
if type(obj).__module__ == 'numpy':
return NumpyEncoder().default(obj)
elif type(obj).__module__ == 'pandas':
if hasattr(obj, 'to_json'):
return obj.to_json(orient='records')
return repr(obj)
return json.JSONEncoder.default(self, obj)


class NumpyEncoder(json.JSONEncoder):
""" Custom encoder for numpy data types """

def default(self, obj):
import numpy as np
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
return float(obj)
elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
return {'real': obj.real, 'imag': obj.imag}
elif isinstance(obj, (np.ndarray)):
return obj.tolist()
elif isinstance(obj, (np.bool_)):
return bool(obj)
return repr(obj)
27 changes: 0 additions & 27 deletions codejail/django_integration.py

This file was deleted.

31 changes: 0 additions & 31 deletions codejail/django_integration_utils.py

This file was deleted.

44 changes: 37 additions & 7 deletions codejail/jail_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import resource
import shutil
import sys
import zipfile
from builtins import bytes

from .proxy import run_subprocess_through_proxy
from .subproc import run_subprocess
from .util import temp_directory
from .util import temp_directory, create_directory, chmod_recursive_directory

log = logging.getLogger("codejail")

Expand Down Expand Up @@ -38,7 +39,8 @@ def configure(command, bin_path, user=None):
if command == "python":
# -E means ignore the environment variables PYTHON*
# -B means don't try to write .pyc files.
cmd_argv.extend(['-E', '-B'])
# -u force the stdout and stderr streams to be unbuffered
cmd_argv.extend(['-E', '-B', '-u'])

COMMANDS[command] = {
# The start of the command line for this program.
Expand Down Expand Up @@ -90,7 +92,7 @@ def is_configured(command):
# Size of files creatable, in bytes, defaulting to nothing can be written.
"FSIZE": 0,
# The number of processes and threads to allow.
"NPROC": 15,
"NPROC": 50,
# Whether to use a proxy process or not. None means use an environment
# variable to decide. NOTE: using a proxy process is NOT THREAD-SAFE, only
# one thread can use CodeJail at a time if you are using a proxy process.
Expand Down Expand Up @@ -186,7 +188,8 @@ def __init__(self):


def jail_code(command, code=None, files=None, extra_files=None, argv=None,
stdin=None, limit_overrides_context=None, slug=None):
stdin=None, limit_overrides_context=None, slug=None,
artifacts=None):
"""
Run code in a jailed subprocess.

Expand Down Expand Up @@ -239,8 +242,7 @@ def jail_code(command, code=None, files=None, extra_files=None, argv=None,

# Make directory readable by other users ('sandbox' user needs to be
# able to read it).
os.chmod(homedir, 0o775)

os.chmod(homedir, 0o751)
# Make a subdir to use for temp files, world-writable so that the
# sandbox user can write to it.
tmptmp = os.path.join(homedir, "tmp")
Expand All @@ -259,11 +261,14 @@ def jail_code(command, code=None, files=None, extra_files=None, argv=None,
else:
shutil.copytree(filename, dest, symlinks=True)

if artifacts:
save_artifacts(artifacts, tmptmp)
# Create the main file.
if code:
with open(os.path.join(homedir, "jailed_code"), "wb") as jailed:
code_bytes = bytes(code, 'utf8')
jailed.write(code_bytes)
os.chmod(os.path.join(homedir, "jailed_code"), 0o750)

argv = ["jailed_code"] + argv

Expand Down Expand Up @@ -320,7 +325,7 @@ def jail_code(command, code=None, files=None, extra_files=None, argv=None,
stdin=stdin,
realtime=effective_limits["REALTIME"],
rlimits=create_rlimits(effective_limits),
)
)

result = JailResult()
result.status = status
Expand Down Expand Up @@ -375,3 +380,28 @@ def create_rlimits(effective_limits):
rlimits.append((resource.RLIMIT_FSIZE, (fsize, fsize)))

return rlimits


def save_artifacts(artifacts, save_path):
datasets_dest_dir = create_directory(save_path, 'datasets')
images_dest_dir = create_directory(save_path, 'images')
videos_dest_dir = create_directory(save_path, 'videos')
for artifact_path in artifacts:
path = datasets_dest_dir
if artifact_path.endswith(('.png', '.jpg', '.jpeg')):
path = images_dest_dir
elif artifact_path.endswith(('.mp4', '.mov', '.gif', '.avi', '.mpeg', '.mkv')):
path = videos_dest_dir

if artifact_path.endswith('.zip'):
with zipfile.ZipFile(artifact_path, 'r') as zip_ref:
zip_ref.extractall(path)
chmod_recursive_directory(path, 0o777)
else:

with open(artifact_path, 'r') as file:
content = file.read()
new_file = os.path.join(path, os.path.basename(artifact_path))
with open(new_file, 'w') as file:
file.write(content)
os.chmod(new_file, 0o777)
4 changes: 2 additions & 2 deletions codejail/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_proxy():
# If we need a proxy, make a proxy.
if PROXY_PROCESS is None:
# Start the proxy by invoking proxy_main.py in our root directory.
root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
root = os.path.dirname(os.path.realpath(__file__))
proxy_main_py = os.path.join(root, "proxy_main.py")

# Run proxy_main.py with the same Python that is running us. "-u" makes
Expand All @@ -128,7 +128,7 @@ def get_proxy():
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
)
log.info("Started CodeJail proxy process (pid %d)", PROXY_PROCESS.pid)

return PROXY_PROCESS
Expand Down
File renamed without changes.
Loading