def tasklogic(self,filepath): with open(filepath,"r") as filehandle: filelocker.lock(filehandle,filelocker.LOCK_NB) #try lock the task try: self.log("normal",0,"开始执行任务%s"%filepath) fsname = os.path.basename(filepath).split(".")[0] success = self.dowork(fsname,filehandle.read()) except Exception,e: self.log("warning",0,"派生类未捕获异常%s"%str(e)) filelocker.unlock(filehandle)
def log(self,level,typeid,msg): logdir = self.options.log if(not os.path.exists(logdir)): os.mkdir(logdir) filename = time.strftime('%Y-%m-%d',time.localtime(time.time()))+".log" t = time.strftime('%H:%M:%S',time.localtime(time.time())) filepath = os.path.join(logdir,filename) with open(filepath,"a") as f: filelocker.lock(f,filelocker.LOCK_EX) #block lock logmsg = "[%8s][%s][%s][%d]%s"%(t,self.options.uuid,level,typeid,msg) f.write(logmsg+"\n") filelocker.unlock(f) print logmsg.decode("utf8").encode("gbk")
def _create_new_build_id(self): """Find a uniq build_id to use""" with filelocker.lock("%s.lock" % self.temp_config_file, filelocker.LOCK_EX): # Read temp_config again to get any new entries self.temp_config.read(self.temp_config_file) # Find a uniq build_id to use if self.build_id is None: ids = self.temp_config.sections() if ids: max_id = int(max(self.temp_config.sections(), key=int)) self.build_id = max_id + 1 else: self.build_id = 1 self.logger.debug("Will use \"%s\" as build id" % _green(self.build_id)) # Create a new section try: self.temp_config.add_section(str(self.build_id)) except DuplicateSectionError: msg = ("Build id \"%s\" already in use. " + "Please use a uniq one or cleanup \"%s\" file.\n") \ % (self.build_id, self.temp_config_file) self.logger.error(msg) sys.exit(1) creation_time = \ time.strftime("%a, %d %b %Y %X", time.localtime()) self.temp_config.set(str(self.build_id), "created", str(creation_time)) # Write changes back to temp config file with open(self.temp_config_file, 'wb') as tcf: self.temp_config.write(tcf)
def write_temp_config(self, option, value): """Write changes back to config file""" # Acquire the lock to write to temp_config_file with filelocker.lock("%s.lock" % self.temp_config_file, filelocker.LOCK_EX): # Read temp_config again to get any new entries self.temp_config.read(self.temp_config_file) self.temp_config.set(str(self.build_id), option, str(value)) curr_time = time.strftime("%a, %d %b %Y %X", time.localtime()) self.temp_config.set(str(self.build_id), "modified", curr_time) # Write changes back to temp config file with open(self.temp_config_file, "wb") as tcf: self.temp_config.write(tcf)
def write_temp_config(self, option, value): """Write changes back to config file""" # Acquire the lock to write to temp_config_file with filelocker.lock("%s.lock" % self.temp_config_file, filelocker.LOCK_EX): # Read temp_config again to get any new entries self.temp_config.read(self.temp_config_file) self.temp_config.set(str(self.build_id), option, str(value)) curr_time = time.strftime("%a, %d %b %Y %X", time.localtime()) self.temp_config.set(str(self.build_id), "modified", curr_time) # Write changes back to temp config file with open(self.temp_config_file, 'wb') as tcf: self.temp_config.write(tcf)