From c537d4661530c12631ea2a1b5a220c0da1b4fc44 Mon Sep 17 00:00:00 2001 From: Aarif Date: Mon, 18 Jan 2021 19:23:36 +0500 Subject: [PATCH] modernized the repo --- __init__.py | 1 - evil_tests/hello/answer.py | 2 +- evil_tests/hello/wrong_cpu.py | 8 +++--- evil_tests/hello/wrong_fork.py | 6 ++--- evil_tests/hello/wrong_network.py | 2 +- evil_tests/hello/wrong_output.py | 4 +-- evil_tests/hello/wrong_sleep.py | 2 +- evil_tests/hello/wrong_stdout.py | 2 +- evil_tests/isVowel/answer.py | 4 +-- pyxserver_wsgi.py | 34 +++++++++++------------ requirements/base.in | 14 +++++----- requirements/base.txt | 45 ++++++++++++++++--------------- sandbox/__init__.py | 1 - sandbox/sandbox.py | 2 +- sandbox/tests/dirs.py | 2 +- settings.py | 4 +-- tests/hello.py | 2 +- tests/hello/answer.py | 2 +- tests/hello/wrong.py | 2 +- tests/hello/wrong2.py | 2 +- tests/test-runserver.py | 16 +++++------ tests/test.py | 25 +++++++++-------- 22 files changed, 91 insertions(+), 91 deletions(-) diff --git a/__init__.py b/__init__.py index 8b13789..e69de29 100644 --- a/__init__.py +++ b/__init__.py @@ -1 +0,0 @@ - diff --git a/evil_tests/hello/answer.py b/evil_tests/hello/answer.py index da6849a..8cde782 100644 --- a/evil_tests/hello/answer.py +++ b/evil_tests/hello/answer.py @@ -1 +1 @@ -print "hello world" +print("hello world") diff --git a/evil_tests/hello/wrong_cpu.py b/evil_tests/hello/wrong_cpu.py index 44f69ca..d8e5a38 100644 --- a/evil_tests/hello/wrong_cpu.py +++ b/evil_tests/hello/wrong_cpu.py @@ -3,12 +3,12 @@ i += x # correct output, but takes too long -print "hello world" +print("hello world") # Debugging import sys sys.stdout = sys.__stdout__ -print "#" * 80 -print "Still running! Should have been killed." -print "#" * 80 +print("#" * 80) +print("Still running! Should have been killed.") +print("#" * 80) diff --git a/evil_tests/hello/wrong_fork.py b/evil_tests/hello/wrong_fork.py index 494326f..3550824 100644 --- a/evil_tests/hello/wrong_fork.py +++ b/evil_tests/hello/wrong_fork.py @@ -5,12 +5,12 @@ if os.fork() == 0: # in child break - print "forked!" + print("forked!") except: import sys - print "Got exception: " + str(sys.exc_info()) + print("Got exception: " + str(sys.exc_info())) import time time.sleep(60) # Can I see a bunch of these in top? -print "hello world" +print("hello world") diff --git a/evil_tests/hello/wrong_network.py b/evil_tests/hello/wrong_network.py index 537dbcd..dd9da6d 100644 --- a/evil_tests/hello/wrong_network.py +++ b/evil_tests/hello/wrong_network.py @@ -1,4 +1,4 @@ import requests r = requests.get('http://www.edx.org') -print "hello world" +print("hello world") diff --git a/evil_tests/hello/wrong_output.py b/evil_tests/hello/wrong_output.py index 00282bc..8ae11b3 100644 --- a/evil_tests/hello/wrong_output.py +++ b/evil_tests/hello/wrong_output.py @@ -1,6 +1,6 @@ # We start so well... -print "hello world" +print("hello world") # but then get a bit too excited... for i in range(10000): - print "hello world!" + print("hello world!") diff --git a/evil_tests/hello/wrong_sleep.py b/evil_tests/hello/wrong_sleep.py index 905266d..c24d8c4 100644 --- a/evil_tests/hello/wrong_sleep.py +++ b/evil_tests/hello/wrong_sleep.py @@ -1,3 +1,3 @@ import time time.sleep(10) -print "hello world" +print("hello world") diff --git a/evil_tests/hello/wrong_stdout.py b/evil_tests/hello/wrong_stdout.py index 6066908..26ab794 100644 --- a/evil_tests/hello/wrong_stdout.py +++ b/evil_tests/hello/wrong_stdout.py @@ -2,4 +2,4 @@ import sys sys.stdout = sys.__stdout__ -print "O hai" +print("O hai") diff --git a/evil_tests/isVowel/answer.py b/evil_tests/isVowel/answer.py index 58b700d..3ca4a01 100644 --- a/evil_tests/isVowel/answer.py +++ b/evil_tests/isVowel/answer.py @@ -1,9 +1,9 @@ def isVowel(char): - ''' + """ char: a single letter of any case returns: True if char is a vowel and False otherwise. - ''' + """ if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u': return True elif char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U': diff --git a/pyxserver_wsgi.py b/pyxserver_wsgi.py index 633501e..45f9ec0 100644 --- a/pyxserver_wsgi.py +++ b/pyxserver_wsgi.py @@ -15,9 +15,9 @@ import sys from time import localtime, strftime, time -import settings # Not django, but do something similar +from . import settings # Not django, but do something similar -from sandbox import sandbox +from .sandbox import sandbox # make sure we can find the grader files sys.path.append(settings.GRADER_ROOT) @@ -28,7 +28,7 @@ log = logging.getLogger("xserver." + __name__) -results_template = u""" +results_template = """
Test results
@@ -44,7 +44,7 @@ """ -results_correct_template = u""" +results_correct_template = """

{short-description}

{long-description}
@@ -58,7 +58,7 @@ """ -results_incorrect_template = u""" +results_incorrect_template = """

{short-description}

{long-description}
@@ -78,15 +78,15 @@ def format_errors(errors): error_list = [esc(e) for e in errors or []] if error_list: try: - items = u'\n'.join([u'
  • {0}
  • \n'.format(e) for e in error_list]) - error_string = u'
      \n{0}
    \n'.format(items) - error_string = u'
    {0}
    '.format(error_string) + items = '\n'.join([f'
  • {e}
  • \n' for e in error_list]) + error_string = f'
      \n{items}
    \n' + error_string = f'
    {error_string}
    ' except UnicodeDecodeError: # See http://wiki.python.org/moin/UnicodeDecodeError; this error happens in the above unicode encoding # because it's assuming str `e` is in ascii encoding; when it is in Unicode already it gets sad. - items = '\n'.join(['
  • {0}
  • \n'.format(e) for e in error_list]) - error_string = '
      \n{0}
    \n'.format(items) - error_string = '
    {0}
    '.format(error_string) + items = '\n'.join([f'
  • {e}
  • \n' for e in error_list]) + error_string = f'
      \n{items}
    \n' + error_string = f'
    {error_string}
    ' return error_string @@ -95,9 +95,9 @@ def to_dict(result): # TODO: replace with mako template esc = cgi.escape if result[1]: - long_desc = u'

    {0}

    '.format(esc(result[1])) + long_desc = '

    {}

    '.format(esc(result[1])) else: - long_desc = u'' + long_desc = '' return {'short-description': esc(result[0]), 'long-description': long_desc, 'correct': result[2], # Boolean; don't escape. @@ -151,10 +151,10 @@ def do_POST(data): # However, for debugging, still want to see what the problem is statsd.increment('xserver.grader_payload_error') - log.debug("error parsing: '{0}' -- {1}".format(payload, err)) + log.debug(f"error parsing: '{payload}' -- {err}") raise - log.debug("Processing submission, grader payload: {0}".format(payload)) + log.debug(f"Processing submission, grader payload: {payload}") relative_grader_path = grader_config['grader'] grader_path = os.path.join(settings.GRADER_ROOT, relative_grader_path) start = time() @@ -187,13 +187,13 @@ def post_wrapper(data): try: return do_POST(data) except: - log.exception("Error processing request: {0}".format(data)) + log.exception(f"Error processing request: {data}") return None handlers = {'GET': do_GET, 'POST': post_wrapper, } - if method in handlers.keys(): + if method in list(handlers.keys()): reply = handlers[method](data) if reply is not None: diff --git a/requirements/base.in b/requirements/base.in index f37be9c..9bf4a29 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -1,14 +1,14 @@ -Django >= 1.4, < 1.5 +Django<2.3 pika -django_nose==1.4.1 +django_nose nosexcover rednose -requests==2.22.0 -boto==2.6.0 -path.py==10.0 -dogstatsd-python==0.2.1 +requests +boto +path.py +dogstatsd-python south -anyjson==0.3.3 +anyjson numpy==1.6.2 lxml==2.3.6 newrelic==2.46.0.37 diff --git a/requirements/base.txt b/requirements/base.txt index ce09747..582b2eb 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -4,29 +4,32 @@ # # make upgrade # -anyjson==0.3.3 -boto==2.6.0 -certifi==2019.11.28 # via requests -chardet==3.0.4 # via requests -colorama==0.4.3 # via rednose -coverage==5.0.2 # via nosexcover -django==1.4.22 -django_nose==1.4.1 -dogstatsd-python==0.2.1 -gunicorn==0.17.4 -idna==2.8 # via requests -lxml==2.3.6 -newrelic==2.46.0.37 +anyjson==0.3.3 # via -r requirements/base.in +boto==2.49.0 # via -r requirements/base.in +certifi==2020.12.5 # via requests +chardet==4.0.0 # via requests +colorama==0.4.4 # via rednose +coverage==5.3.1 # via nosexcover +django-nose==1.4.7 # via -r requirements/base.in +django==2.2.17 # via -r requirements/base.in +dogstatsd-python==0.5.6 # via -r requirements/base.in +gunicorn==0.17.4 # via -r requirements/base.in +idna==2.10 # via requests +lxml==2.3.6 # via -r requirements/base.in +newrelic==2.46.0.37 # via -r requirements/base.in nose==1.3.7 # via django-nose, nosexcover -nosexcover==1.0.11 -numpy==1.6.2 -path.py==10.0 -pika==1.1.0 -rednose==1.3.0 -requests==2.22.0 -south==1.0.2 +nosexcover==1.0.11 # via -r requirements/base.in +numpy==1.6.2 # via -r requirements/base.in +path.py==12.5.0 # via -r requirements/base.in +path==15.0.1 # via path.py +pika==1.1.0 # via -r requirements/base.in +pytz==2020.5 # via django +rednose==1.3.0 # via -r requirements/base.in +requests==2.25.1 # via -r requirements/base.in +south==1.0.2 # via -r requirements/base.in +sqlparse==0.4.1 # via django termstyle==0.1.11 # via rednose -urllib3==1.25.7 # via requests +urllib3==1.26.2 # via requests # The following packages are considered to be unsafe in a requirements file: # setuptools diff --git a/sandbox/__init__.py b/sandbox/__init__.py index 8b13789..e69de29 100644 --- a/sandbox/__init__.py +++ b/sandbox/__init__.py @@ -1 +0,0 @@ - diff --git a/sandbox/sandbox.py b/sandbox/sandbox.py index 434e192..700c336 100644 --- a/sandbox/sandbox.py +++ b/sandbox/sandbox.py @@ -16,7 +16,7 @@ def record_suspicious_submission(msg, code_str): logging to avoids need for more config changes (S3 credentials, python requirements). """ - log.warning('Suspicious code: {0}, {1}'.format(msg, code_str)) + log.warning(f'Suspicious code: {msg}, {code_str}') def sandbox_cmd_list(): """ diff --git a/sandbox/tests/dirs.py b/sandbox/tests/dirs.py index cacbbc5..c712238 100644 --- a/sandbox/tests/dirs.py +++ b/sandbox/tests/dirs.py @@ -10,7 +10,7 @@ def main(): while True: i += 1 if i % 100 == 0: - print "Made {0} dirs!".format(i) + print(f"Made {i} dirs!") os.mkdir('deepdir') os.chdir('deepdir') diff --git a/settings.py b/settings.py index 4749e63..f4160a0 100644 --- a/settings.py +++ b/settings.py @@ -2,7 +2,7 @@ import json import os -from logsettings import get_logger_config +from .logsettings import get_logger_config from path import Path import sys @@ -29,7 +29,7 @@ # AWS if os.path.isfile(ENV_ROOT / "env.json"): - print "Opening env.json file" + print("Opening env.json file") with open(ENV_ROOT / "env.json") as env_file: ENV_TOKENS = json.load(env_file) diff --git a/tests/hello.py b/tests/hello.py index 4351743..f7cf60e 100644 --- a/tests/hello.py +++ b/tests/hello.py @@ -1 +1 @@ -print "Hello, world!" +print("Hello, world!") diff --git a/tests/hello/answer.py b/tests/hello/answer.py index da6849a..8cde782 100644 --- a/tests/hello/answer.py +++ b/tests/hello/answer.py @@ -1 +1 @@ -print "hello world" +print("hello world") diff --git a/tests/hello/wrong.py b/tests/hello/wrong.py index b732142..11b15b1 100644 --- a/tests/hello/wrong.py +++ b/tests/hello/wrong.py @@ -1 +1 @@ -print "hello" +print("hello") diff --git a/tests/hello/wrong2.py b/tests/hello/wrong2.py index d7d5f18..2e35199 100644 --- a/tests/hello/wrong2.py +++ b/tests/hello/wrong2.py @@ -1,3 +1,3 @@ import time time.sleep(0.1) -print "hello" +print("hello") diff --git a/tests/test-runserver.py b/tests/test-runserver.py index cf4858b..df291dc 100755 --- a/tests/test-runserver.py +++ b/tests/test-runserver.py @@ -30,7 +30,7 @@ def upload(paths): Given a list of paths, upload them to the sandbox, and return an id that identifies the created directory. """ - files = dict( (os.path.basename(f)+unique, open(f)) for f in paths) + files = { os.path.basename(f)+unique: open(f) for f in paths} return upload_files(files) def upload_files(files): @@ -38,7 +38,7 @@ def upload_files(files): r = requests.post(endpoint, files=files) if r.status_code != requests.codes.ok: - log.error("Request error: {0}".format(r.text)) + log.error(f"Request error: {r.text}") return None if r.json is None: @@ -70,9 +70,9 @@ def main(args): global runserver global upload_server if len(args) < 4: - print "Usage: test-runserver.py http://x-server-to-upload-to:port/ http://x-server-to-run-on:port/ FILES cmd" - print "The first file in FILES will be randomized by appending a random string," - print "and the name of that file in 'cmd' will be modified the same way." + print("Usage: test-runserver.py http://x-server-to-upload-to:port/ http://x-server-to-run-on:port/ FILES cmd") + print("The first file in FILES will be randomized by appending a random string,") + print("and the name of that file in 'cmd' will be modified the same way.") sys.exit(1) upload_server = args[0] @@ -87,14 +87,14 @@ def main(args): start = time.time() id = upload(files) - print "Upload took %.03f sec" % (time.time() - start) + print("Upload took %.03f sec" % (time.time() - start)) start = time.time() cmd = cmd.replace(files[0], files[0]+unique) r = run(id, cmd) - print "run took %.03f sec" % (time.time() - start) + print("run took %.03f sec" % (time.time() - start)) if r is None: - print 'error' + print('error') if __name__=="__main__": main(sys.argv[1:]) diff --git a/tests/test.py b/tests/test.py index 849778c..1d6368b 100755 --- a/tests/test.py +++ b/tests/test.py @@ -32,10 +32,10 @@ def send(payload, answer): start = time.time() r = requests.post(xserver, data=json.dumps(data)) end = time.time() - print "Request took %.03f sec" % (end - start) + print("Request took %.03f sec" % (end - start)) if r.status_code != requests.codes.ok: - print "Request error" + print("Request error") #print "Text: ", r.text return r.text @@ -45,23 +45,22 @@ def check_output(data, verbose, expected_correct): try: d = json.loads(data) if d["correct"] != expected_correct: - print "ERROR: expected correct={0}. Message: {1}".format( - expected_correct, pprint.pformat(d)) + print("ERROR: expected correct={}. Message: {}".format( + expected_correct, pprint.pformat(d))) elif verbose: - print "Output: " + print("Output: ") pprint.pprint(d) except ValueError: - print "ERROR: invalid json %r" % data + print("ERROR: invalid json %r" % data) def globs(dirname, *patterns): """ Produce a sequence of all the files matching any of our patterns in dirname. """ for pat in patterns: - for fname in glob.glob(os.path.join(dirname, pat)): - yield fname + yield from glob.glob(os.path.join(dirname, pat)) def contents(fname): """ @@ -83,7 +82,7 @@ def check(dirname, verbose): #print "No payload.json or grade*.py in {0}".format(dirname) return if len(graders) > 1: - print "More than one grader in {0}".format(dirname) + print(f"More than one grader in {dirname}") return # strip off everything up to and including graders/ @@ -91,21 +90,21 @@ def check(dirname, verbose): index = p.find('graders/') if index < 0: # - print ("{0} is not in the 6.00x graders dir, and there's no payload.json file" + print("{} is not in the 6.00x graders dir, and there's no payload.json file" ", so we don't know how to grade it".format(p)) return else: grader_path = p[index + len('graders/'):] - print 'grader_path: ' + grader_path + print('grader_path: ' + grader_path) payload = json.dumps({'grader': grader_path}) for name in globs(dirname, 'answer*.py', 'right*.py'): - print "Checking correct response from {0}".format(name) + print(f"Checking correct response from {name}") answer = contents(name) check_output(send(payload, answer), verbose, expected_correct=True) for name in globs(dirname, 'wrong*.py'): - print "Checking wrong response from {0}".format(name) + print(f"Checking wrong response from {name}") answer = contents(name) check_output(send(payload, answer), verbose, expected_correct=False)