2020-02-19 15:31:39 +00:00
|
|
|
""" Pulls in the new version of PagerMaid from the git server. """
|
|
|
|
|
2021-07-16 05:23:50 +00:00
|
|
|
import platform, json, time
|
|
|
|
import urllib.request
|
2021-08-18 08:41:22 +00:00
|
|
|
from distutils.util import strtobool
|
2021-07-16 05:23:50 +00:00
|
|
|
from json import JSONDecodeError
|
2020-09-20 02:53:55 +00:00
|
|
|
from subprocess import run, PIPE
|
|
|
|
from datetime import datetime
|
2020-02-19 15:31:39 +00:00
|
|
|
from os import remove
|
|
|
|
from git import Repo
|
|
|
|
from git.exc import GitCommandError, InvalidGitRepositoryError, NoSuchPathError
|
2021-07-02 12:13:22 +00:00
|
|
|
from sys import executable
|
2021-07-16 05:23:50 +00:00
|
|
|
from pagermaid import log, config
|
2020-02-19 15:31:39 +00:00
|
|
|
from pagermaid.listener import listener
|
2021-06-15 04:31:05 +00:00
|
|
|
from pagermaid.utils import execute, lang, alias_command
|
2020-02-19 15:31:39 +00:00
|
|
|
|
2021-07-16 05:23:50 +00:00
|
|
|
try:
|
|
|
|
git_api = config['git_api']
|
|
|
|
git_ssh = config['git_ssh']
|
2021-07-17 06:05:38 +00:00
|
|
|
need_update_check = strtobool(config['update_check'])
|
|
|
|
update_time = config['update_time']
|
|
|
|
update_username = config['update_username']
|
|
|
|
update_delete = strtobool(config['update_delete'])
|
2021-07-16 05:23:50 +00:00
|
|
|
except KeyError:
|
2021-07-17 06:05:38 +00:00
|
|
|
git_api = "https://api.github.com/repos/Xtao-Labs/PagerMaid-Modify/commits/master"
|
2021-07-16 05:23:50 +00:00
|
|
|
git_ssh = 'https://github.com/Xtao-Labs/PagerMaid-Modify.git'
|
2021-07-17 06:05:38 +00:00
|
|
|
need_update_check = True
|
|
|
|
update_time = 86400
|
|
|
|
update_username = 'self'
|
|
|
|
update_delete = True
|
|
|
|
try:
|
|
|
|
update_time = int(update_time)
|
|
|
|
except ValueError:
|
|
|
|
update_time = 86400
|
|
|
|
try:
|
|
|
|
update_username = int(update_username)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2021-07-16 05:23:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_get():
|
|
|
|
with urllib.request.urlopen(git_api) as response:
|
|
|
|
result = response.read()
|
|
|
|
try:
|
|
|
|
data = json.loads(result)
|
|
|
|
except JSONDecodeError as e:
|
|
|
|
raise e
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
update_get_time = 0
|
2021-07-17 06:05:38 +00:00
|
|
|
update_id = 0
|
2021-07-16 05:23:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@listener(incoming=True, ignore_edited=True)
|
|
|
|
async def update_refresher(context):
|
2021-07-17 06:05:38 +00:00
|
|
|
global update_get_time, update_id
|
2021-07-16 05:23:50 +00:00
|
|
|
if not need_update_check:
|
|
|
|
return
|
2021-07-17 06:05:38 +00:00
|
|
|
if time.time() - update_get_time > update_time:
|
2021-07-16 05:23:50 +00:00
|
|
|
update_get_time = time.time()
|
|
|
|
changelog = None
|
|
|
|
try:
|
|
|
|
repo = Repo()
|
|
|
|
active_branch = repo.active_branch.name
|
|
|
|
if not await branch_check(active_branch):
|
|
|
|
return
|
|
|
|
repo.create_remote('upstream', git_ssh)
|
|
|
|
upstream_remote = repo.remote('upstream')
|
|
|
|
upstream_remote.fetch(active_branch)
|
|
|
|
changelog = await changelog_gen(repo, f'HEAD..upstream/{active_branch}')
|
|
|
|
if not changelog:
|
|
|
|
return
|
|
|
|
else:
|
2021-07-17 06:05:38 +00:00
|
|
|
if update_username == 'self':
|
|
|
|
user = await context.client.get_me(input_peer=True)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
user = await context.client.get_input_entity(update_username)
|
|
|
|
except ValueError:
|
|
|
|
user = await context.client.get_me(input_peer=True)
|
|
|
|
if not update_id == 0 and update_delete:
|
|
|
|
try:
|
|
|
|
await context.client.delete_messages(user, update_id)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
msg = await context.client.send_message(user,
|
|
|
|
f'**{lang("update_found_update_in_branch")} '
|
|
|
|
f'{active_branch}.\n\n'
|
|
|
|
f'{lang("update_change_log")}:**\n`{changelog}`')
|
|
|
|
update_id = msg.id
|
|
|
|
except:
|
|
|
|
pass
|
2021-07-16 05:23:50 +00:00
|
|
|
except:
|
|
|
|
try:
|
|
|
|
data = update_get()
|
|
|
|
git_hash = run("git rev-parse HEAD", stdout=PIPE, shell=True).stdout.decode().strip()
|
|
|
|
if not data['sha'] == git_hash:
|
2021-07-17 06:05:38 +00:00
|
|
|
if update_username == 'self':
|
|
|
|
user = await context.client.get_me(input_peer=True)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
user = await context.client.get_input_entity(update_username)
|
|
|
|
except ValueError:
|
|
|
|
user = await context.client.get_me(input_peer=True)
|
2021-07-16 05:23:50 +00:00
|
|
|
changelog = data['commit']['message']
|
2021-07-17 06:05:38 +00:00
|
|
|
if not update_id == 0 and update_delete:
|
|
|
|
try:
|
|
|
|
await context.client.delete_messages(user, update_id)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
msg = await context.client.send_message(user, f'**{lang("update_found_update_in_branch")} '
|
|
|
|
f'master.\n\n'
|
|
|
|
f'{lang("update_change_log")}:**\n`{changelog}`')
|
|
|
|
update_id = msg.id
|
|
|
|
except:
|
|
|
|
pass
|
2021-07-16 05:23:50 +00:00
|
|
|
except Exception as e:
|
2021-07-17 06:05:38 +00:00
|
|
|
await log(f"Warning: module update failed to refresh git commit data.\n{e}")
|
2021-07-16 05:23:50 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("update"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('update_des'),
|
2020-09-20 02:53:55 +00:00
|
|
|
parameters="<true/debug>")
|
2020-02-19 15:31:39 +00:00
|
|
|
async def update(context):
|
|
|
|
if len(context.parameter) > 1:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('update_processing'))
|
2020-02-19 15:31:39 +00:00
|
|
|
parameter = None
|
2021-06-16 06:17:11 +00:00
|
|
|
changelog = None
|
2020-02-19 15:31:39 +00:00
|
|
|
if len(context.parameter) == 1:
|
|
|
|
parameter = context.parameter[0]
|
|
|
|
|
2020-09-20 02:53:55 +00:00
|
|
|
if parameter:
|
|
|
|
if parameter == "debug":
|
|
|
|
# Version info
|
2021-06-15 16:51:34 +00:00
|
|
|
git_version = run("git --version", stdout=PIPE, shell=True).stdout.decode().strip().replace("git version ",
|
|
|
|
"")
|
2020-09-20 02:53:55 +00:00
|
|
|
git_change = bool(run("git diff-index HEAD --", stdout=PIPE, shell=True).stdout.decode().strip())
|
|
|
|
git_change = "是" if git_change else "否"
|
|
|
|
git_date = run("git log -1 --format='%at'", stdout=PIPE, shell=True).stdout.decode()
|
|
|
|
git_date = datetime.utcfromtimestamp(int(git_date)).strftime("%Y/%m/%d %H:%M:%S")
|
|
|
|
git_hash = run("git rev-parse --short HEAD", stdout=PIPE, shell=True).stdout.decode().strip()
|
|
|
|
get_hash_link = f"https://github.com/xtaodada/PagerMaid-Modify/commit/{git_hash}"
|
|
|
|
# Generate the text
|
2021-07-16 05:23:50 +00:00
|
|
|
text = f"{lang('status_platform')}: {str(platform.platform())}\n" \
|
|
|
|
f"{lang('update_platform_version')}: {str(platform.version())}\n" \
|
|
|
|
f"{lang('status_python')}: {str(platform.python_version())}\n" \
|
|
|
|
f"{lang('update_git_version')}: {git_version}\n" \
|
|
|
|
f"{lang('update_local_git_change')}: {git_change}\n" \
|
|
|
|
f"{lang('update_hash')}: [{git_hash}]({get_hash_link})\n" \
|
|
|
|
f"{lang('update_date')}: {git_date} "
|
2020-09-20 02:53:55 +00:00
|
|
|
await context.edit(text)
|
|
|
|
return
|
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
try:
|
|
|
|
repo = Repo()
|
|
|
|
except NoSuchPathError as exception:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(f"{lang('update_NoSuchPathError')} {exception}")
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
except InvalidGitRepositoryError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('update_InvalidGitRepositoryError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
except GitCommandError as exception:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(f'{lang("update_GitCommandError")} :`{exception}`')
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
active_branch = repo.active_branch.name
|
|
|
|
if not await branch_check(active_branch):
|
|
|
|
await context.edit(
|
2021-04-12 16:25:32 +00:00
|
|
|
f"{lang('update_not_active_branch')}: {active_branch}.")
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-07-16 05:23:50 +00:00
|
|
|
repo.create_remote('upstream', git_ssh)
|
2020-02-19 15:31:39 +00:00
|
|
|
except BaseException:
|
|
|
|
pass
|
2021-06-19 07:25:31 +00:00
|
|
|
try:
|
|
|
|
upstream_remote = repo.remote('upstream')
|
|
|
|
upstream_remote.fetch(active_branch)
|
|
|
|
except GitCommandError:
|
|
|
|
await context.edit(lang('update_failed'))
|
|
|
|
return
|
2021-04-13 07:32:34 +00:00
|
|
|
try:
|
|
|
|
changelog = await changelog_gen(repo, f'HEAD..upstream/{active_branch}')
|
|
|
|
except:
|
|
|
|
distribution = await execute('lsb_release -a')
|
|
|
|
if distribution.find('Ubuntu') != -1 or distribution.find('Debain') != -1:
|
|
|
|
try:
|
|
|
|
await execute('apt-get install --upgrade git -y')
|
|
|
|
except:
|
|
|
|
await context.edit(lang('update_failed') + '\n' + lang('update_auto_upgrade_git_failed_ubuntu'))
|
|
|
|
return
|
|
|
|
elif distribution.find('Cent') != -1:
|
|
|
|
try:
|
|
|
|
await execute('yum install git -y')
|
|
|
|
except:
|
|
|
|
await context.edit(lang('update_failed') + '\n' + lang('update_auto_upgrade_git_failed_cent'))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
await execute('apt-get install --upgrade git -y')
|
|
|
|
await execute('yum install git -y')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
await context.edit(lang('update_auto_upgrade_git_hint'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
2020-09-20 02:53:55 +00:00
|
|
|
if not parameter:
|
|
|
|
if not changelog:
|
2021-06-15 16:51:34 +00:00
|
|
|
await context.edit(
|
|
|
|
f"`PagerMaid-Modify {lang('update_in_branch')} ` **{active_branch}**` {lang('update_is_updated')}`")
|
2020-09-20 02:53:55 +00:00
|
|
|
return
|
2021-04-12 16:25:32 +00:00
|
|
|
changelog_str = f'**{lang("update_found_update_in_branch")} {active_branch}.\n\n{lang("update_change_log")}:**\n`{changelog}`'
|
2020-02-19 15:31:39 +00:00
|
|
|
if len(changelog_str) > 4096:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('update_log_too_big'))
|
2020-02-19 15:31:39 +00:00
|
|
|
file = open("output.log", "w+")
|
|
|
|
file.write(changelog_str)
|
|
|
|
file.close()
|
|
|
|
await context.client.send_file(
|
|
|
|
context.chat_id,
|
|
|
|
"output.log",
|
|
|
|
reply_to=context.id,
|
|
|
|
)
|
|
|
|
remove("output.log")
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(changelog_str + f"\n**{lang('update_hint')}**\n`-update true`")
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('update_found_pulling'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
try:
|
2020-08-09 06:11:04 +00:00
|
|
|
try:
|
|
|
|
upstream_remote.pull(active_branch)
|
|
|
|
except:
|
2021-06-15 16:51:34 +00:00
|
|
|
await execute("""git status | grep modified | sed -r "s/ +/ /" | cut -f2 | awk -F " " '{print "mkdir -p
|
|
|
|
$(dirname ../for-update/" $2 ") && mv " $2 " ../for-update/" $2}' | sh""")
|
2020-08-09 06:11:04 +00:00
|
|
|
await execute("git pull")
|
2021-06-15 16:51:34 +00:00
|
|
|
await execute("""cd ../for-update/ && find -H . -type f | awk '{print "cp " $1 " ../PagerMaid-Modify/"
|
|
|
|
$1}' | sh && cd ../PagerMaid-Modify""")
|
2020-08-09 06:11:04 +00:00
|
|
|
await execute("rm -rf ../for-update/")
|
2021-07-02 12:13:22 +00:00
|
|
|
await execute(f"{executable} -m pip install -r requirements.txt --upgrade")
|
|
|
|
await execute(f"{executable} -m pip install -r requirements.txt")
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"PagerMaid-Modify {lang('update_is_updated')}")
|
|
|
|
await context.edit(lang('update_success') + lang('apt_reboot'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await context.client.disconnect()
|
|
|
|
except GitCommandError:
|
|
|
|
upstream_remote.git.reset('--hard')
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(lang('update_failed'))
|
|
|
|
await context.edit(lang('update_failed') + lang('apt_reboot'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await context.client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
async def changelog_gen(repo, diff):
|
|
|
|
result = ''
|
|
|
|
d_form = "%d/%m/%y"
|
|
|
|
for c in repo.iter_commits(diff):
|
|
|
|
result += f'•[{c.committed_datetime.strftime(d_form)}]: {c.summary} <{c.author}>\n'
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
async def branch_check(branch):
|
2021-07-17 06:05:38 +00:00
|
|
|
official = ['master', 'dev']
|
2020-02-19 15:31:39 +00:00
|
|
|
for k in official:
|
|
|
|
if k == branch:
|
|
|
|
return 1
|
|
|
|
return
|