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 import os, shutil
# from thtools.coursebox.core.info_paths import get_paths # from thtools.coursebox.core.info_paths import get_paths
# from thtools.coursebox.core.info import class_information # 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 thtools.coursebox.material.homepage_lectures_exercises import fix_shared
from pathlib import Path from pathlib import Path
import time import time
...@@ -33,38 +33,65 @@ def snip_dir(source_dir, dest_dir, exclude=None, clean_destination_dir=True): ...@@ -33,38 +33,65 @@ def snip_dir(source_dir, dest_dir, exclude=None, clean_destination_dir=True):
shutil.copytree(base, out) shutil.copytree(base, out)
time.sleep(0.2) 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"] run_out_dirs = ["./output"]
n = 0 n = 0
edirs = [os.path.join(out, f_) for f_ in hw['exclusion']] # Exclude directories on exclude list (speed) # 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 = {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)) # edirs.remove(os.path.normpath(out))
for f in ls: 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)) 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]): if accept:
slist = hw['solutions'] if not CE else [os.path.basename(f)[:-3]] # slist = hw['solutions'] if not CE else [os.path.basename(f)[:-3]]
base = None # base = None
if students_irlc_tools is not None: # if students_irlc_tools is not None:
base = os.path.relpath(str(f), students_irlc_tools + "/..") # base = os.path.relpath(str(f), students_irlc_tools + "/..")
base = base.replace("\\", "/") # base = base.replace("\\", "/")
if "assignments" in str(f) and "_grade.py" in str(f): # if "assignments" in str(f) and "_grade.py" in str(f):
continue # 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) 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: if nrem > 0:
print(f"{nrem}> {f}") print(f"{nrem}> {f}")
n += nrem n += nrem
for rm_file, accept in acceptable:
for f in hw['exclusion']: # rm_file = l
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) rm_file = os.path.abspath(rm_file)
if os.path.isfile(rm_file): if not accept:
os.remove(rm_file) if os.path.isfile(rm_file):
else: os.remove(rm_file)
if os.path.isdir(rm_file + "\\"): else:
shutil.rmtree(rm_file) 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 return n
...@@ -411,8 +411,8 @@ def censor_file(file, info, paths, run_files=True, run_out_dirs=None, cut_files= ...@@ -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: if run_files or cut_files:
ofiles = [] ofiles = []
for rod in run_out_dirs: for rod in run_out_dirs:
if not os.path.isdir(rod): # if not os.path.isdir(rod):
os.mkdir(rod) # os.mkdir(rod)
ofiles.append(os.path.join(rod, os.path.basename(file).split(".")[0]) ) ofiles.append(os.path.join(rod, os.path.basename(file).split(".")[0]) )
ofiles[0] = ofiles[0].replace("\\", "/") ofiles[0] = ofiles[0].replace("\\", "/")
...@@ -435,21 +435,22 @@ def censor_file(file, info, paths, run_files=True, run_out_dirs=None, cut_files= ...@@ -435,21 +435,22 @@ def censor_file(file, info, paths, run_files=True, run_out_dirs=None, cut_files=
fname = file.__str__() fname = file.__str__()
i = fname.find("irlc") i = fname.find("irlc")
wk = fname[i+5:fname.find("\\", i+6)] wk = fname[i+5:fname.find("\\", i+6)]
sp = paths['02450students'] +"/solutions/" # sp = paths['02450students'] +"/solutions/"
if not os.path.exists(sp): # if not os.path.exists(sp):
os.mkdir(sp) # os.mkdir(sp)
sp = sp + wk # sp = sp + wk
if not os.path.exists(sp): # if not os.path.exists(sp):
os.mkdir(sp) # os.mkdir(sp)
sols = []
stext = ["\n".join(lines) for lines in cut] stext = ["\n".join(lines) for lines in cut]
for i,sol in enumerate(stext): for i,sol in enumerate(stext):
sout = sp + f"/{os.path.basename(fname)[:-3]}_TODO_{i+1}.py" sols.append( (sol,) )
wsol = any([True for s in solution_list if os.path.basename(sout).startswith(s)]) # sout = sp + f"/{os.path.basename(fname)[:-3]}_TODO_{i+1}.py"
print(sout, "(published)" if wsol else "") # wsol = any([True for s in solution_list if os.path.basename(sout).startswith(s)])
if wsol: # print(sout, "(published)" if wsol else "")
with open(sout, "w") as f: # if wsol:
f.write(sol) # with open(sout, "w") as f:
# f.write(sol)
if len(lines[-1])>0: if len(lines[-1])>0:
lines.append("") lines.append("")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment