Skip to content

Commit c328ad3

Browse files
author
bspkrs
committed
add fpull and restart
1 parent 6e30805 commit c328ad3

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

BotBase.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import Logger
44
import AsyncSocket
5-
import asyncore, socket
5+
import asyncore
66
import DCCSocket
77
import threading
88
import datetime
99
import json
10-
import os
10+
import os, sys, subprocess
1111
import shutil
1212
import time
1313
from IRCHandler import CmdHandler, CmdGenerator, Sender, Color, EOL
@@ -69,7 +69,9 @@ def run(self):
6969
raise
7070

7171
if not self.bot.isTerminating:
72-
if not self.bot.isRestarting:
72+
if self.bot.isRestarting:
73+
restart_program()
74+
else:
7375
self.bot.logger.warning('IRC connection was lost.')
7476
reconnect_attempts += 1
7577

@@ -80,6 +82,10 @@ def run(self):
8082
else:
8183
restart = False
8284

85+
def restart_program():
86+
python = sys.executable
87+
os.execl(python, python, *sys.argv)
88+
8389

8490
class BotBase(object):
8591
def __init__(self, configfile=None, backupcfg=False):
@@ -192,6 +198,7 @@ def __init__(self, configfile=None, backupcfg=False):
192198
self.registerCommand('sendraw', self.sendRawCmd, ['admin'], 0, 999, "<irccmd>", "Send a raw IRC cmd.")
193199
self.registerCommand('shutdown', self.killSelf, ['admin'], 0, 0, "", "Kills the bot.")
194200
self.registerCommand('restart', self.restart, ['admin'], 0, 0, '', 'Restarts the bot.')
201+
self.registerCommand('fpull', self.fpull, ['admin'], 0, 0, '', 'Pulls and updates the bot code.')
195202
self.registerCommand('readonly', self.setReadOnly, ['admin'], 1, 1, '[true|false]', 'Sets Read-Only Mode for commands that are able to set data.')
196203

197204
self.registerCommand('help', self.helpcmd, ['any'], 0, 1, "[<command>|*]", "Lists available commands or help about a specific command.", allowpub=True)
@@ -206,7 +213,7 @@ def __init__(self, configfile=None, backupcfg=False):
206213
self.updateConfig()
207214

208215
def clone(self):
209-
return BotBase(self.configfile, self.nspass, self.backupcfg)
216+
return BotBase(self.configfile, self.backupcfg)
210217

211218
def run(self):
212219
if self.host == "":
@@ -266,6 +273,31 @@ def setReadOnly(self, bot, sender, dest, cmd, args):
266273
else:
267274
self.sendPrimChanMessage('%s is no longer in read-only mode. All commands are now available again.' % self.nick)
268275

276+
def fpull(self, bot, sender, dest, cmd, args):
277+
"""Pulls from the repository to update the bot."""
278+
279+
commands = ["hg pull",
280+
"hg update"]
281+
282+
for command in commands:
283+
child = subprocess.Popen(command.split(),
284+
stdout=subprocess.PIPE,
285+
stderr=subprocess.PIPE)
286+
(out, err) = child.communicate()
287+
ret = child.returncode
288+
289+
for line in (out + err).splitlines():
290+
self.sendOutput(dest, line.decode("utf-8"))
291+
292+
if ret != 0:
293+
if ret < 0:
294+
cause = "signal"
295+
ret *= -1
296+
else:
297+
cause = "status"
298+
self.logger.error('Command "%s" returned %s %d' % (command, cause, ret))
299+
else:
300+
self.sendOutput(dest, 'Successfully pulled and updated.')
269301

270302
# User handling commands
271303
def useradd(self, bot, sender, dest, cmd, args):

0 commit comments

Comments
 (0)