Skip to content
Snippets Groups Projects
Commit d90ee361 authored by tuhe's avatar tuhe
Browse files

Updates

parent 3178d175
No related branches found
No related tags found
No related merge requests found
File added
File added
import os, shutil
# from thtools.coursebox.core.info_paths import get_paths
# from thtools.coursebox.core.info import class_information
from thtools.coursebox.material.snipper import censor_file
from snipper.snipper import censor_file
# from thtools.coursebox.material.homepage_lectures_exercises import fix_shared
from pathlib import Path
import time
......@@ -33,38 +33,65 @@ def snip_dir(source_dir, dest_dir, exclude=None, clean_destination_dir=True):
shutil.copytree(base, out)
time.sleep(0.2)
ls = Path(out).glob('**/*.py')
ls = list(Path(out).glob('**/*.*'))
acceptable = []
import fnmatch
for l in ls:
split = os.path.normpath(os.path.relpath(l, out))
m = [fnmatch.fnmatch(split, ex) for ex in exclude]
acceptable.append( (l, not any(m) ))
# print(acceptable)
# now we have acceptable files in list.
run_out_dirs = ["./output"]
n = 0
edirs = [os.path.join(out, f_) for f_ in hw['exclusion']] # Exclude directories on exclude list (speed)
edirs = {os.path.normpath(os.path.dirname(f_) if not os.path.isdir(f_) else f_) for f_ in edirs}
edirs.remove(os.path.normpath(out))
for f in ls:
# edirs = [os.path.join(out, f_) for f_ in hw['exclusion']] # Exclude directories on exclude list (speed)
# edirs = {os.path.normpath(os.path.dirname(f_) if not os.path.isdir(f_) else f_) for f_ in edirs}
# edirs.remove(os.path.normpath(out))
for f, accept in acceptable:
if os.path.isdir(f) or not str(f).endswith(".py"): # We only touch .py files.
continue
f_dir = os.path.normpath(f if os.path.isdir(f) else os.path.dirname(f))
if not any([f_dir.startswith(f_) for f_ in edirs]):
slist = hw['solutions'] if not CE else [os.path.basename(f)[:-3]]
base = None
if students_irlc_tools is not None:
base = os.path.relpath(str(f), students_irlc_tools + "/..")
base = base.replace("\\", "/")
if accept:
# slist = hw['solutions'] if not CE else [os.path.basename(f)[:-3]]
# base = None
# if students_irlc_tools is not None:
# base = os.path.relpath(str(f), students_irlc_tools + "/..")
# base = base.replace("\\", "/")
if "assignments" in str(f) and "_grade.py" in str(f):
continue
nrem = censor_file(f, info, paths, run_files=run_files, run_out_dirs=run_out_dirs[:1], cut_files=cut_files, solution_list=slist, include_path_base=base, **kwargs)
# if "assignments" in str(f) and "_grade.py" in str(f):
# continue
info = {'new_references': [], 'code_copyright': 'Example student code. This file is automatically generated from the files in the instructor-directory'}
paths = {}
solution_list = []
kwargs = {}
cut_files = True
run_files = True
nrem = censor_file(f, info, paths, run_files=run_files, run_out_dirs=run_out_dirs[:1], cut_files=cut_files, solution_list=solution_list, include_path_base=base, **kwargs)
if nrem > 0:
print(f"{nrem}> {f}")
n += nrem
for f in hw['exclusion']:
rm_file = os.path.join(out, f)
if any([df_ in rm_file for df_ in hw['inclusion']]):
continue
for rm_file, accept in acceptable:
# rm_file = l
rm_file = os.path.abspath(rm_file)
if not accept:
if os.path.isfile(rm_file):
os.remove(rm_file)
else:
if os.path.isdir(rm_file + "\\"):
shutil.rmtree(rm_file)
# for f in hw['exclusion']:
# rm_file = os.path.join(out, f)
# if any([df_ in rm_file for df_ in hw['inclusion']]):
# continue
#
# rm_file = os.path.abspath(rm_file)
# if os.path.isfile(rm_file):
# os.remove(rm_file)
# else:
# if os.path.isdir(rm_file + "\\"):
# shutil.rmtree(rm_file)
return n
......@@ -411,8 +411,8 @@ def censor_file(file, info, paths, run_files=True, run_out_dirs=None, cut_files=
if run_files or cut_files:
ofiles = []
for rod in run_out_dirs:
if not os.path.isdir(rod):
os.mkdir(rod)
# if not os.path.isdir(rod):
# os.mkdir(rod)
ofiles.append(os.path.join(rod, os.path.basename(file).split(".")[0]) )
ofiles[0] = ofiles[0].replace("\\", "/")
......@@ -435,21 +435,22 @@ def censor_file(file, info, paths, run_files=True, run_out_dirs=None, cut_files=
fname = file.__str__()
i = fname.find("irlc")
wk = fname[i+5:fname.find("\\", i+6)]
sp = paths['02450students'] +"/solutions/"
if not os.path.exists(sp):
os.mkdir(sp)
sp = sp + wk
if not os.path.exists(sp):
os.mkdir(sp)
# sp = paths['02450students'] +"/solutions/"
# if not os.path.exists(sp):
# os.mkdir(sp)
# sp = sp + wk
# if not os.path.exists(sp):
# os.mkdir(sp)
sols = []
stext = ["\n".join(lines) for lines in cut]
for i,sol in enumerate(stext):
sout = sp + f"/{os.path.basename(fname)[:-3]}_TODO_{i+1}.py"
wsol = any([True for s in solution_list if os.path.basename(sout).startswith(s)])
print(sout, "(published)" if wsol else "")
if wsol:
with open(sout, "w") as f:
f.write(sol)
sols.append( (sol,) )
# sout = sp + f"/{os.path.basename(fname)[:-3]}_TODO_{i+1}.py"
# wsol = any([True for s in solution_list if os.path.basename(sout).startswith(s)])
# print(sout, "(published)" if wsol else "")
# if wsol:
# with open(sout, "w") as f:
# f.write(sol)
if len(lines[-1])>0:
lines.append("")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment