diff options
| author | 2019-03-28 21:08:54 -0700 | |
|---|---|---|
| committer | 2019-03-28 21:08:54 -0700 | |
| commit | 94b5ea60fd823ae2d69b339f39d686959c6791de (patch) | |
| tree | 028996ba83a000272f05b00f1d974b0942078749 | |
| parent | Replace RMQ with a POST endpoint (#7) (diff) | |
| parent | Restructure project layout (diff) | |
Merge pull request #15 from python-discord/refactor/restructure
Restructure the Project Layout
| -rw-r--r-- | .flake8 | 2 | ||||
| -rw-r--r-- | Pipfile | 2 | ||||
| -rw-r--r-- | snekbox/__init__.py (renamed from logs.py) | 0 | ||||
| -rw-r--r-- | snekbox/nsjail.py (renamed from snekbox.py) | 42 | ||||
| -rw-r--r-- | snekbox/site/snekapp.py | 38 | ||||
| -rw-r--r-- | snekbox/site/templates/index.html (renamed from templates/index.html) | 0 | ||||
| -rw-r--r-- | snekbox/site/templates/result.html (renamed from templates/result.html) | 0 | ||||
| -rw-r--r-- | tests/test_snekbox.py | 16 | 
8 files changed, 50 insertions, 50 deletions
| @@ -1,6 +1,6 @@  [flake8]  max-line-length=100 -application_import_names=snekbox,config,logs +application_import_names=snekbox  ignore=      P102,B311,W503,E226,S311,      # Missing Docstrings @@ -29,7 +29,7 @@ lint = "flake8"  precommit = "pre-commit install"  test = "pytest tests --cov . --cov-report term-missing -v"  report = "pytest tests --cov . --cov-report=html" -snekbox = "gunicorn -w 2 -b 0.0.0.0:8060 snekbox:app" +snekbox = "gunicorn -w 2 -b 0.0.0.0:8060 snekbox.site.snekapp:app"  buildbox = "docker build -t pythondiscord/snekbox:latest -f docker/Dockerfile ."  pushbox = "docker push pythondiscord/snekbox:latest"  buildboxbase = "docker build -t pythondiscord/snekbox-base:latest -f docker/base.Dockerfile ." diff --git a/logs.py b/snekbox/__init__.py index fc6070e..fc6070e 100644 --- a/logs.py +++ b/snekbox/__init__.py diff --git a/snekbox.py b/snekbox/nsjail.py index 65fc4b3..458a94e 100644 --- a/snekbox.py +++ b/snekbox/nsjail.py @@ -2,11 +2,9 @@ import os  import subprocess  import sys -from flask import Flask, jsonify, render_template, request - -class Snekbox: -    """Core snekbox functionality, providing safe execution of Python code.""" +class NsJail: +    """Core Snekbox functionality, providing safe execution of Python code."""      def __init__(self,                   nsjail_binary='nsjail', @@ -95,39 +93,3 @@ class Snekbox:              return 'unknown error, no error code'          return output - - -snekbox = Snekbox() - -# Load app -app = Flask(__name__) -app.use_reloader = False - -# Logging -log = app.logger - - [email protected]('/') -def index(): -    """Return a page with a form for inputting code to be executed.""" - -    return render_template('index.html') - - [email protected]('/result', methods=["POST", "GET"]) -def result(): -    """Execute code and return a page displaying the results.""" - -    if request.method == "POST": -        code = request.form["Code"] -        output = snekbox.python3(code) -        return render_template('result.html', code=code, result=output) - - [email protected]('/input', methods=["POST"]) -def code_input(): -    """Execute code and return the results.""" - -    body = request.get_json() -    output = snekbox.python3(body["code"]) -    return jsonify(input=body["code"], output=output) diff --git a/snekbox/site/snekapp.py b/snekbox/site/snekapp.py new file mode 100644 index 0000000..492d703 --- /dev/null +++ b/snekbox/site/snekapp.py @@ -0,0 +1,38 @@ +from flask import Flask, jsonify, render_template, request + +from snekbox.nsjail import NsJail + +nsjail = NsJail() + +# Load app +app = Flask(__name__) +app.use_reloader = False + +# Logging +log = app.logger + + [email protected]('/') +def index(): +    """Return a page with a form for inputting code to be executed.""" + +    return render_template('index.html') + + [email protected]('/result', methods=["POST", "GET"]) +def result(): +    """Execute code and return a page displaying the results.""" + +    if request.method == "POST": +        code = request.form["Code"] +        output = nsjail.python3(code) +        return render_template('result.html', code=code, result=output) + + [email protected]('/input', methods=["POST"]) +def code_input(): +    """Execute code and return the results.""" + +    body = request.get_json() +    output = nsjail.python3(body["code"]) +    return jsonify(input=body["code"], output=output) diff --git a/templates/index.html b/snekbox/site/templates/index.html index 41980d1..41980d1 100644 --- a/templates/index.html +++ b/snekbox/site/templates/index.html diff --git a/templates/result.html b/snekbox/site/templates/result.html index e339605..e339605 100644 --- a/templates/result.html +++ b/snekbox/site/templates/result.html diff --git a/tests/test_snekbox.py b/tests/test_snekbox.py index cc79a2a..c08178f 100644 --- a/tests/test_snekbox.py +++ b/tests/test_snekbox.py @@ -1,20 +1,20 @@  import unittest -from snekbox import Snekbox +from snekbox.nsjail import NsJail -snek = Snekbox() +nsjail = NsJail()  class SnekTests(unittest.TestCase):      def test_nsjail(self): -        result = snek.python3('print("test")') +        result = nsjail.python3('print("test")')          self.assertEquals(result.strip(), 'test')      # def test_memory_error(self):      #     code = ('x = "*"\n'      #             'while True:\n'      #             '    x = x * 99\n') -    #     result = snek.python3(code) +    #     result = nsjail.python3(code)      #     self.assertEquals(result.strip(), 'timed out or memory limit exceeded')      def test_timeout(self): @@ -27,13 +27,13 @@ class SnekTests(unittest.TestCase):              '        continue\n'          ) -        result = snek.python3(code) +        result = nsjail.python3(code)          self.assertEquals(result.strip(), 'timed out or memory limit exceeded')      def test_kill(self):          code = ('import subprocess\n'                  'print(subprocess.check_output("kill -9 6", shell=True).decode())') -        result = snek.python3(code) +        result = nsjail.python3(code)          if 'ModuleNotFoundError' in result.strip():              self.assertIn('ModuleNotFoundError', result.strip())          else: @@ -43,7 +43,7 @@ class SnekTests(unittest.TestCase):          code = ('import os\n'                  'while 1:\n'                  '    os.fork()') -        result = snek.python3(code) +        result = nsjail.python3(code)          self.assertIn('Resource temporarily unavailable', result.strip())      def test_juan_golf(self):  # in honour of Juan @@ -52,5 +52,5 @@ class SnekTests(unittest.TestCase):                  "bytecode = CodeType(0,1,0,0,0,b'',(),(),(),'','',1,b'')\n"                  "exec(bytecode)") -        result = snek.python3(code) +        result = nsjail.python3(code)          self.assertEquals('unknown error, code: 111', result.strip()) | 
