#!/usr/bin/env python import sys, os, re, shutil from stat import * mylib = os.path.join(os.environ['HOME'], 'lib', 'python') grlib = os.path.join(os.environ['HOME'], 'lib', 'grading') sys.path.extend([mylib, grlib]) from Util import * from course.Hwdirs import * def usage(): usage_str = 'Usage: '+sys.argv[0]+""" You must be in the source directory ~/coursenumber/hw/02 to execute this. Prepares the target directory e.g. /cs/course/coursenumber/current/homework/hw/02, hence called target. It copies various files and subdirectories from source to target: - The files 02.txt, to-grade, gr_start, GRADE-02 - For each part 1, 2, ..., say part 3, the subdirectories solution-3, testfiles-3 along with all their content (recursively). Sets the group ownerships and permissions in the target appropriately. Each directory below homework will be created if it does not exist. Below 02, if these targets exist, they will be removed first. """ sys.stderr.write(usage_str) sys.exit(1) 'main' if 1 != len(sys.argv): usage() cwd = os.getcwd() # cwd == '/home/fac0/gacs/cs113/hw/03' cwdlist = cwd.split(os.sep) if not ('hw' == cwdlist[5] and 7 == len(cwdlist)): die('You must be in the source directory \ ~//hw/.') course, hw = cwdlist[4], cwdlist[6] target = os.path.join(os.sep, 'cs', 'course', course, 'current', 'homework'); if not os.path.exists(target): die(target+' does not exist.') stat_return = os.lstat(target) gid = stat_return[ST_GID] target = os.path.join(target, 'hw') try: if not os.path.exists(target): print 'making', target group_mkdir(target, gid) target = os.path.join(target, hw) if not os.path.exists(target): print 'making', target group_mkdir(target, gid) except (IOError, OSError), detail: die('mkdir '+target, detail) for file in os.listdir('.'): if file in [hw+'.txt', 'gr_start', 'to-grade', 'GRADE-'+hw]: print file subsource = os.path.join('.', file) if not os.path.isfile(subsource): die(subsource+' is not a regular file') subtarget = os.path.join(target, file) if not group_regcopy(subsource, subtarget, gid): die('copy(%s, %s)' % (subsource, target)) elif re.match('^(solution-|testfiles-)', file): subsource = os.path.join('.', file) if not os.path.isdir(subsource): die(subsource+' must be a directory') subtarget = os.path.join(target, file) 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) if not os.path.exists(os.path.join('.', 'gr_start')): subsource = os.path.join(os.environ['HOME'], 'lib', 'grading', 'scripts', 'gr_start') if not os.path.isfile(subsource): die(subsource+' is not a regular file') subtarget = os.path.join(target, 'gr_start') if not group_regcopy(subsource, subtarget, gid): die('copy(%s, %s)' % (subsource, target))