Advertisement
Vermiculus

Grading.py

Sep 21st, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. print
  2. print "Begin"
  3.  
  4. import subprocess
  5. import os
  6.  
  7. def check(needle, haystack):
  8.     if not needle:
  9.         return True
  10.     try:
  11.         offset = haystack.index(needle[0])
  12.         return check(needle[1:], haystack[offset+1:])
  13.     except:
  14.         return False
  15.  
  16. def contains_sublist(lst, sublst):
  17.     n = len(sublst)
  18.     return any((sublst == lst[i:i+n]) for i in xrange(len(lst)-n+1))
  19.  
  20. def isMain(path):
  21.     """Takes path and searches for the main method"""
  22.     with open(path, 'r') as f:
  23.         lines = f.readlines()
  24.         for line in lines:
  25.             if contains_sublist(line.split(), "public static void main(String[]".split()):
  26.                 return True
  27.     return False
  28.  
  29. def runjava(path):
  30.     print path
  31.     print "javac", path
  32.     subprocess.call(["javac", path])
  33.     print "java", path[:-5]
  34.     subprocess.call(["java", path[:-5]])
  35.  
  36. for d in os.walk('.').next()[1]:
  37.     print "*.java files from ./{}:".format(d)
  38.     javafiles = filter(lambda x: x.endswith(".java"), os.listdir("./" + d))
  39.     for f in javafiles:
  40.         print "\t", f[:-5]
  41.         fullpath = "./" + d + "/" + f
  42.         if isMain(fullpath):
  43.             print "\t\tMain found!"
  44.             runjava(fullpath)
  45.     print
  46.  
  47. # clean up and delete class files
  48. for dirname, dirnames, filenames in os.walk('.'):
  49.     for filename in filenames:
  50.         if filename.endswith(".class"):
  51.             os.remove(os.path.join(dirname, filename))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement