Commit c14fa558 authored by Alan Marchiori's avatar Alan Marchiori
Browse files

check updates and remove pyc

parent 7808ba0c
......@@ -3,3 +3,4 @@ venv
lt.c
lt
__pycache__/
*.py[cod]
import click
import config
from config.echo import *
import courses
from config import UserConfig
from pprint import pprint
import os
def dbg(x):
debug(__name__ + x)
class Checker:
def __init__(self, checkinfo, labroot):
self.info = checkinfo
self.cwd = labroot
def exists(self, args):
"file existence check, args should be a list"
dbg('.exists({})'.format(args))
return all(map(os.path.exists, args))
def is_empty(self, args):
"true if all files in args (list) exist and have size 0 bytes"
dbg('.is_empty({})'.format(args))
if self.exists(args):
return all(map(lambda x: os.stat(x).st_size == 0, args))
else:
return False
def cd_labroot(self):
if os.path.exists(self.cwd):
debug("{}: {}".format(__name__,
"cd into {}".format(self.cwd)))
os.chdir(self.cwd)
return True
else:
error("The lab should be in the folder {}, but this path doesn't exist!".format(self.cwd))
return False
def do_check(self):
"""Does the actual check defined in the dict/json object
The cwd is set to labroot (and restored on exit)
Returns True if all tests pass
Returns False if any test fails
"""
incwd = os.getcwd()
try:
if self.cd_labroot():
r = []
for k, args in self.info.items():
if k in ['index', 'name', 'check', 'prompt', 'points',
'on_error', 'on_pass']:
continue
debug(k, args)
if hasattr(self, k):
r += [getattr(self, k)(args)]
else:
warn("I don't know what {} means.".format(
k
))
return all(r)
else:
return False
finally:
os.chdir(incwd)
@click.command(short_help="Check LAB_NUMBER (e.g. \"lt check lab01\")")
@click.argument('lab_number')
......@@ -30,10 +94,19 @@ def check(lab_number):
for part, info in rubric['parts'].items():
if info['check']:
c = Checker(info, os.path.join(coursepath, rubric['path']))
if do_check(coursepath, info):
success(info['name'])
echo("-"*40)
echo(info['name'], info['prompt'])
if c.do_check():
if 'on_pass' in info:
success(info['on_pass'])
else:
success('This part passed!')
else:
error(info['name'])
if 'on_error' in info:
error(info['on_error'])
else:
error('This part failed!')
#pprint(rubric)
......@@ -8,12 +8,14 @@ from config.user import UserConfig
def debug(*args, **kwargs):
if UserConfig.DEBUG:
click.echo(*args, **kwargs)
click.echo(
click.style("⚐ ", bold=True) + \
" ".join(map(str, args)), **kwargs)
def echo(*args, **kwargs):
return click.echo(
#\uf0d0 = magic
click.style("\uf0d0 ", bold=True) + \
click.style(" ", bold=True) + \
" ".join(map(str, args)), **kwargs)
def confirm(*args, **kwargs):
return click.confirm(
......
......@@ -34,7 +34,8 @@ class UserConfig:
def add_string(self, name, value):
"add a named value to the config"
self.cfg[name] = value
def debug(self):
return 'debug' in [x.lower() for x in UserConfig.cfg.keys()]
def __enter__(self):
"Load User config on enter"
if UserConfig.__instance == None:
......@@ -45,7 +46,7 @@ class UserConfig:
UserConfig.cfg_json = cf.read()
UserConfig.cfg = json.loads(UserConfig.cfg_json)
if 'debug' in [x.lower() for x in UserConfig.cfg.keys()]:
if self.debug():
UserConfig.DEBUG = True
print("USERCONFIG: READ")
......@@ -72,11 +73,11 @@ class UserConfig:
sort_keys=True
)
if userstr == UserConfig.cfg_json:
if 'debug' in UserConfig.cfg:
if UserConfig.DEBUG:
print("USERCONFIG: CLEAN")
return
if 'debug' in UserConfig.cfg:
if UserConfig.DEBUG:
print("USERCONFIG: WRITE")
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, 'w') as cf:
......
......@@ -30,6 +30,11 @@ def load_json(filepath):
def load_rubric(coursename, labname):
"return a rubric doc or none"
debug("Looking for rubric for {}.{}".format(coursename, labname))
if coursename not in all:
error("Course ({}) not properly configured in courses file.".format(
coursename
))
return None
for pathname in all[coursename]['rubric_paths']:
tpath = os.path.join(pathname, labname + ".json")
if os.path.exists(tpath):
......@@ -53,6 +58,9 @@ def load_courses(searchpath):
for coursename, coursefile in courses.items():
c[coursename] = load_json(
os.path.join(pathname, coursefile))
if len(c)==0:
error("No courses file found in the course search path!")
raise Exception("No courses file found in the course search path!")
return c
# places to search for coures.json, all courses are loaded
......@@ -61,6 +69,7 @@ course_paths = ['/home/accounts/COURSES/cs206',
'/unixspace/csci206/']
# dictionary of all course infos
# TODO this slows startup and may not be needed, move to lazy loading.
all = load_courses(course_paths)
# define valid course strings and course definition search paths
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment