forked from Just-Some-Bots/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
245 lines (183 loc) · 7.26 KB
/
run.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import sys, os, traceback, subprocess, webbrowser
class GIT(object):
@classmethod
def works(cls):
try:
return bool(subprocess.check_output('git --version', shell=True))
except:
return False
class PIP(object):
@classmethod
def run(cls, command, quiet=False, check_output=False):
if not cls.works():
raise RuntimeError("Could not import pip.")
check = subprocess.check_output if check_output else subprocess.check_call
fullcommand = [sys.executable] + "-m pip {}{}".format('-q ' if quiet else '', command)
try:
return check(fullcommand.split(), shell=True)
except:
traceback.print_exc()
raise RuntimeError("Failed to run command: %s" % fullcommand)
@classmethod
def run_install(cls, cmd, quiet=False, check_output=False):
return cls.run("install %s" % cmd, quiet, check_output)
@classmethod
def run_show(cls, cmd, quiet=False, check_output=False):
return cls.run("show %s" % cmd, quiet, check_output)
@classmethod
def works(cls):
try:
import pip
return True
except ImportError:
return False
@classmethod
def get_module_version(cls, mod):
try:
out = cls.run_show(mod, check_output=True)
datas = str(out).split('\\r\\n')
expectedversion = datas[3]
if expectedversion.startswith('Version: '):
return expectedversion.split()[1]
else:
return [x.split()[1] for x in datas if x.startswith("Version: ")][0]
except:
pass
def open_in_wb(text, printanyways=True, indents=4):
import webbrowser
# TODO: Figure out console browser stuff
# also check GenericBrowser
if isinstance(webbrowser.get(), webbrowser.BackgroundBrowser):
print('%s%s' % (' ' * indents , text))
else:
webbrowser.open_new_tab(text)
if printanyways:
print('%s%s' % (' ' * indents , text))
def main():
if not sys.version_info >= (3, 5):
print("Python 3.5+ is required. This version is %s" % sys.version.split()[0])
print("Attempting to locate python 3.5...")
pycom = None
# Maybe I should check for if the current dir is the musicbot folder, just in case
if sys.platform.startswith('win'):
try:
subprocess.check_output('py -3.5 -c "exit()"', shell=True)
pycom = 'py -3.5'
except:
try:
subprocess.check_output('python3 -c "exit()"', shell=True)
pycom = 'python3'
except:
pass
if pycom:
print("Python 3 found. Launching bot...")
os.system('start cmd /k %s run.py' % pycom)
sys.exit(0)
else:
try:
pycom = subprocess.check_output(['which', 'python3.5']).strip().decode()
except:
pass
if pycom:
print("\nPython 3 found. Re-launching bot using: ")
print(" %s run.py\n" % pycom)
os.system("kill -9 %s && %s run.py" % (os.getpid(), pycom))
print("Please run the bot using python 3.5")
input("Press enter to continue . . .")
return
if '--update' in sys.argv:
if PIP.works():
err = PIP.run_install('--upgrade -r requirements.txt', quiet=True)
print()
if err:
if err == 2:
print("Upgrade failed, you may need to run it as admin/root")
else:
print("Automatic upgrade failed")
input("Press enter to continue . . .")
return
else:
print("\n"
"Could not locate PIP. If you're sure you have it, run this:\n"
" your_pip_command install --upgrade -r requirements.txt"
"\n\n")
input("Press enter to continue . . .")
return
tried_requirementstxt = False
tryagain = True
while tryagain:
tryagain = False
try:
from musicbot import MusicBot
MusicBot().run()
break
except ImportError as e:
traceback.print_exc()
if not tried_requirementstxt:
tried_requirementstxt = True
print("Attempting to install dependencies...")
err = PIP.run_install('-r requirements.txt', quiet=True)
if err == 2:
print("\nIf that said \"Access/Permission is denied\", run it as admin/with sudo.")
break
if e.name in ['discord', 'opus', 'win_unicode_console']:
tryagain = unfuck(e)
if not tryagain:
input('Press enter to continue . . .')
def unfuck(e):
try:
import pip
except:
if not PIP.works():
print("Additionally, pip cannot be imported. Has python been installed properly?")
print("Bot setup instructions can be found here:")
print("https://github.com/SexualRhinoceros/MusicBot/blob/develop/README.md")
return
print()
if e.name == 'discord':
if PIP.get_module_version('discord.py'):
return True
print("Discord.py is not installed.")
if not GIT.works():
print("Additionally, git is also not installed. Please install git.")
print("Bot setup instructions can be found here:")
print("https://github.com/SexualRhinoceros/MusicBot/blob/develop/README.md")
return
print("Attempting to install discord.py")
err = PIP.run_install("git+https://github.com/Rapptz/discord.py@async", quiet=True)
if err:
print()
print("Could not install discord.py for you. Please run:")
print(" pip install git+https://github.com/Rapptz/discord.py@async")
else:
print("Ok, maybe we're good?")
print()
return True
elif e.name == 'opus':
print("Discord.py is out of date. Did you install the old version?")
print("Attempting to upgrade discord.py")
err = PIP.run_install("--upgrade git+https://github.com/Rapptz/discord.py@async", quiet=True)
if err:
print()
print("Could not update discord.py for you. Please run:")
print(" pip install --upgrade git+https://github.com/Rapptz/discord.py@async")
else:
print("Ok, maybe we're good?")
print()
return True
elif e.name == 'win_unicode_console':
if PIP.get_module_version('win-unicode-console'):
return True
print("Module 'win_unicode_console' is missing.")
print("Attempting to install win_unicode_console")
err = PIP.run_install("win-unicode-console", quiet=True)
if err:
print()
print("Could not install win_unicode_console for you. Please run:")
print(" pip install win-unicode-console")
else:
print("Ok, maybe we're good?")
print()
return True
if __name__ == '__main__':
main()