コード例 #1
0
	def write_lock(self):
		""" enter critical section """
		ml_log.log(self.tag + ".write_lock")
		self.lockfile = open(ml_system.LOCK_FILE, "w")
		fcntl.flock(self.lockfile, fcntl.LOCK_EX)
		if self.threadlock:
			self.threadlock.acquire()
コード例 #2
0
	def set(self, cfg={}):
		""" sync config, do_set, dump. """
		ml_log.log(self.tag + ".set()")

		ret = ml_check.sync(self.cfg, cfg, self.main_syntax)
		if not ret[0]:
			return ret

		self.write_lock()
		try:
			ret = self.do_set()
		except Exception as e:
			ret = (False, [str(e)])
		self.write_unlock()
		if not ret[0]:
			# do_set fails, load old cfg then do_set.
			ret_load = self.load()
			if ret_load[0]:
				self.write_lock()
				try:
					ret_set = self.do_set()
				except Exception as e:
					ret_set = (False, [str(e)])
				self.write_unlock()
			return ret
		else:
			return self.dump()
コード例 #3
0
	def __init__(self, fpath = os.path.join(ml_system.CFG_PATH, "config.txt"), threadlock = None):
		""" init config """
		self.tag = "config"
		ml_log.log(self.tag + ".__init__")
		self.fpath = fpath
		self.cfg = {}
		self.default = {}
		self.main_syntax = {}
		self.helper = []
		# lock
		self.lockfile = None
		self.threadlock = threadlock
コード例 #4
0
	def load(self):
		""" load from a file """
		ml_log.log(self.tag + ".load()")
		self.read_lock()
		e = self.do_load()
		self.read_unlock()
		if not e[0]:
			return e

		ret = ml_check.sync(self.cfg, e[1], self.main_syntax)
		if not ret[0]:
			return ret

		return (True, None)
コード例 #5
0
	def get(self):
		""" load, do_get. """
		ml_log.log(self.tag + ".get()")
		e = self.load()
		if not e[0]:
			return e
		self.write_lock()
		try:
			ret = self.do_get()
		except Exception as e:
			ret = (False, [str(e)])
		self.write_unlock()
		if not ret[0]:
			return ret
		return (True, self.cfg.copy())
コード例 #6
0
	def dump(self):
		""" dump to a file """
		ml_log.log(self.tag + ".dump()")

		# ignore empty config items (None or "") maybe include default
		dumpcfg = {}
		dumpcfg.update(self.cfg)
		#for k in dumpcfg.keys():
		#	if not dumpcfg[k] or dumpcfg[k] == "":
		#		dumpcfg.pop(k)

		self.write_lock()
		ret = self.do_dump(dumpcfg)
		self.write_unlock()
		return ret
コード例 #7
0
def sh(ocmd, stdout="", block=True):
	cmd = " ".join(ocmd)
	if log_workflow:
		#fp = open(const.WORKFLOW_LOG, "a")
		#fp.write(cmd + "\n")
		#fp.close()
		ml_log.log(__name__ + ": " + cmd)
	try:
		p = subprocess.Popen(cmd, shell=True,
			stdin = subprocess.PIPE,
			stdout = subprocess.PIPE,
			stderr = subprocess.PIPE,
			close_fds=True)
		if block:
			x = p.communicate()
			if 0 != p.returncode:
				#log_failure(cmd, x[0])
				ml_log.log(__name__ + ": " + cmd + str(x[0]))
				return (False, x[0])
		else:
			x = [""]
	except Exception as e:
		#log_failure(cmd, e)
		ml_log.log(__name__ + ": " + cmd + str(e))
		return (False, str(e))
	return (True, x[0])
コード例 #8
0
	def write_unlock(self):
		""" leave critical section """
		ml_log.log(self.tag + ".write_unlock")
		self.unlock()
コード例 #9
0
	def read_unlock(self):
		""" leave critical section """
		ml_log.log(self.tag + ".read_unlock")
		self.unlock()