#!/usr/bin/env python import sys, os sys.path.append('/home/fac/gacs/lib/python') from Util import * from stat import * import re def usage(progname): usage_str = \ " Usage: " + progname + """ --course --file [--subdir --students , ... ] (students separeted by commas, no space). Remove a file from (a subdirectory of) the homework submission directory of each student. The subdirectory name can contain slashes. If the argument --students is present, the removing happens only in the directories of these students. """ sys.stderr.write(usage_str) sys.exit(1) def rm_from_dir(subm_dir, subdir, filename): if not os.path.isdir(subm_dir): sys.stderr.write(subm_dir + " is not a directory.\n") return if subdir: target = os.path.join(subm_dir, subdir) else: target = subm_dir if not os.path.isdir(target): target = subm_dir target = os.path.join(target, os.path.basename(filename)) try: if os.path.exists(target): os.remove(target) except (OSError, IOError), detail: die("Removing "+target+" failed.", detail) # --------------------- Main ---------------------- progname = sys.argv[0] long_optlist = ["course=", "file=", "subdir=", "students="] options, args = getopt_map("x", long_optlist) if not (options.has_key('--course') and options.has_key('--file')): usage(progname) course, filename = options['--course'], options['--file'] if not os.path.exists(filename): sys.stderr.write("File " + filename + " does not exist.\n") sys.exit(1) if options.has_key('--subdir'): subdir = options['--subdir'] else: subdir = "" spool = os.path.join(os.sep, "cs", "course", course, "current", "homework", "spool") if options.has_key("--students"): students = options['--students'].split(",") else: students = os.listdir(spool) for student in students: rm_from_dir(os.path.join(spool, student), subdir, filename)