From 6aca17e9d44ba53f5504fb7f0384797b47d49655 Mon Sep 17 00:00:00 2001 From: Chaskiel Grundman Date: Thu, 11 Feb 2016 09:58:54 -0500 Subject: [PATCH] Move autograde_wrapper guts into a main() function This cleans up some warnings --- autodriver/autograde_wrapper.py | 82 +++++++++++++++++---------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/autodriver/autograde_wrapper.py b/autodriver/autograde_wrapper.py index d62ac6cd..5e45fdbf 100644 --- a/autodriver/autograde_wrapper.py +++ b/autodriver/autograde_wrapper.py @@ -20,46 +20,50 @@ def __call__(self): while np is None or np != self.waitfor: (np, self.status)=os.wait() except OSError: - if pid: + if np: print("Chld process {} never exited, but no more children left".format(self.waitfor)) self.status=-1 -for f in os.listdir("mount"): - src=os.path.join("mount", f) - dst=os.path.join("autolab", f) - shutil.copy(src, dst) +def main(): + for f in os.listdir("mount"): + src=os.path.join("mount", f) + dst=os.path.join("autolab", f) + shutil.copy(src, dst) -autolabuser=pwd.getpwnam("autolab") -(r_p, w_p)=os.pipe() -pid=os.fork() -if pid == 0: - os.close(r_p) - os.setgroups([]) - os.setgid(autolabuser.pw_gid) - os.setuid(autolabuser.pw_uid) - args=["autodriver"] - args.extend(sys.argv[1:]) - args.append("autolab") - if w_p != 1: - os.dup2(w_p, 1) - if w_p != 2: - os.dup2(w_p, 2) - if w_p > 2: - os.close(w_p) - os.execvp(args[0], args) -os.close(w_p) -waiter=WaitLoop(pid) -thr=threading.Thread(target=waiter) -thr.start() -rpf=os.fdopen(r_p) -shutil.copyfileobj(rpf, open("mount/feedback", "w")) -#print("Copied output") -rpf.close() -thr.join() -# if core, exit -1, else pass through code. -if os.WIFSIGNALED(waiter.status): - status=-1 -else: - status=os.WEXITSTATUS(waiter.status) -#print("Status is {}".format(status)) -sys.exit(status) + autolabuser=pwd.getpwnam("autolab") + (r_p, w_p)=os.pipe() + pid=os.fork() + if pid == 0: + os.close(r_p) + os.setgroups([]) + os.setgid(autolabuser.pw_gid) + os.setuid(autolabuser.pw_uid) + args=["autodriver"] + args.extend(sys.argv[1:]) + args.append("autolab") + if w_p != 1: + os.dup2(w_p, 1) + if w_p != 2: + os.dup2(w_p, 2) + if w_p > 2: + os.close(w_p) + os.execvp(args[0], args) + os.close(w_p) + waiter=WaitLoop(pid) + thr=threading.Thread(target=waiter) + thr.start() + rpf=os.fdopen(r_p) + shutil.copyfileobj(rpf, open("mount/feedback", "w")) + #print("Copied output") + rpf.close() + thr.join() + # if core, exit -1, else pass through code. + if os.WIFSIGNALED(waiter.status): + status=-1 + else: + status=os.WEXITSTATUS(waiter.status) + #print("Status is {}".format(status)) + sys.exit(status) + +if __name__ == '__main__': + main()