You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.7 KiB

  1. #!/usr/bin/env python
  2. import json
  3. import sys
  4. import glob
  5. from datetime import datetime
  6. solutionsDir = "solutions/"
  7. class bcolors:
  8. HEADER = '\033[95m'
  9. OKBLUE = '\033[94m'
  10. OKGREEN = '\033[92m'
  11. WARNING = '\033[93m'
  12. FAIL = '\033[91m'
  13. ENDC = '\033[0m'
  14. BOLD = '\033[1m'
  15. UNDERLINE = '\033[4m'
  16. if len(sys.argv) < 2 or sys.argv[1] != "status":
  17. results = json.loads(''.join(sys.stdin.readlines()))
  18. resultsScore = {}
  19. with open('scores') as f:
  20. for line in f:
  21. tmp = line.strip().split(' ')
  22. key = tmp[0]+"."+tmp[1]
  23. if not key in resultsScore:
  24. resultsScore[key] = int(tmp[2])
  25. #else:
  26. # print "Score alredy present for " + tmp[0] + " " + tmp[1]
  27. f.close()
  28. if len(sys.argv) < 2 or sys.argv[1] != "status":
  29. strategy = "unknown" if len(sys.argv) < 2 else sys.argv[1]
  30. for i in range(0, len(results)):
  31. key = str(results[i]['problemId']) + "." + str(results[i]['seed'])
  32. if key in resultsScore:
  33. out_file = open(solutionsDir+str(results[i]['problemId'])+"."+str(results[i]['seed'])+"."+"%.110d"%resultsScore[key]+"."+strategy,"w")
  34. results[i]['tag'] = strategy
  35. out_file.write(json.dumps(results[i]))
  36. out_file.close()
  37. else:
  38. print "Missing score for " + str(results[i]['problemId']) + " " + str(results[i]['seed'])
  39. sys.exit(1)
  40. else:
  41. results=[{'problemId':''}]
  42. solutionsToSend=[]
  43. scores = {}
  44. lastProblem = ""
  45. for solution in sorted(glob.glob(solutionsDir + str(results[0]['problemId']) + '.*'), reverse=True):
  46. problemHash = solution[:solution.rfind(".", 0, solution.rfind(".")) if solution.count('.') == 3 else solution.rfind(".")]
  47. if lastProblem != problemHash:
  48. lastProblem = problemHash
  49. with open(solution) as json_data:
  50. partialSolution = json.load(json_data)
  51. json_data.close()
  52. solutionsToSend.append(partialSolution)
  53. scores[lastProblem.replace(solutionsDir, '')] = int(solution.split('.')[2])
  54. if len(sys.argv) < 2 or sys.argv[1] != "status":
  55. print json.dumps(list(reversed(solutionsToSend))).replace(', ', ',').replace(': ', ':')
  56. else:
  57. import subprocess
  58. process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X GET https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
  59. out, err = process.communicate()
  60. if err == None:
  61. serverResults = {}
  62. results = json.loads(out)
  63. for result in results:
  64. #2015-08-08T12:26:42.894Z
  65. date_object = datetime.strptime(result['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ')
  66. # print str(date_object)
  67. key = str(result['problemId'])+"."+str(result['seed'])
  68. if not key in serverResults or serverResults[key]['createdAt'] < result['createdAt']:
  69. serverResults[key] = result
  70. #print results
  71. tags = {}
  72. for serverResult in serverResults:
  73. if not serverResults[serverResult]['tag'] in tags:
  74. tags[serverResults[serverResult]['tag']] = 1
  75. else:
  76. tags[serverResults[serverResult]['tag']] += 1
  77. print "Submitted solution: %d"%len(solutionsToSend)
  78. for solutionToSend in solutionsToSend:
  79. key = str(solutionToSend['problemId'])+"."+str(solutionToSend['seed'])
  80. if not key in serverResults:
  81. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " not present on server" + bcolors.ENDC
  82. else:
  83. if solutionToSend['solution'] != serverResults[key]['solution']:
  84. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution differ: " + solutionToSend['solution'] + " != " + serverResults[key]['solution'] + " (server)" + bcolors.ENDC
  85. elif serverResults[key]['score'] == None:
  86. found = False
  87. for i in range(1, len(results) + 1):
  88. result = results[len(results)- i]
  89. if result['problemId'] == solutionToSend['problemId'] and result['seed'] == solutionToSend['seed'] and result['solution'] == solutionToSend['solution'] and scores[key] == result['score']:
  90. found = True
  91. print bcolors.WARNING + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " " + bcolors.OKGREEN + "score " + str(result['score']) + " ok" + bcolors.ENDC + bcolors.WARNING + ", but solution evaluation pending" + bcolors.ENDC
  92. break
  93. if not found:
  94. print bcolors.WARNING + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution evaluation pending" + bcolors.ENDC
  95. elif scores[key] != serverResults[key]['score']:
  96. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " score differ: " + str(scores[key]) + " != " + str(serverResults[key]['score']) + " (server)" + bcolors.ENDC
  97. print "Tags statistics:"
  98. for tag in tags:
  99. print " %s: %d"%(tag, tags[tag])
  100. # print scores
  101. # print type(out)
  102. # print json.dumps(serverResults)
  103. # print len(serverResults)
  104. else:
  105. print "error retriving results from server"
  106. sys.exit(1)
  107. sys.exit(0)