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.

114 lines
4.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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. problemIdForGlob = str(results[0]['problemId']) + '.'
  41. else:
  42. problemIdForGlob = ''
  43. solutionsToSend=[]
  44. scores = {}
  45. lastProblem = ""
  46. for solution in sorted(glob.glob(solutionsDir + problemIdForGlob + '*'), reverse=True):
  47. problemHash = solution[:solution.rfind(".", 0, solution.rfind(".")) if solution.count('.') == 3 else solution.rfind(".")]
  48. if lastProblem != problemHash:
  49. lastProblem = problemHash
  50. with open(solution) as json_data:
  51. partialSolution = json.load(json_data)
  52. json_data.close()
  53. solutionsToSend.append(partialSolution)
  54. scores[lastProblem.replace(solutionsDir, '')] = int(solution.split('.')[2])
  55. if len(sys.argv) < 2 or sys.argv[1] != "status":
  56. print json.dumps(list(reversed(solutionsToSend))).replace(', ', ',').replace(': ', ':')
  57. else:
  58. import subprocess
  59. process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X GET https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
  60. out, err = process.communicate()
  61. if err == None:
  62. serverResults = {}
  63. results = json.loads(out)
  64. for result in results:
  65. #2015-08-08T12:26:42.894Z
  66. date_object = datetime.strptime(result['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ')
  67. # print str(date_object)
  68. key = str(result['problemId'])+"."+str(result['seed'])
  69. if not key in serverResults or serverResults[key]['createdAt'] < result['createdAt']:
  70. serverResults[key] = result
  71. #print results
  72. tags = {}
  73. for serverResult in serverResults:
  74. if not serverResults[serverResult]['tag'] in tags:
  75. tags[serverResults[serverResult]['tag']] = 1
  76. else:
  77. tags[serverResults[serverResult]['tag']] += 1
  78. print "Submitted solution: %d"%len(serverResults)
  79. for solutionToSend in solutionsToSend:
  80. key = str(solutionToSend['problemId'])+"."+str(solutionToSend['seed'])
  81. if not key in serverResults:
  82. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " not present on server" + bcolors.ENDC
  83. else:
  84. if solutionToSend['solution'] != serverResults[key]['solution']:
  85. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution differ: " + solutionToSend['solution'] + " != " + serverResults[key]['solution'] + " (server)" + bcolors.ENDC
  86. elif serverResults[key]['score'] == None:
  87. found = False
  88. for i in range(1, len(results) + 1):
  89. result = results[len(results)- i]
  90. if result['problemId'] == solutionToSend['problemId'] and result['seed'] == solutionToSend['seed'] and result['solution'] == solutionToSend['solution'] and scores[key] == result['score']:
  91. found = True
  92. 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
  93. break
  94. if not found:
  95. print bcolors.WARNING + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution evaluation pending" + bcolors.ENDC
  96. elif scores[key] != serverResults[key]['score']:
  97. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " score differ: " + str(scores[key]) + " != " + str(serverResults[key]['score']) + " (server)" + bcolors.ENDC
  98. print "Tags statistics:"
  99. for tag in tags:
  100. print " %s: %d"%(tag, tags[tag])
  101. # print scores
  102. # print type(out)
  103. # print json.dumps(serverResults)
  104. # print len(serverResults)
  105. else:
  106. print "error retriving results from server"
  107. sys.exit(1)
  108. sys.exit(0)