def compile(self, submission_id=None, report_status=False, run_test=True): def report(status, language="Unknown", errors=None): if report_status: self.post_id += 1 result = {"post_id": self.post_id, "submission_id": submission_id, "status_id": status, "language": language } if status != 40: if type(errors) != list: errors = [errors] # for valid json according to php # get rid of any binary garbage by decoding to UTF-8 for i in range(len(errors)): try: errors[i] = errors[i].decode("UTF-8", "replace") except AttributeError: pass result['errors'] = json.dumps(errors) return self.cloud.post_result('api_compile_result', result) else: return True if submission_id == None: # compile in current directory compiler.compile_anything(os.getcwd()) else: submission_dir = self.submission_dir(submission_id) if os.path.exists(submission_dir): log.info("Already compiled: %s" % submission_id) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: if report(STATUS_RUNABLE, compiler.get_run_lang(submission_dir)): return True else: log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False else: report(STATUS_TEST_ERROR, compiler.get_run_lang(submission_dir), errors) log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False if (not submission_id in self.download_dirs or len(os.listdir(self.download_dir(submission_id))) == 0): if not self.download_submission(submission_id): report(STATUS_DOWNLOAD_ERROR) log.error("Download Error") return False download_dir = self.download_dir(submission_id) if not os.path.exists(os.path.join(self.download_dir(submission_id), 'bot')): if len(os.listdir(download_dir)) == 1: if not self.unpack(submission_id): report(STATUS_UNPACK_ERROR) log.error("Unpack Error") return False log.info("Compiling %s " % submission_id) bot_dir = os.path.join(download_dir, 'bot') detected_lang, errors = compiler.compile_anything(bot_dir) if errors != None: log.info(errors) if not self.debug: shutil.rmtree(download_dir) log.error(str(errors)) log.error(detected_lang) report(STATUS_COMPILE_ERROR, detected_lang, errors=errors); log.error("Compile Error") return False else: log.info("Detected language: {0}".format(detected_lang)) if not os.path.exists(os.path.split(submission_dir)[0]): os.makedirs(os.path.split(submission_dir)[0]) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: os.rename(download_dir, submission_dir) del self.download_dirs[submission_id] if report(STATUS_RUNABLE, detected_lang): return True else: # could not report back to server, cleanup compiled dir log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False else: log.info("Functional Test Failure") report(STATUS_TEST_ERROR, detected_lang, errors) return False
def compile(self, submission_id=None, report_status=(False, False), run_test=True): report_success, report_failure = report_status def report(status, language="Unknown", errors=None): # oooh, tricky, a terinary in an if if report_success if type(errors) != list else report_failure: self.post_id += 1 result = {"post_id": self.post_id, "submission_id": submission_id, "status_id": status, "language": language } if status != 40: if type(errors) != list: errors = [errors] # for valid json result['errors'] = json.dumps(errors) return self.cloud.post_result('api_compile_result', result) else: return True if submission_id == None: # compile in current directory compiler.compile_anything(os.getcwd()) else: submission_dir = self.submission_dir(submission_id) if os.path.exists(submission_dir): log.info("Already compiled: %s" % submission_id) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: if report(STATUS_RUNABLE, compiler.get_run_lang(submission_dir)): return True else: log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False else: report(STATUS_TEST_ERROR, compiler.get_run_lang(submission_dir), errors) log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False if (not submission_id in self.download_dirs or len(os.listdir(self.download_dir(submission_id))) == 0): if not self.download_submission(submission_id): report(STATUS_DOWNLOAD_ERROR) log.error("Download Error") return False download_dir = self.download_dir(submission_id) if not os.path.exists(os.path.join(self.download_dir(submission_id), 'bot')): if len(os.listdir(download_dir)) == 1: if not self.unpack(submission_id): report(STATUS_UNPACK_ERROR) log.error("Unpack Error") return False log.info("Compiling %s " % submission_id) bot_dir = os.path.join(download_dir, 'bot') timelimit = 10 * 60 # 10 minute limit to compile submission if not run_test: # give it 50% more time if this isn't the initial compilation # this is to try and prevent the situation where the initial # compilation just makes it in the time limit and then a # subsequent compilation fails when another worker goes to # play a game with it timelimit += timelimit * 0.5 detected_lang, errors = compiler.compile_anything(bot_dir, timelimit) if errors != None: log.error(errors) if not self.debug: shutil.rmtree(download_dir) log.error(detected_lang) report(STATUS_COMPILE_ERROR, detected_lang, errors=errors); log.error("Compile Error") return False else: log.info("Detected language: {0}".format(detected_lang)) if not os.path.exists(os.path.split(submission_dir)[0]): os.makedirs(os.path.split(submission_dir)[0]) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: os.rename(download_dir, submission_dir) del self.download_dirs[submission_id] if report(STATUS_RUNABLE, detected_lang): return True else: # could not report back to server, cleanup compiled dir log.debug("Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False else: log.info("Functional Test Failure") report(STATUS_TEST_ERROR, detected_lang, errors) return False
def compile(self, submission_id=None, report_status=(False, False), run_test=True): report_success, report_failure = report_status def report(status, language="Unknown", errors=None): # oooh, tricky, a terinary in an if if report_success if type(errors) != list else report_failure: self.post_id += 1 result = { "post_id": self.post_id, "submission_id": submission_id, "status_id": status, "language": language } if status != 40: if type(errors) != list: errors = [errors] # for valid json result['errors'] = json.dumps(errors) return self.cloud.post_result('api_compile_result', result) else: return True if submission_id == None: # compile in current directory compiler.compile_anything(os.getcwd()) else: submission_dir = self.submission_dir(submission_id) if os.path.exists(submission_dir): log.info("Already compiled: %s" % submission_id) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: if report(STATUS_RUNABLE, compiler.get_run_lang(submission_dir)): return True else: log.debug("Cleanup of compiled dir: {0}".format( submission_dir)) shutil.rmtree(submission_dir) return False else: report(STATUS_TEST_ERROR, compiler.get_run_lang(submission_dir), errors) log.debug( "Cleanup of compiled dir: {0}".format(submission_dir)) shutil.rmtree(submission_dir) return False if (not submission_id in self.download_dirs or len(os.listdir(self.download_dir(submission_id))) == 0): if not self.download_submission(submission_id): report(STATUS_DOWNLOAD_ERROR) log.error("Download Error") return False download_dir = self.download_dir(submission_id) if not os.path.exists( os.path.join(self.download_dir(submission_id), 'bot')): if len(os.listdir(download_dir)) == 1: if not self.unpack(submission_id): report(STATUS_UNPACK_ERROR) log.error("Unpack Error") return False log.info("Compiling %s " % submission_id) bot_dir = os.path.join(download_dir, 'bot') timelimit = 10 * 60 # 10 minute limit to compile submission if not run_test: # give it 50% more time if this isn't the initial compilation # this is to try and prevent the situation where the initial # compilation just makes it in the time limit and then a # subsequent compilation fails when another worker goes to # play a game with it timelimit += timelimit * 0.5 detected_lang, errors = compiler.compile_anything( bot_dir, timelimit) if errors != None: log.error(errors) if not self.debug: shutil.rmtree(download_dir) log.error(detected_lang) report(STATUS_COMPILE_ERROR, detected_lang, errors=errors) log.error("Compile Error") return False else: log.info("Detected language: {0}".format(detected_lang)) if not os.path.exists(os.path.split(submission_dir)[0]): os.makedirs(os.path.split(submission_dir)[0]) if run_test: errors = self.functional_test(submission_id) else: errors = None if errors == None: os.rename(download_dir, submission_dir) del self.download_dirs[submission_id] if report(STATUS_RUNABLE, detected_lang): return True else: # could not report back to server, cleanup compiled dir log.debug("Cleanup of compiled dir: {0}".format( submission_dir)) shutil.rmtree(submission_dir) return False else: log.info("Functional Test Failure") report(STATUS_TEST_ERROR, detected_lang, errors) return False