Skip to content

Instantly share code, notes, and snippets.

@kulttuuri
Last active November 24, 2022 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulttuuri/547b0758a92cb55993a7c8e89ca460c3 to your computer and use it in GitHub Desktop.
Save kulttuuri/547b0758a92cb55993a7c8e89ca460c3 to your computer and use it in GitHub Desktop.
import subprocess, os, sys
from os.path import exists
__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""
SEPARATOR = "#<ab@17943918#@>#"
# Wrap all things inside try / catch to make sure that token etc does not leak out
# Authentication to Git
# NOTE: This part is only necessary if you want to support pulling private repositories, like in GitHub.
# If not, then you can remove this part.
'''process = subprocess.call(
[
'git config --global user.email "githubEmail"',
'git config --global user.name "githubUsername"'
],
shell = True,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)'''
try:
# Try replacing the student_answer https:// part with https://token@github.com...
# Note: this part is also only necessary should you want to support private repositories
__student_answer__ = __student_answer__.replace("https://", "https://yourGithubToken@")
except:
sys.exit("Something went wrong while trying to clone the repository. Check that the URL is correct.")
# Clone the repository
try:
process = subprocess.call(["git clone " + __student_answer__ + " repo"],
shell = True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
except:
sys.exit("Repository was not cloned succesfully. Did you provide a correct git url and it can be accessed by Coderunner?")
# TODO: Somehow check how did this fail and provide clear error message. No authentication, link not found... ?
# For example, can we check that user has not shared the repository to the correct user?
if os.path.isdir('repo') == False:
sys.exit("Repository was not cloned succesfully. Did you provide a correct git url and it can be accessed by Coderunner?")
#os.chdir('repo')
#sys.path.append('repo')
# HELPER FUNCTIONS
mainPath = "repo/"
def fileShouldExist(filePath: str, dieOnFail: bool = True) -> None:
filePathFull = mainPath + filePath
if exists(filePathFull): return True
else:
if dieOnFail: sys.exit(f"File {filePath} does not exist.")
else: return False
def fileShouldContainLine(filePath: str, line: str, dieOnFail: bool = True) -> None:
filePathFull = mainPath + filePath
file = open(filePathFull, 'r')
lines = file.readlines()
file.close()
found = False
for goLine in lines:
if line in goLine:
found = True
if found == True: return True
else:
if dieOnFail:
sys.exit(f"Line {line} was not found in {filePath}")
else:
return False
{% for TEST in TESTCASES %}
{{ TEST.testcode }}
{% if not loop.last %}
print(SEPARATOR)
{% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment