def manual_grader_all(submission_folder_path, filename): # path : path of one submission folder. eg. "submissions/" # filename : name of the submission. eg. "question2.txt" report = {} dirs = get_dir(submission_folder_path) dirs = os.listdir(submission_folder_path) dirs = sorted(dirs) try: for c,i in enumerate(dirs): clear_screen() print("---------", i, "----------") print(c+1,'/',len(dirs)) path = os.path.join(submission_folder_path, i) id = get_id_from_path(i) r = manual_grader_one(path, filename) report[id] = r; except Exception as e: print(e) print(report) finally: output_report(report_name, report, sensitivity_words) print("report created!")
def extract_submissions(sub_path): report = {} dirs = get_dir(sub_path) for i in dirs: path = os.path.join(sub_path, i) id = get_id_from_path(i) r = extract(path) report[id] = r output_report(report_name, report, sensitivity_words)
def run(): # os.chdir(submission_path) # get subdirectories dir = get_dir(submission_path) counter = 0 # run TB for every folder inside submission folder for i in dir: # start time t1 = time.time() # copy files for TB r = subprocess.run("cp " + tb + " " + os.path.join(submission_path, i), shell=True) # student canvas id student_id = get_id_from_path(i) d = defaultdict(str) print("-------------------" + "-------------------") os.chdir(os.path.join(submission_path, i)) print("current dir :", os.getcwd()) # running the process r = subprocess.Popen("./run.sh", stdout=PIPE, stderr=PIPE, shell=True) # live output out = "" for line in iter(r.stdout.readline, ''): if r.poll() is not None: break print(line.decode("utf-8"), end="") out += line.decode("utf-8") if parse(line.decode("utf-8")) is not None: key, value = parse(line.decode("utf-8")) if key in d: d["warning"] += "duplicate key *" + key + "*:" d[key] = value.strip() # finalized result if student_id in gradebook: print("Warning: duplicate ID") d["warning"] += "duplicate ID;" gradebook[student_id] = d # clean up os.chdir(submission_path) counter += 1 print(str(counter) + "/" + str(len(dir)) + " finished") # time for one iteration print(time.strftime("%H:%M:%S", time.localtime()), '\t\t,', time.time() - t1, "\n---------------------------------------------")
def extract_pdf(sub_dir, dst_dir): os.system("mkdir -p " + dst_dir) os.chdir(sub_dir) dir = get_dir(".") for i in dir: print(i) os.chdir(i) os.system("cp *.pdf " + dst_dir + "/" + i + ".pdf") # os.system("cp *.docx " + dst_dir + "/" + i + ".docx") os.chdir(sub_dir)
def add_dir(item: UpdateDir, q: str = Query(None)): safe_path = safe_path_join(path=q, root=os.environ["ROOT_DIR"]) dir_path = util.make_dir(safe_path, item.name) items = util.get_dir(dir_path) items_json_encoded = jsonable_encoder(items) resp = { "path": get_host_path(dir_path), "count": len(items), "items": items_json_encoded, } return JSONResponse(content=resp)
def delete_dir(item: UpdateDir, q: str = Query(None)): safe_path = safe_path_join(path=q, root=os.environ["ROOT_DIR"]) dir_path = os.path.join(safe_path, item.name) if not os.path.isdir(dir_path): raise HTTPException(status_code=404, detail="This directory doesn't exist.") items = util.get_dir(dir_path) items_json_encoded = jsonable_encoder(items) resp = { "path": get_host_path(dir_path), "count": len(items), "items": items_json_encoded, } util.remove_dir(dir_path) return JSONResponse(content=resp)
def test_non_empty_dir(self): self.mock_os_listdir.return_value = ["house", "tree.txt"] resp = [ { "name": "house", "owner": mock.ANY, "permissions": mock.ANY, "size": mock.ANY, }, { "name": "tree.txt", "owner": mock.ANY, "permissions": mock.ANY, "size": mock.ANY, }, ] self.assertEqual(util.get_dir(self.path), resp)
def folder_to_files(root=os.getcwd(), depth=0): if depth == 3: return False os.chdir(root) # change_non_std_char(root) os.system("rm -rf __MACOSX") os.system("rm -rf _MACOSX") # start next level dir = get_dir(".") if dir == []: return True for i in dir: os.chdir(i) os.system("rm -rf __MACOSX") os.system("rm -rf _MACOSX") os.system("mv * ../") os.chdir("..") os.system("rm -rf " + i) return folder_to_files(root, depth + 1)
def get_path_items(path: str): """ Get directory or file details at the given filepath """ if not os.path.exists(path): host_path = get_host_path(path) raise HTTPException( status_code=404, detail="The path `{}` does not exist or you don't have permissions to access it.".format( host_path ), ) _, file_extension = os.path.splitext(path) if file_extension and file_extension != ".txt": raise HTTPException(status_code=403, detail="Can only read .txt files") items = [] if os.path.isdir(path): items = util.get_dir(path) else: items = util.get_file(path) return items
change_non_std_char(root) os.system("rm -rf __MACOSX") os.system("rm -rf _MACOSX") # start next level dir = get_dir(".") if dir == []: return True for i in dir: os.chdir(i) os.system("rm -rf __MACOSX") os.system("rm -rf _MACOSX") os.system("mv * ../") os.chdir("..") os.system("rm -rf " + i) return folder_to_files(root, depth + 1) if __name__ == '__main__': # move files from sub-director to root directory # submission_path = "sub_tmp/" # path = os.path.join(os.getcwd(),submission_path) path = "/home/yb/112L_W21/lab4/submissions" os.chdir(path) change_non_std_char(path) dir = get_dir(".") for f in dir: print(f) if not folder_to_files(os.path.join(path, f)): print("ERROR: " + f)
def test_empty_dir(self): self.mock_os_listdir.return_value = [] self.assertEqual(util.get_dir(self.path), [])