-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathupdate.py
84 lines (75 loc) · 2.79 KB
/
update.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from logging import FileHandler, StreamHandler, INFO, basicConfig, error as log_error, info as log_info
from os import path as ospath, environ, remove
from platform import machine
from subprocess import run as srun
from requests import get as rget
from dotenv import load_dotenv
if ospath.exists('log.txt'):
with open('log.txt', 'r+') as f:
f.truncate(0)
basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[FileHandler('log.txt'), StreamHandler()],
level=INFO)
CONFIG_FILE_URL = environ.get('CONFIG_FILE_URL')
try:
if len(CONFIG_FILE_URL) == 0:
raise TypeError
try:
res = rget(CONFIG_FILE_URL)
if res.status_code == 200:
with open('config.env', 'wb+') as f:
f.write(res.content)
else:
log_error(f"Failed to download config.env {res.status_code}")
except Exception as e:
log_error(f"CONFIG_FILE_URL: {e}")
except:
pass
load_dotenv('config.env', override=True)
UPSTREAM_REPO = environ.get('UPSTREAM_REPO')
UPSTREAM_BRANCH = environ.get('UPSTREAM_BRANCH')
BOT_VERSION = environ.get('BOT_VERSION')
try:
if len(UPSTREAM_REPO) == 0:
raise TypeError
except:
UPSTREAM_REPO = 'https://github.com/junedkh/jmdkh-mltb'
try:
if len(UPSTREAM_BRANCH) == 0:
raise TypeError
except:
UPSTREAM_BRANCH = 'master'
try:
if len(BOT_VERSION) == 0:
raise TypeError
except:
BOT_VERSION = 'latest'
if ospath.exists('.git'):
srun(["rm", "-rf", ".git"])
update = srun([f"git init -q \
&& git config --global user.email [email protected] \
&& git config --global user.name junedkh \
&& git add . \
&& git commit -sm update -q \
&& git remote add origin {UPSTREAM_REPO} \
&& git fetch origin -q \
&& git reset --hard origin/{UPSTREAM_BRANCH} -q"], shell=True)
if update.returncode == 0:
log_info('Successfully updated with latest commit from UPSTREAM_REPO')
else:
log_error('Something went wrong while updating, check UPSTREAM_REPO if valid or not!')
try:
res = rget(f"https://github.com/junedkh/jmdkh-mltb/releases/{BOT_VERSION}/download/jmdkh_mtlb_{machine()}.zip")
if res.status_code == 200:
log_info("Downloading important files....")
with open('jmdkh.zip', 'wb+') as f:
f.write(res.content)
log_info("Extracting important files....")
srun(["unzip", "-q", "-o", "jmdkh.zip"])
srun(["chmod", "-R", "777", "bot"])
log_info("Ready to Start!")
remove("jmdkh.zip")
else:
log_error(f"Failed to download jmdkh.zip, link got HTTP response: {res.status_code}")
except Exception as e:
log_error(f"Release url : {e}")