Reputation: 31
I'm writing Hacker Rank style, offline applications for a class at Temple University. I have a task.py, which is where the user should write their own code. I have a test.py where I have created a module to test the user's code.
I'm using Thonny, because it requires very low resources, which is assumed to be if the student doesn't have internet at home, for whom the code is being written.
Here is an example for task.py
import sys
def variables():
f = open('test.txt', 'w')
#Start your code below (tip: Make sure to indent your code)
penguin = "Penguin"
six = "6"
false = "False"
none = "None"
a = "100.66"
f.write(penguin+"\n")
f.write(six+"\n")
f.write(false+"\n")
f.write(none+"\n")
f.write(a+"\n")
print(penguin)
print(6)
print(false)
print(none)
print(a)
f.close()
Here is my testing file, test.py
from task import variables
import filecmp, os
variables()
a=filecmp.cmp("test.txt","ans.txt")
if a == True:
print("Test Case Passed")
os.remove("test.txt")`
I need the test.py file to be locked down so the student can't modify it, but I need it to be able to be called. Any ideas?
Upvotes: 3
Views: 1349
Reputation: 592
It has nothing to do with Python. And I don't think you'll be able to achieve it. Even if you lock changes in test.py (somehow), you have to set it readable (so it will be able to run). But if you do so, student can Ctrl+C and Ctrl+V code of test.py, save to new file and modify.
Python is interpreted language, so you can't compile it completely (like in C). You can compile it to bytecode (.pyc files), but they can be decompiled, so it's not the solution.
Btw. If it's offline and doesn't save or send results anywhere, making this file unchangeable is pointless.
EDIT: If you want simply to prevent task.py to modify test.py, you can make bash (or cmd - whatever) script: first run task.py and then run test.py (but as separate python processes).
Upvotes: 0
Reputation: 1576
A problem is, even if you lock the test file down and even if you lock the .txt document down, the result can still be manipulated.
E. g. in my variables submission I could do something like this:
import filecmp
filecmp.cmp = lambda x, y: True
Adding this anywhere in the variables function, your test file would always pass. You probably want to completely isolate the student code when you execute it. Maybe you can use something like CodeJail to create a sandbox to execute student code and then separately compare the result.
Upvotes: 0
Reputation: 779
There is a module for cross-platform file locking called portalocker (https://pypi.python.org/pypi/portalocker), but if you don't want to use that you can check for something else. Using the os function
last_mod = os.path.getmtime('test.txt')
will give you the last time the file was modified, if the last time is different then when it was created then someone edited it. This is just an alternative to portalocker... Just keep a copy of the og file somewhere and if the modification time is different just have python write the copy in over the modified file. I strongly recommend just using the module for this one because there's a lot more cool functions that you can use from it. If you have any further questions just comment them.
Upvotes: 1