diff --git a/src/snipper/__pycache__/snip_dir.cpython-38.pyc b/src/snipper/__pycache__/snip_dir.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..90c94e065160551d01fbfaf907b3f93611e62869 Binary files /dev/null and b/src/snipper/__pycache__/snip_dir.cpython-38.pyc differ diff --git a/src/snipper/__pycache__/snipper.cpython-38.pyc b/src/snipper/__pycache__/snipper.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..76ee5eb7cb4c254035e4eadec59eb0389b6faf46 Binary files /dev/null and b/src/snipper/__pycache__/snipper.cpython-38.pyc differ diff --git a/src/snipper/snip_dir.py b/src/snipper/snip_dir.py index ce188b22dbd278edc3fb0a27c082c573ed883c91..4d2ff272499fe11d18c3e2dbeed1c39be68e93f2 100644 --- a/src/snipper/snip_dir.py +++ b/src/snipper/snip_dir.py @@ -1,7 +1,7 @@ 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 os.path.isfile(rm_file): - os.remove(rm_file) - else: - if os.path.isdir(rm_file + "\\"): - shutil.rmtree(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 diff --git a/src/snipper/snipper.py b/src/snipper/snipper.py index 77f330f055662520674bf3c6c41eda43bcbc7aab..1c0a3d3a3db5600c0f28f7ebafe669e1a2110ffc 100644 --- a/src/snipper/snipper.py +++ b/src/snipper/snipper.py @@ -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("")