-
Notifications
You must be signed in to change notification settings - Fork 503
/
update.py
154 lines (138 loc) · 3.39 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from dotenv import (
load_dotenv,
dotenv_values
)
from logging import (
ERROR,
INFO,
basicConfig,
error as log_error,
FileHandler,
getLogger,
info as log_info,
StreamHandler,
)
from os import (
environ,
path,
remove
)
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from subprocess import run as urun
from sys import exit
getLogger("pymongo").setLevel(ERROR)
if path.exists("Zee_Logs.txt"):
with open(
"Zee_Logs.txt",
"r+"
) as f:
f.truncate(0)
if path.exists("rlog.txt"):
remove("rlog.txt")
basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
FileHandler("Zee_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(1)
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 is not None:
try:
conn = MongoClient(
DATABASE_URL,
server_api=ServerApi("1")
)
db = conn.zee
old_config = db.settings.deployConfig.find_one({"_id": BOT_ID})
config_dict = db.settings.config.find_one({"_id": BOT_ID})
if old_config is not None:
del old_config["_id"]
if (
old_config is not None
and old_config == dict(dotenv_values("config.env"))
or old_config is None
) and config_dict is not None:
environ["UPSTREAM_REPO"] = config_dict["UPSTREAM_REPO"]
environ["UPSTREAM_BRANCH"] = config_dict["UPSTREAM_BRANCH"]
conn.close()
except Exception as e:
log_error(f"Database ERROR: {e}")
UPSTREAM_REPO = environ.get(
"UPSTREAM_REPO",
""
)
if len(UPSTREAM_REPO) == 0:
UPSTREAM_REPO = "https://github.com/Dawn-India/Z-Mirror"
UPSTREAM_BRANCH = environ.get(
"UPSTREAM_BRANCH",
""
)
if len(UPSTREAM_BRANCH) == 0:
UPSTREAM_BRANCH = "main"
if UPSTREAM_REPO is not None:
if path.exists(".git"):
urun([
"rm",
"-rf",
".git"
])
update = urun(
[
f"git init -q \
&& git config --global user.email [email protected] \
&& git config --global user.name zee \
&& 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...")
log_info("Thanks For Using @Z_Mirror")
else:
log_error("Error while getting latest updates.")
log_error("Check if entered UPSTREAM_REPO is valid or not!")
urun(
[
"rm",
"-rf",
"py_generators",
"config_sample.env",
"Dockerfile",
"LICENSE",
"README.md",
"requirements.txt"
]
)