Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.66 KB | None | 0 0
  1. #!/bin/python
  2. """Tests the searchshell output of everyone's results."""
  3. from os import listdir, chdir as cd, getcwd as pwd, umask
  4. from subprocess import check_output as sh
  5.  
  6. # Set group permissions to be the same as user
  7. umask(007)
  8.  
  9. # Setup the grading directory and constants
  10. GRADINGDIR = "/cse/courses/cse333/17au/grading"
  11. SCRIPTDIR = "{}/script/hw2".format(GRADINGDIR)
  12.  
  13. PROMPT = "enter query:"
  14. PRE = "Indexing './test_tree'"
  15. POST = "shutting down..."
  16.  
  17. # cd into the hw directory
  18. cd('{}/hw/hw2/'.format(GRADINGDIR))
  19. dirs = sh('ls -d */', shell=True)
  20.  
  21. queries = []
  22.  
  23.  
  24. def genQDict(lines, expect_query_line=True):
  25.     query_results = {}
  26.     curr_query = None
  27.     query_num = 0
  28.     i = 0
  29.     while i != len(lines):
  30.         l = lines[i].strip()
  31.  
  32.         if l == PROMPT:
  33.             if i == len(lines) - 1 or i == len(lines) - 2 and \
  34.                 lines[-1].startswith('shutting'):
  35.                 break
  36.             elif expect_query_line:
  37.                 i += 1
  38.                 curr_query = lines[i].strip()
  39.                 assert '.' not in curr_query, lines[i - 1]
  40.                 queries.append(curr_query)
  41.             else:
  42.                 # We have the query cached
  43.                 curr_query = queries[query_num]
  44.                 assert '.' not in curr_query
  45.                 query_num += 1
  46.             query_results[curr_query] = []
  47.         else:
  48.             query_results[curr_query].append(l)
  49.         i += 1
  50.     return query_results
  51.  
  52.  
  53. def strip_output(output):
  54.     """Strips the top and end of the output, checking for them."""
  55.     if len(output) == 0:
  56.         print "  MISSING: everything"
  57.     else:
  58.         if output[0] == PRE:
  59.             output = output[1:]
  60.         else:
  61.             if output[0].startswith("Indexing"):
  62.                 output = output[1:]
  63.                 print "  MISSPELLED: " + PRE
  64.             else:
  65.                 print "  MISSING: " + PRE
  66.  
  67.         if output[-1] == POST:
  68.             output = output[:-1]
  69.         else:
  70.             if output[-1].startswith("shutting"):
  71.                 output = output[:-1]
  72.                 print "  MISSPELLED: " + POST
  73.             else:
  74.                 print "  MISSING: " + POST
  75.  
  76.         if output[-1] == PROMPT:
  77.             output = output[:-1]
  78.     return output
  79.  
  80.  
  81. with open('{}/hw2.expected'.format(SCRIPTDIR)) as f:
  82.     expected = genQDict(strip_output(f.readlines()))
  83.  
  84. skipTo = None  #'chenqy/'
  85. reached = False
  86.  
  87. for id in dirs.split():
  88.     if skipTo and not reached and id != skipTo:
  89.         continue
  90.     elif id == skipTo:
  91.         reached = True
  92.  
  93.     print "\n=== GRADING %s ===" % id
  94.     cd('{}/hw/hw2/{}/hw2'.format(GRADINGDIR, id))
  95.  
  96.     try:
  97.         output = sh(
  98.             'timeout 120s ./searchshell ./test_tree < {}/hw2.queries'.format(
  99.                 SCRIPTDIR),
  100.             shell=True)
  101.  
  102.         with open('../shell.output', 'w+') as f:
  103.             f.write(output)
  104.         output = [s.strip() for s in output.split('\n') if len(s.strip())]
  105.         output = strip_output(output)
  106.         count = output.count(PROMPT)
  107.         if count == 0:
  108.             print "  SKIPPING: Did not handle queries"
  109.             continue
  110.         elif count != len(queries):
  111.             print "  SKIPPING: Expected %s queries, Found %s" % (len(queries),
  112.                                                                  count)
  113.             print "  See %s/shell.output" % id
  114.             continue
  115.  
  116.     except Exception as e:
  117.         print '  EXCEPTION:', e
  118.         continue
  119.  
  120.     # Make sure all the queries are the same
  121.     try:
  122.         actual = genQDict(output, expect_query_line=False)
  123.         for q in expected:
  124.             if len(expected[q]) > len(actual[q]):
  125.                 print("  Actual had less results than expected for query: %s" %
  126.                       q)
  127.             elif len(expected[q]) < len(actual[q]):
  128.                 print("  Actual had more results than expected for query: %s" %
  129.                       q)
  130.             else:
  131.                 # Make sure the results are the same
  132.                 for res in expected[q]:
  133.                     if res not in actual[q]:
  134.                         print("  Query \"%s\" MISSING %s" % (q, res))
  135.                         break
  136.             # Make sure the order was monotonically decreasing
  137.             prev = None
  138.             for res in actual[q]:
  139.                 if prev:
  140.                     prevrank = prev[prev.rfind('(') + 1:prev.rfind(')')]
  141.                     rank = res[res.rfind('(') + 1:res.rfind(')')]
  142.                     if int(prevrank) < int(rank):
  143.                         print("  Misaligned order for query: %s" % q)
  144.  
  145.                 prev = res
  146.     except Exception as e:
  147.         print '  EXCEPTION:', e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement