-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
firstRun.py
128 lines (106 loc) · 3.79 KB
/
firstRun.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
"""
A script to run first-time setup for a Discord bot.
This script installs dependencies, prepares the database, fetches data from GitHub,
indexes MyAnimeList data from AnimeAPI, and copies .env.example to .env.
Usage: python3 firstRun.py
Example: python3 firstRun.py
"""
import asyncio
import os
import shlex
import subprocess
from modules.oobe.commons import (check_termux, current_os, prepare_database,
py_bin_path)
from modules.oobe.getNekomimi import nk_run
from modules.oobe.malIndexer import mal_run
from modules.oobe.migrate import migrate
class FirstRunError(Exception):
"""An exception class for first run script."""
async def first_run(py_bin: str = py_bin_path()):
"""
Runs the first run script.
Args:
py_bin (str, optional): Path to the Python binary. Defaults to py_bin_path().
Returns:
None
Raises:
Exception: If the script is not run from the root directory.
"""
# Check if the script is run from the root directory
if not os.path.exists("requirements.txt"):
raise FirstRunError("Please run the script from the repo's directory.")
match os.name:
case "nt":
safe_path = py_bin
case _:
safe_path = shlex.quote(py_bin)
try:
# Check if Termux is used
env = {"MATHLAB": "m"} if check_termux() else {}
# Install dependencies
print(
"Installing and upgrading dependencies for the next step and the bot itself..."
)
proc_args = [
safe_path,
"-m",
"pip",
"install",
"-U",
"-r",
"requirements.txt",
]
if current_os() == "Windows":
subprocess.run(proc_args, check=True)
else:
subprocess.run(proc_args, check=True, env=env)
except subprocess.CalledProcessError:
print("\033[31mError installing packages, please run frollowing command:")
command = "pip install -U -r requirements.txt"
if check_termux():
command = "MATHLAB=m " + command
print(f"{command}\033[0m")
# create a dummy file named cache/dict_installed, if it doesn't exist
if not os.path.exists("cache/dict_installed"):
print("Installing unidic dictionary from NINJAL...")
try:
subprocess.run(
[
safe_path,
"-m",
"unidic",
"download",
],
check=True,
)
with open("cache/dict_installed", "w", encoding="utf8") as file:
file.write("")
except subprocess.CalledProcessError:
print(
"\033[31mError installing unidic dictionary, please run frollowing command:"
)
print(f"{py_bin} -m unidic download\033[0m")
# Prepare the database
print("Preparing the database as database.csv in tabbed format...")
prepare_database()
# Fetch data from GitHub
print("Fetching the latest github:nattadasu/nekomimiDb data...")
await nk_run()
# Index MyAnimeList data from AnimeAPI
print("Indexing MyAnimeList data from AnimeAPI...")
await mal_run()
# Migrate the database
print("Migrating database to new schema...")
await migrate()
# Check if .env exists, if not, copy .env.example
if not os.path.exists(".env"):
print("Copying .env.example to .env...")
if current_os() == "Windows":
os.system("copy .env.example .env")
else:
os.system("cp .env.example .env")
else:
print(".env already exists, skipping...")
print("Initialization finished. You should be able to run the bot safely now.")
if __name__ == "__main__":
asyncio.run(first_run())