Skip to content
This repository was archived by the owner on Sep 29, 2021. It is now read-only.

Commit a8d14b5

Browse files
committed
Userbot: Update Latest from Official Repos
1 parent 0bbd8ef commit a8d14b5

File tree

4 files changed

+237
-41
lines changed

4 files changed

+237
-41
lines changed

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
{
174174
"plan": "heroku-postgresql",
175175
"options": {
176-
"version": "9.5"
176+
"version": "9.9.9"
177177
}
178178
}
179179
]

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ google_images_download>=2.7.1
1515
gTTS>=2.0.1
1616
gTTS-token>=1.1.3
1717
hachoir
18+
heroku
19+
heroku3
1820
httplib2==0.15.0
1921
humanize
2022
lxml

userbot/modules/changelog.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright (C) 2019 The Raphielscape Company LLC.
2+
#
3+
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
4+
# you may not use this file except in compliance with the License.
5+
#
6+
"""
7+
This module updates the userbot based on chtream revision
8+
"""
9+
10+
from os import remove, execl
11+
import sys
12+
13+
from git import Repo
14+
from git.exc import GitCommandError, InvalidGitRepositoryError, NoSuchPathError
15+
16+
from userbot import CMD_HELP, bot, HEROKU_MEMEZ, HEROKU_APIKEY, HEROKU_APPNAME
17+
from userbot.events import register
18+
19+
20+
async def gen_chlog(repo, diff):
21+
ch_log = ''
22+
d_form = "%d/%m/%y"
23+
for c in repo.iter_commits(diff):
24+
ch_log += f'•[{c.committed_datetime.strftime(d_form)}]: {c.summary} <{c.author}>\n'
25+
return ch_log
26+
27+
28+
async def is_off_br(br):
29+
off_br = ['sql-extended']
30+
for k in off_br:
31+
if k == br:
32+
return 1
33+
return
34+
35+
36+
@register(outgoing=True, pattern="^.chl(?: |$)(.*)")
37+
async def chtream(ch):
38+
"For .update command, check if the bot is up to date, update if specified"
39+
await ch.edit("`Checking for updates, please wait....`")
40+
conf = ch.pattern_match.group(1).lower()
41+
off_repo = 'https://github.com/MoveAngel/One4uBot.git'
42+
43+
try:
44+
txt = "`Oops.. Updater cannot continue due to some problems occured`\n\n**LOGTRACE:**\n"
45+
repo = Repo()
46+
except NoSuchPathError as error:
47+
await ch.edit(f'{txt}\n`directory {error} is not found`')
48+
return
49+
except GitCommandError as error:
50+
await ch.edit(f'{txt}\n`Early failure! {error}`')
51+
return
52+
except InvalidGitRepositoryError:
53+
repo = Repo.init()
54+
await ch.edit(
55+
"`Warning: Force-Syncing to the latest stable code from repo.`\
56+
\nI may lose my downloaded files during this update."
57+
)
58+
origin = repo.create_remote('chtream', off_repo)
59+
origin.fetch()
60+
repo.create_head('sql-extended', origin.refs.master)
61+
repo.heads.master.checkout(True)
62+
63+
ac_br = repo.active_branch.name
64+
if not await is_off_br(ac_br):
65+
await ch.edit(
66+
f'**[UPDATER]:**` Looks like you are using your own custom branch ({ac_br}). \
67+
in that case, Updater is unable to identify which branch is to be merged. \
68+
please checkout to any official branch`')
69+
return
70+
71+
try:
72+
repo.create_remote('chtream', off_repo)
73+
except BaseException:
74+
pass
75+
76+
ch_rem = repo.remote('chtream')
77+
ch_rem.fetch(ac_br)
78+
changelog = await gen_chlog(repo, f'HEAD..chtream/{ac_br}')
79+
80+
if not changelog:
81+
await ch.edit(f'\n`Your BOT is` **up-to-date** `with` **{ac_br}**\n')
82+
return
83+
84+
if conf != "w":
85+
changelog_str = f'**New UPDATE available for [{ac_br}]:\n\nCHANGELOG:**\n`{changelog}`'
86+
if len(changelog_str) > 4096:
87+
await ch.edit("`Changelog is too big, sending it as a file.`")
88+
file = open("output.txt", "w+")
89+
file.write(changelog_str)
90+
file.close()
91+
await ch.client.send_file(
92+
ch.chat_id,
93+
"output.txt",
94+
reply_to=ch.id,
95+
)
96+
remove("output.txt")
97+
else:
98+
await ch.edit(changelog_str)
99+
await ch.respond(
100+
"`do \".update \" to update\nif using Heroku`")
101+
return
102+
103+
await ch.edit('`New update found, updating...`')
104+
ch_rem.fetch(ac_br)
105+
await ch.edit('`Successfully Updated!\n'
106+
'Bot is restarting... Wait for a second!`')
107+
await install_requirements()
108+
await bot.disconnect()
109+
# Spin a new instance of bot
110+
execl(sys.executable, sys.executable, *sys.argv)
111+
# Shut the existing one down
112+
exit()
113+
114+
115+
CMD_HELP.update({
116+
'update':
117+
".chl\
118+
\nUsage: Checks if the main userbot repository has any updates and shows a changelog if so.\
119+
\n\n.update\
120+
\nUsage: Updates your userbot, if there are any updates in the main userbot repository."
121+
})

userbot/modules/updater.py

Lines changed: 113 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
#
33
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
44
# you may not use this file except in compliance with the License.
5+
# credits to @AvinashReddy3108
56
#
67
"""
78
This module updates the userbot based on Upstream revision
89
"""
910

10-
from os import remove, execl
11+
from os import remove, execle, path, makedirs, getenv, environ
12+
from shutil import rmtree
13+
import asyncio
1114
import sys
1215

1316
from git import Repo
1417
from git.exc import GitCommandError, InvalidGitRepositoryError, NoSuchPathError
1518

16-
from userbot import CMD_HELP, bot, HEROKU_MEMEZ, HEROKU_APIKEY, HEROKU_APPNAME
19+
from userbot import CMD_HELP, bot, HEROKU_APIKEY, HEROKU_APPNAME, UPSTREAM_REPO_URL
1720
from userbot.events import register
1821

22+
requirements_path = path.join(
23+
path.dirname(path.dirname(path.dirname(__file__))), 'requirements.txt')
24+
1925

2026
async def gen_chlog(repo, diff):
2127
ch_log = ''
@@ -25,47 +31,62 @@ async def gen_chlog(repo, diff):
2531
return ch_log
2632

2733

28-
async def is_off_br(br):
29-
off_br = ['sql-extended']
30-
for k in off_br:
31-
if k == br:
32-
return 1
33-
return
34+
async def update_requirements():
35+
reqs = str(requirements_path)
36+
try:
37+
process = await asyncio.create_subprocess_shell(
38+
' '.join([sys.executable, "-m", "pip", "install", "-r", reqs]),
39+
stdout=asyncio.subprocess.PIPE,
40+
stderr=asyncio.subprocess.PIPE)
41+
await process.communicate()
42+
return process.returncode
43+
except Exception as e:
44+
return repr(e)
3445

3546

36-
@register(outgoing=True, pattern="^.update(?: |$)(.*)")
47+
@register(outgoing=True, pattern="^\.update(?: |$)(.*)")
3748
async def upstream(ups):
3849
"For .update command, check if the bot is up to date, update if specified"
3950
await ups.edit("`Checking for updates, please wait....`")
40-
conf = ups.pattern_match.group(1).lower()
41-
off_repo = 'https://github.com/MoveAngel/One4uBot.git'
51+
conf = ups.pattern_match.group(1)
52+
off_repo = UPSTREAM_REPO_URL
53+
force_update = False
4254

4355
try:
44-
txt = "`Oops.. Updater cannot continue due to some problems occured`\n\n**LOGTRACE:**\n"
56+
txt = "`Oops.. Updater cannot continue due to "
57+
txt += "some problems occured`\n\n**LOGTRACE:**\n"
4558
repo = Repo()
4659
except NoSuchPathError as error:
4760
await ups.edit(f'{txt}\n`directory {error} is not found`')
61+
repo.__del__()
4862
return
4963
except GitCommandError as error:
5064
await ups.edit(f'{txt}\n`Early failure! {error}`')
65+
repo.__del__()
5166
return
52-
except InvalidGitRepositoryError:
67+
except InvalidGitRepositoryError as error:
68+
if conf != "now":
69+
await ups.edit(
70+
f"`Unfortunately, the directory {error} does not seem to be a git repository.\
71+
\nBut we can fix that by force updating the userbot using .update now.`"
72+
)
73+
return
5374
repo = Repo.init()
54-
await ups.edit(
55-
"`Warning: Force-Syncing to the latest stable code from repo.`\
56-
\nI may lose my downloaded files during this update."
57-
)
5875
origin = repo.create_remote('upstream', off_repo)
5976
origin.fetch()
60-
repo.create_head('sql-extended', origin.refs.master)
61-
repo.heads.master.checkout(True)
77+
force_update = True
78+
repo.create_head('sql-extended', origin.refs.sql-extended)
79+
repo.heads.sql-extended.set_tracking_branch(origin.refs.sql-extended)
80+
repo.heads.sql-extended.checkout(True)
6281

6382
ac_br = repo.active_branch.name
64-
if not await is_off_br(ac_br):
83+
if ac_br != 'sql-extended':
6584
await ups.edit(
66-
f'**[UPDATER]:**` Looks like you are using your own custom branch ({ac_br}). \
67-
in that case, Updater is unable to identify which branch is to be merged. \
68-
please checkout to any official branch`')
85+
f'**[UPDATER]:**` Looks like you are using your own custom branch ({ac_br}). '
86+
'in that case, Updater is unable to identify '
87+
'which branch is to be merged. '
88+
'please checkout to any official branch`')
89+
repo.__del__()
6990
return
7091

7192
try:
@@ -75,16 +96,19 @@ async def upstream(ups):
7596

7697
ups_rem = repo.remote('upstream')
7798
ups_rem.fetch(ac_br)
99+
78100
changelog = await gen_chlog(repo, f'HEAD..upstream/{ac_br}')
79101

80-
if not changelog:
81-
await ups.edit(f'\n`Your BOT is` **up-to-date** `with` **{ac_br}**\n')
102+
if not changelog and not force_update:
103+
await ups.edit(
104+
f'\n`Your BOT is` **up-to-date** `with` **{ac_br}**\n')
105+
repo.__del__()
82106
return
83107

84-
if conf != "now":
108+
if conf != "now" and not force_update:
85109
changelog_str = f'**New UPDATE available for [{ac_br}]:\n\nCHANGELOG:**\n`{changelog}`'
86110
if len(changelog_str) > 4096:
87-
await ups.edit("`Changelog is too big, sending it as a file.`")
111+
await ups.edit("`Changelog is too big, view the file to see it.`")
88112
file = open("output.txt", "w+")
89113
file.write(changelog_str)
90114
file.close()
@@ -96,20 +120,69 @@ async def upstream(ups):
96120
remove("output.txt")
97121
else:
98122
await ups.edit(changelog_str)
99-
await ups.respond(
100-
"`do \".update now\" to update\nDon't if using Heroku`")
123+
await ups.respond('`do \".update now\" to update`')
101124
return
102125

103-
await ups.edit('`New update found, updating...`')
104-
ups_rem.fetch(ac_br)
105-
await ups.edit('`Successfully Updated!\n'
106-
'Bot is restarting... Wait for a second!`')
107-
await install_requirements()
108-
await bot.disconnect()
109-
# Spin a new instance of bot
110-
execl(sys.executable, sys.executable, *sys.argv)
111-
# Shut the existing one down
112-
exit()
126+
if force_update:
127+
await ups.edit(
128+
'`Force-Syncing to latest stable userbot code, please wait...`')
129+
else:
130+
await ups.edit('`Updating userbot, please wait....`')
131+
# We're in a Heroku Dyno, handle it's memez.
132+
if HEROKU_APIKEY is not None:
133+
import heroku3
134+
heroku = heroku3.from_key(HEROKU_APIKEY)
135+
heroku_app = None
136+
heroku_applications = heroku.apps()
137+
if not HEROKU_APPNAME:
138+
await ups.edit(
139+
'`[HEROKU MEMEZ] Please set up the HEROKU_APPNAME variable to be able to update userbot.`'
140+
)
141+
repo.__del__()
142+
return
143+
for app in heroku_applications:
144+
if app.name == HEROKU_APPNAME:
145+
heroku_app = app
146+
break
147+
if heroku_app is None:
148+
await ups.edit(
149+
f'{txt}\n`Invalid Heroku credentials for updating userbot dyno.`'
150+
)
151+
repo.__del__()
152+
return
153+
await ups.edit('`[HEROKU MEMEZ]\
154+
\nUserbot dyno build in progress, please wait for it to complete.`'
155+
)
156+
ups_rem.fetch(ac_br)
157+
repo.git.reset("--hard", "FETCH_HEAD")
158+
heroku_git_url = heroku_app.git_url.replace(
159+
"https://", "https://api:" + HEROKU_APIKEY + "@")
160+
if "heroku" in repo.remotes:
161+
remote = repo.remote("heroku")
162+
remote.set_url(heroku_git_url)
163+
else:
164+
remote = repo.create_remote("heroku", heroku_git_url)
165+
try:
166+
remote.push(refspec="HEAD:refs/heads/sql-extended", force=True)
167+
except GitCommandError as error:
168+
await ups.edit(f'{txt}\n`Here is the error log:\n{error}`')
169+
repo.__del__()
170+
return
171+
await ups.edit('`Successfully Updated!\n'
172+
'Restarting, please wait...`')
173+
else:
174+
# Classic Updater, pretty straightforward.
175+
try:
176+
ups_rem.pull(ac_br)
177+
except GitCommandError:
178+
repo.git.reset("--hard", "FETCH_HEAD")
179+
reqs_upgrade = await update_requirements()
180+
await ups.edit('`Successfully Updated!\n'
181+
'Bot is restarting... Wait for a second!`')
182+
# Spin a new instance of bot
183+
args = [sys.executable, "-m", "userbot"]
184+
execle(sys.executable, *args, environ)
185+
return
113186

114187

115188
CMD_HELP.update({
@@ -118,4 +191,4 @@ async def upstream(ups):
118191
\nUsage: Checks if the main userbot repository has any updates and shows a changelog if so.\
119192
\n\n.update now\
120193
\nUsage: Updates your userbot, if there are any updates in the main userbot repository."
121-
})
194+
})

0 commit comments

Comments
 (0)