diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2022-05-21 22:38:52 +0100 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2022-05-21 22:38:52 +0100 |
commit | f2f734194c03dfff2024cf417c502515ddb7a855 (patch) | |
tree | 745910a6206be1243187523bef355849dda0da77 /API/helpers.py | |
parent | abc7f067ff20bc2bd07d9236c30055549481547c (diff) | |
download | Smarker-f2f734194c03dfff2024cf417c502515ddb7a855.tar.gz Smarker-f2f734194c03dfff2024cf417c502515ddb7a855.zip |
Added running as an API
Diffstat (limited to 'API/helpers.py')
-rw-r--r-- | API/helpers.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/API/helpers.py b/API/helpers.py new file mode 100644 index 0000000..692e566 --- /dev/null +++ b/API/helpers.py @@ -0,0 +1,40 @@ +import tempfile +import docker +import json +import os + +CLIENT = docker.from_env() + +def run_smarker_simple(db, zip_name, assessment_name, volumes): + with tempfile.TemporaryDirectory() as td: # remember to passthru /tmp/ as a volume + env = [ # probably need to find a better way tbh + "submission=/tmp/%s" % zip_name, + "assessment=%s" % assessment_name, + "format=json", + "output=/out/report.json" + ] + outjson = os.path.join(td, "report.json") + volumes.append("%s:/out/report.json" % (outjson)) + # print("file_deps:", volumes) + + try: + pypideps = db.get_assessment_yaml(assessment_name)["dependencies"]["libraries"] + env.append("SMARKERDEPS=" + ",".join(pypideps)) + except KeyError: + pass + # print("env: ", env) + + open(outjson, 'a').close() # make a blank file so docker doesnt make it as a directory + log = CLIENT.containers.run( + "smarker", + remove = True, + volumes = volumes, + environment = env + ) + print("log: ", log) + + for f in os.listdir(".uploads"): + os.remove(os.path.join(".uploads", f)) + + with open(outjson, "r") as f: + return json.load(f) |