Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python
- """Tests the searchshell output of everyone's results."""
- from os import listdir, chdir as cd, getcwd as pwd, umask
- from subprocess import check_output as sh
- # Set group permissions to be the same as user
- umask(007)
- # Setup the grading directory and constants
- GRADINGDIR = "/cse/courses/cse333/17au/grading"
- SCRIPTDIR = "{}/script/hw2".format(GRADINGDIR)
- PROMPT = "enter query:"
- PRE = "Indexing './test_tree'"
- POST = "shutting down..."
- # cd into the hw directory
- cd('{}/hw/hw2/'.format(GRADINGDIR))
- dirs = sh('ls -d */', shell=True)
- queries = []
- def genQDict(lines, expect_query_line=True):
- query_results = {}
- curr_query = None
- query_num = 0
- i = 0
- while i != len(lines):
- l = lines[i].strip()
- if l == PROMPT:
- if i == len(lines) - 1 or i == len(lines) - 2 and \
- lines[-1].startswith('shutting'):
- break
- elif expect_query_line:
- i += 1
- curr_query = lines[i].strip()
- assert '.' not in curr_query, lines[i - 1]
- queries.append(curr_query)
- else:
- # We have the query cached
- curr_query = queries[query_num]
- assert '.' not in curr_query
- query_num += 1
- query_results[curr_query] = []
- else:
- query_results[curr_query].append(l)
- i += 1
- return query_results
- def strip_output(output):
- """Strips the top and end of the output, checking for them."""
- if len(output) == 0:
- print " MISSING: everything"
- else:
- if output[0] == PRE:
- output = output[1:]
- else:
- if output[0].startswith("Indexing"):
- output = output[1:]
- print " MISSPELLED: " + PRE
- else:
- print " MISSING: " + PRE
- if output[-1] == POST:
- output = output[:-1]
- else:
- if output[-1].startswith("shutting"):
- output = output[:-1]
- print " MISSPELLED: " + POST
- else:
- print " MISSING: " + POST
- if output[-1] == PROMPT:
- output = output[:-1]
- return output
- with open('{}/hw2.expected'.format(SCRIPTDIR)) as f:
- expected = genQDict(strip_output(f.readlines()))
- skipTo = None #'chenqy/'
- reached = False
- for id in dirs.split():
- if skipTo and not reached and id != skipTo:
- continue
- elif id == skipTo:
- reached = True
- print "\n=== GRADING %s ===" % id
- cd('{}/hw/hw2/{}/hw2'.format(GRADINGDIR, id))
- try:
- output = sh(
- 'timeout 120s ./searchshell ./test_tree < {}/hw2.queries'.format(
- SCRIPTDIR),
- shell=True)
- with open('../shell.output', 'w+') as f:
- f.write(output)
- output = [s.strip() for s in output.split('\n') if len(s.strip())]
- output = strip_output(output)
- count = output.count(PROMPT)
- if count == 0:
- print " SKIPPING: Did not handle queries"
- continue
- elif count != len(queries):
- print " SKIPPING: Expected %s queries, Found %s" % (len(queries),
- count)
- print " See %s/shell.output" % id
- continue
- except Exception as e:
- print ' EXCEPTION:', e
- continue
- # Make sure all the queries are the same
- try:
- actual = genQDict(output, expect_query_line=False)
- for q in expected:
- if len(expected[q]) > len(actual[q]):
- print(" Actual had less results than expected for query: %s" %
- q)
- elif len(expected[q]) < len(actual[q]):
- print(" Actual had more results than expected for query: %s" %
- q)
- else:
- # Make sure the results are the same
- for res in expected[q]:
- if res not in actual[q]:
- print(" Query \"%s\" MISSING %s" % (q, res))
- break
- # Make sure the order was monotonically decreasing
- prev = None
- for res in actual[q]:
- if prev:
- prevrank = prev[prev.rfind('(') + 1:prev.rfind(')')]
- rank = res[res.rfind('(') + 1:res.rfind(')')]
- if int(prevrank) < int(rank):
- print(" Misaligned order for query: %s" % q)
- prev = res
- except Exception as e:
- print ' EXCEPTION:', e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement