forked from Dawn-India/Z-Mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
73 lines (60 loc) · 2.46 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
from logging import FileHandler, StreamHandler, INFO, basicConfig, error as log_error, info as log_info
from os import path as ospath, environ
from subprocess import run as srun
from dotenv import load_dotenv
from pymongo import MongoClient
if ospath.exists('Z_Logs.txt'):
with open('Z_Logs.txt', 'r+') as f:
f.truncate(0)
basicConfig(format='%(asctime)s | %(name)s | %(levelname)s | %(message)s',
handlers=[FileHandler('Z_Logs.txt'), StreamHandler()],
level=INFO)
load_dotenv('config.env', override=True)
try:
if bool(environ.get('_____REMOVE_THIS_LINE_____')):
log_error('The README.md file there to be read! Exiting now!')
exit()
except:
pass
BOT_TOKEN = environ.get('BOT_TOKEN', '')
if len(BOT_TOKEN) == 0:
log_error("BOT_TOKEN variable is missing! Exiting now")
exit(1)
bot_id = BOT_TOKEN.split(':', 1)[0]
DATABASE_URL = environ.get('DATABASE_URL', '')
if len(DATABASE_URL) == 0:
DATABASE_URL = None
if DATABASE_URL:
conn = MongoClient(DATABASE_URL)
db = conn.z
# retrun config dict (all env vars)
if config_dict := db.settings.config.find_one({'_id': bot_id}):
environ['UPSTREAM_REPO'] = config_dict['UPSTREAM_REPO']
environ['UPSTREAM_BRANCH'] = config_dict['UPSTREAM_BRANCH']
conn.close()
UPSTREAM_REPO = environ.get('UPSTREAM_REPO', '')
if len(UPSTREAM_REPO) == 0:
UPSTREAM_REPO = None
UPSTREAM_BRANCH = environ.get('UPSTREAM_BRANCH', '')
if len(UPSTREAM_BRANCH) == 0:
UPSTREAM_BRANCH = ''
if UPSTREAM_REPO:
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 Dawn-India \
&& 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.')
log_info(f'Repo in use: {UPSTREAM_REPO}')
log_info(f'Branch in use: {UPSTREAM_BRANCH}')
log_info('Thanks For Using Z_Mirror')
else:
log_error('Something went wrong while updating.')
log_info('Check if entered UPSTREAM_REPO is valid or not!')
log_info(f'Entered upstream repo: {UPSTREAM_REPO}')