privateone/ONesVC_bot
0
1#2# Copyright (C) 2021-2022 by TeamYukki@Github, < https://github.com/TeamYukki >.3#4# This file is part of < https://github.com/TeamYukki/YukkiMusicBot > project,5# and is released under the "GNU v3.0 License Agreement".6# Please see < https://github.com/TeamYukki/YukkiMusicBot/blob/master/LICENSE >7#8# All rights reserved.9 10import asyncio11import shlex12from typing import Tuple13 14from git import Repo15from git.exc import GitCommandError, InvalidGitRepositoryError16 17import config18 19from ..logging import LOGGER20 21 22def install_req(cmd: str) -> Tuple[str, str, int, int]:23 async def install_requirements():24 args = shlex.split(cmd)25 process = await asyncio.create_subprocess_exec(26 *args,27 stdout=asyncio.subprocess.PIPE,28 stderr=asyncio.subprocess.PIPE,29 )30 stdout, stderr = await process.communicate()31 return (32 stdout.decode("utf-8", "replace").strip(),33 stderr.decode("utf-8", "replace").strip(),34 process.returncode,35 process.pid,36 )37 38 return asyncio.get_event_loop().run_until_complete(39 install_requirements()40 )41 42 43def git():44 REPO_LINK = config.UPSTREAM_REPO45 if config.GIT_TOKEN:46 GIT_USERNAME = REPO_LINK.split("com/")[1].split("/")[0]47 TEMP_REPO = REPO_LINK.split("https://")[1]48 UPSTREAM_REPO = (49 f"https://{GIT_USERNAME}:{config.GIT_TOKEN}@{TEMP_REPO}"50 )51 else:52 UPSTREAM_REPO = config.UPSTREAM_REPO53 try:54 repo = Repo()55 LOGGER(__name__).info(f"Git Client Found [VPS DEPLOYER]")56 except GitCommandError:57 LOGGER(__name__).info(f"Invalid Git Command")58 except InvalidGitRepositoryError:59 repo = Repo.init()60 if "origin" in repo.remotes:61 origin = repo.remote("origin")62 else:63 origin = repo.create_remote("origin", UPSTREAM_REPO)64 origin.fetch()65 repo.create_head(66 config.UPSTREAM_BRANCH,67 origin.refs[config.UPSTREAM_BRANCH],68 )69 repo.heads[config.UPSTREAM_BRANCH].set_tracking_branch(70 origin.refs[config.UPSTREAM_BRANCH]71 )72 repo.heads[config.UPSTREAM_BRANCH].checkout(True)73 try:74 repo.create_remote("origin", config.UPSTREAM_REPO)75 except BaseException:76 pass77 nrs = repo.remote("origin")78 nrs.fetch(config.UPSTREAM_BRANCH)79 try:80 nrs.pull(config.UPSTREAM_BRANCH)81 except GitCommandError:82 repo.git.reset("--hard", "FETCH_HEAD")83 install_req("pip3 install --no-cache-dir -r requirements.txt")84 LOGGER(__name__).info(f"Fetched Updates from: {REPO_LINK}")85 