#!/usr/local/bin/python import sys, os, shutil, getpass from stat import * # home = os.environ['HOME'] home = os.path.join(os.sep, 'home', 'fac', 'gacs') mylib = os.path.join(home, 'lib', 'python') grlib = os.path.join(home, 'lib', 'grading') sys.path.extend([mylib, grlib]) from Util import * from course.Hwdirs import * def usage(progname): usage_str = """Usage: If grading student with name jimmy and part 3 of the assignment, type """ + progname + """ --student jimmy --part 3 (default --part is 1) [--new-tests]. Prepares for grading a part of a student's homework. Assume that this is for assignment 02. You must be in /cs/course/coursenumber/current/homework/hw/02 to execute the present script. The part number tells which part of the homework is to be graded: assume it is part 3. This script empties the test directory testdir-, then it copies the necessary files of the student, and the testfiles of the grader into this directory. The names of student files to copy are the same as the names of files in directory solution-3, and the test files are in directory testfiles-3. Both of these may have a directory structure: in this case, this whole structure will be reproduced, since group_recursive_copy is used. (Note that group_recursive_copy copies symbolic links verbatim, and preserves the hard-link structure.) If the option --new-tests is there, the test directories are recopied from the instructor's private directory. This option works only for the instructor. """ sys.stderr.write(usage_str) sys.exit(1) "main" if 1 == len(sys.argv): usage(sys.argv[0]) long_optlist = ["student=", "part=", "new-tests"] options, args = getopt_map("x", long_optlist) if not options.has_key('--student'): usage(progname) student = options['--student'] if options.has_key('--part'): part = options['--part'] else: part = "1" # Must be a string. cwd = os.getcwd() # cwd == "/cs/course/cs113/current/homework/hw/03" cwdlist = cwd.split(os.sep) course = cwdlist[3] if not 8 == len(cwdlist): die("You are in the wrong directory.") hw = cwdlist[7] print "Creating the grader work directory..." grader = getpass.getuser() testdir = "testdir-" + grader try: if os.path.exists(testdir): shutil.rmtree(testdir) stat_return = os.lstat(".") gid = stat_return[ST_GID] group_mkdir(testdir, gid) except (OSError, IOError), detail: die("Recreating testdir failed.", detail) print "Linking the work directory to name 'sandbox'..." if os.path.exists('sandbox'): os.remove('sandbox') os.symlink(testdir, 'sandbox') print "Copying the student solution files..." student_dir = os.path.join(os.sep, "cs", "course", course, "current", "homework", "spool", student); if not os.path.exists(student_dir): die("There is no directory " + student_dir) student_hw_dir = os.path.join(student_dir, hw) if not os.path.exists(student_dir): die(student + "has no assignment directory " + hw) for file in os.listdir("solution-" + part): student_solution_file = os.path.join(student_hw_dir, file) if os.path.exists(student_solution_file): group_recursive_copy(student_solution_file, testdir, gid) "Possibly recreate the test files:" if options.has_key('--new-tests'): print "\nRecreating the test files..." instructor = file_owner(".") subsource = os.path.join(os.sep, 'home', 'fac', instructor, course, 'hw', hw, 'testfiles-'+part) if not os.path.isdir(subsource): die(subsource+" must be a directory") subtarget = 'testfiles-'+part try: if os.path.exists(subtarget): shutil.rmtree(subtarget) except (OSError, IOError), detail: die("rmtree("+subtarget+")", detail) try: group_recursive_copy(subsource, subtarget, gid) except (OSError, IOError), detail: die("copy("+subsource+", "+subtarget+")", detail) print "\nCopying the test files into the work directory..." testfile_dir = "testfiles-" + part "There must be at least an empty testfiles-... directory:" if not os.path.exists(testfile_dir): die("testfiles-"+part+" does not exist") for file in os.listdir(testfile_dir): group_recursive_copy(os.path.join(testfile_dir, file), testdir, gid) print "\nCopying possible makefile..." if os.path.exists("Makefile"): try: smart_link("Makefile", testdir) except OSError, detail: warn("Cannot link Makefile ", detail)