import sys, os
import random, time
from subprocess import call

since_epoch = time.time()
assignment_directory = "assignment-%s" % since_epoch

num_files = random.randint(5, 15)
num_garbage = random.randint(5, 15)

# CONTRACT
# string -> string
# PURPOSE
# Takes a string and prepends the assignment directory
def assign (name):
  return "%s/%s" % (assignment_directory, name)

# CONTRACT
# string -> void
# PURPOSE
# 'touch'es a filename given.
def touch (fname, times=None):
	with file(fname, 'a'):
		os.utime(fname, times)

def garbage_ending ():
	return random.choice(["~", "swp", "log", "aux", "out", "toc"])

def good_ending ():
	return random.choice (["md", "tex", "c", "py", "bash", "sh"])

# Create the assignment directory
os.makedirs(assignment_directory)

# Generate the files
files = []
for i in range (0, num_files):
	f = "file-%s.%s" % (i, good_ending())
	files.append(f)
	touch(assign(f))

garbage = []
for i in range (0, num_garbage):
	f = "garbage-%s.%s" % (i, garbage_ending())
	garbage.append(f)
	touch(assign(f))

# Call your script
retval = call(["./assignment-submission.bash", assignment_directory])

import os.path
# Check the results of the student script
# os.path.isfile()

# Check that the .tar.gz was created

targz = "%s.tar.gz" % assignment_directory
if not os.path.isfile(targz):
	print "Where is my tar.gz file!? Exiting."
	exit()

import shutil
# Expand the tar.gz file in /tmp
shutil.copyfile(targz, "/tmp/%s" % targz)
os.chdir("/tmp")
retval = call(["tar", "xvzf", targz])

# CD into the assignment directory
os.chdir(assignment_directory)

result = True

for f in files:
	if not os.path.isfile(f):
		print "I'm missing the file '%s'. Oops!" % f
		result = False

for g in garbage:
	if os.path.isfile(g):
		print "You should have cleaned up the file '%s'. Oops!" % g
		result = False

if result:
	print "Looks like your script checks out! Congrats!"
else:
	print "Looks like your script didn't do its job. What a sad, sad day."

# Cleanup