Guest User

Untitled

a guest
Aug 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import os, filecmp
  2.  
  3. codes = {200:'success',404:'file not found',400:'error',408:'timeout'}
  4.  
  5. def compile(file,lang):
  6. if lang == 'java':
  7. class_file = file[:-4]+"class"
  8. elif lang == 'c':
  9. class_file = file[:-2]
  10. elif lang=='cpp':
  11. class_file = file[:-4]
  12.  
  13. if (os.path.isfile(class_file)):
  14. os.remove(class_file)
  15. if (os.path.isfile(file)):
  16. if lang == 'java':
  17. os.system('javac '+file)
  18. elif lang == 'c' or lang == 'cpp':
  19. os.system('gcc -o '+class_file+' '+file)
  20. if (os.path.isfile(class_file)):
  21. return 200
  22. else:
  23. return 400
  24. else:
  25. return 404
  26.  
  27. def run(file,input,timeout,lang):
  28. if lang == 'java':
  29. cmd = 'java '+file
  30. elif lang=='c' or lang=='cpp':
  31. cmd = './'+file
  32. r = os.system('timeout '+timeout+' '+cmd+' < '+input+' > out.txt')
  33. if lang == 'java':
  34. os.remove(file+'.class')
  35. elif lang == 'c' or lang == 'cpp':
  36. os.remove(file)
  37. if r==0:
  38. return 200
  39. elif r==31744:
  40. os.remove('out.txt')
  41. return 408
  42. else:
  43. os.remove('out.txt')
  44. return 400
  45.  
  46. def match(output):
  47. if os.path.isfile('out.txt') and os.path.isfile(output):
  48. b = filecmp.cmp('out.txt',output)
  49. #os.remove('out.txt')
  50. return b
  51. else:
  52. return 404
  53.  
  54. file = 'add.c'
  55. lang = 'c'
  56. testin = 'testin.txt'
  57. testout = 'testout.txt'
  58. timeout = '1' # secs
  59.  
  60. print(codes[compile(file,'c')])
  61. print (codes[run('add',testin,timeout,lang)])
  62. print (match(testout)) # True implies that code is accepted.
Add Comment
Please, Sign In to add comment