def doSave(self, trees=None, startTime=None, force=None, update_md5=None): # Save failure reason codes NON_MATCHING_ANNOTALDS = 1 NON_MATCHING_HASHES = 2 cherrypy.response.headers["Content-Type"] = "application/json" if (startTime != self.startTime) and not (force == "true"): return json.dumps( dict( result="failure", reason="non-matching invocations of Annotald", # noqa reasonCode=NON_MATCHING_ANNOTALDS, startTime=self.startTime, ) ) tosave = self.integrateTrees(trees) tosave = tosave.replace("-FLAG", "") print("self.thefile is: %s" % self.thefile) if update_md5: self.versionCookie = util.updateVersionCookie( self.versionCookie, "HASH.MD5", util.hashTrees(trees, self.versionCookie) ) if util.queryVersionCookie(self.versionCookie, "HASH.MD5"): print("checking hash") # TODO: document hash function in user manual old_hash = util.queryVersionCookie(self.versionCookie, "HASH.MD5") new_hash = util.hashTrees(tosave, self.versionCookie) if old_hash != new_hash: return json.dumps( dict( result="failure", reason=("corpus text has changed" + " (it shouldn't!)"), reasonCode=NON_MATCHING_HASHES, startTime=self.startTime, ) ) tosave = tosave.replace("-FLAG", "") try: util.writeTreesToFile(self.versionCookie, tosave, self.thefile) self.doLogEvent(json.dumps({"type": "save"})) return json.dumps(dict(result="success")) except Exception as e: print("something went wrong: %s" % e) traceback.print_exc() return json.dumps(dict(result="failure", reason="server got an exception"))
def doExit(self): print("Exit message received") print("Reformatting trees") if self.pythonOptions["rewriteIndices"]: print("...and rewriting indices sequentially") print("Please be patient, this may take some time") util.writeTreesToFile( self.versionCookie, "\n\n".join(self.trees), self.thefile, True, self.pythonOptions["rewriteIndices"] ) print("Done. :)") self.doLogEvent(json.dumps({"type": "program-exit"})) time.sleep(3) # Wait for log events from server if self.eventLog: self.eventLog.close() self.eventLog = None # forceful exit to make up for lack of proper thread management os._exit(0)