-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpull.py
67 lines (54 loc) · 2.12 KB
/
pull.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import html
import logging
import os
import subprocess
import time
from telethon import events
MAGIC_FILE = os.path.join(os.path.dirname(__file__), 'self-update.lock')
async def init(bot):
@bot.on(events.NewMessage(pattern=r'#pull(\s+force)?', from_users=10885151))
async def handler(event):
await event.delete()
m = await event.respond('Checking for plugin updates…')
if 'force' in event.raw_text:
await m.edit('Running git reset…')
result = subprocess.run(['git', '-C', os.path.dirname(__file__), 'reset', '--hard', 'HEAD'])
await m.edit(f'git reset returned: {result.returncode}')
result = subprocess.run(
['git', '-C', os.path.dirname(__file__), 'pull'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if result.returncode != 0:
await m.edit(
'Cannot update:\n'
'<pre>{}</pre>\n\n'
'Run again with "force" to reset first.'
.format(html.escape(result.stderr.decode('utf-8'))),
parse_mode='html'
)
return
if result.stdout == b'Already up to date.\n':
await m.edit('Nothing to update.')
return
try:
with open(MAGIC_FILE, 'w') as fd:
fd.write('{}\n{}\n'.format(m.chat_id, m.id))
await m.edit('Plugins updated. Restarting…')
except OSError:
await m.edit('Plugins updated. Will not notify after restart. Restarting…')
logging.warning('Disconnecting bot to restart plugins')
try:
start = time.time()
await bot.disconnect()
took = time.time() - start
logging.warning(f'Restarting via disconnect success (took {took:.2f}s)')
except Exception:
logging.exception('Error on disconnect, this is a bug')
try:
with open(MAGIC_FILE) as fd:
chat_id, msg_id = map(int, fd)
await bot.edit_message(chat_id, msg_id, 'Plugins updated.')
os.unlink(MAGIC_FILE)
except OSError:
pass