- added .restart and .update

Fri, 13 Jun 2014 00:14:42 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 13 Jun 2014 00:14:42 +0300
changeset 5
b6d2b7de0a6d
parent 4
bd9508f8a10f
child 6
2c6265a99d32

- added .restart and .update

cobalt.py file | annotate | diff | comparison | revisions
--- a/cobalt.py	Mon Jun 09 02:06:23 2014 +0300
+++ b/cobalt.py	Fri Jun 13 00:14:42 2014 +0300
@@ -36,6 +36,8 @@
 import json
 import urllib
 import urllib2
+import hgapi
+import os
 from suds.xsd.doctor import Import
 from suds.xsd.doctor import ImportDoctor
 from suds.client import Client
@@ -104,8 +106,12 @@
 			print line
 			control (line)
 	for client in g_clients:
-		client.exceptdie()
-	quit()
+		if len(data) > 0:
+			client.exceptdie()
+		else:
+			client.quit_irc()
+	restart_self()
+	#quit()
 
 sys.excepthook = handle_exception
 
@@ -119,6 +125,11 @@
 	def __str__ (self):
 		return self.value
 
+# from http://www.daniweb.com/software-development/python/code/260268/restart-your-python-program
+def restart_self():
+	python = sys.executable
+	os.execl (python, python, * sys.argv)
+
 #
 # Main IRC client class
 #
@@ -195,6 +206,10 @@
 			if len(words) >= 2 and words[1] == "PRIVMSG":
 				self.handle_privmsg (line)
 
+	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+	#
+	#	Handle a PRIVMSG line from the IRC server
+	#
 	def handle_privmsg (self, line):
 		rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
 		match = rex.match (line)
@@ -224,6 +239,10 @@
 		else:
 			control ("Recieved bad PRIVMSG: %s" % line)
 
+	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+	#
+	#	Retrieve a ticket from mantisbt
+	#
 	def get_ticket_data (self, replyto, ticket, withlink):
 		if suds_active == False:
 			return
@@ -246,6 +265,10 @@
 			if withlink:
 				self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket))
 
+	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+	#
+	#	Process an IRC command
+	#
 	def handle_command (self, sender, ident, host, replyto, command, args):
 		if command == "raw":
 			check_admin (sender, ident, host, command)
@@ -294,8 +317,28 @@
 				raise e
 			except Exception as e:
 				raise logical_exception ('Search failed: %s' % `e`)
-		else:
-			raise logical_exception ("unknown command `.%s`" % command)
+		elif command == 'restart':
+			check_admin (sender, ident, host, command)
+			excepterm('')
+		elif command == 'update':
+			check_admin (sender, ident, host, command)
+
+			try:
+				repo = hgapi.Repo ('.')
+				r1 = repo.hg_id()
+				repo.hg_pull()
+				repo.hg_update('tip', True)
+				r2 = repo.hg_id()
+				self.privmsg (replyto, '%s <-> %s' % (r1, r2))
+				if r1 != r2:
+					self.privmsg (replyto, 'Updated to %s, restarting...' % r2)
+					excepterm('')
+				else:
+					self.privmsg (replyto, 'Up to date at %s.' % r2)
+			except hgapi.HgException as e:
+				raise logical_exception ('Search failed: %s' % `e`)
+#		else:
+#			raise logical_exception ("unknown command `.%s`" % command)
 
 	def handle_error(self):
 		excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
@@ -303,6 +346,12 @@
 	def privmsg (self, channel, msg):
 		self.write ("PRIVMSG %s :%s" % (channel, msg))
 
+	def quit_irc (self):
+		if self.flags & CLIF_CONNECTED:
+			self.write ("QUIT :Leaving")
+		self.send_all_now()
+		self.close()
+
 	def exceptdie (self):
 		if self.flags & CLIF_CONNECTED:
 			self.write ("QUIT :Caught exception")

mercurial