1
1
#!/usr/bin/env python3
2
2
3
3
import os
4
+ import pathlib
4
5
import shutil
5
6
import subprocess
6
7
import sys
@@ -130,6 +131,41 @@ def check_bot_updates(git_bin: str, branch_name: str) -> Optional[Tuple[str, str
130
131
return None
131
132
132
133
134
+ def check_for_process (proc_path : str ) -> None :
135
+ """Check for processes that would prevent updates"""
136
+ path = pathlib .Path (proc_path )
137
+ name = path .stem
138
+ print (f"Checking if { name } is still running..." )
139
+ try :
140
+ o = (
141
+ subprocess .check_output (
142
+ [
143
+ "powershell.exe" ,
144
+ "Get-Process" ,
145
+ "-name" ,
146
+ f"{ name } *" ,
147
+ "-ErrorAction" ,
148
+ "silentlycontinue" ,
149
+ "|" ,
150
+ "select" ,
151
+ "path" ,
152
+ ],
153
+ )
154
+ .decode ("utf-8" )
155
+ .strip ()
156
+ )
157
+ except subprocess .CalledProcessError :
158
+ # The command will error out if no process is found at all.
159
+ # May be a better way to deal with that but this is simple.
160
+ o = ""
161
+
162
+ if proc_path in o :
163
+ raise RuntimeError (
164
+ f"Cannot continue because { name } is still in use!\n "
165
+ "Make sure MusicBot is shut down, or use Task Manager to stop the process first."
166
+ )
167
+
168
+
133
169
def update_deps () -> None :
134
170
"""
135
171
Tries to upgrade MusicBot dependencies using pip module.
@@ -245,6 +281,16 @@ def dl_windows_ffmpeg() -> None:
245
281
os .unlink (tmp_ffmpeg_path )
246
282
247
283
284
+ def check_ffmpeg_running () -> None :
285
+ """check if ffmpeg is still running and exit if so."""
286
+ if not sys .platform .startswith ("win" ):
287
+ return
288
+
289
+ ffmpeg_bin = shutil .which ("ffmpeg" )
290
+ if ffmpeg_bin :
291
+ check_for_process (ffmpeg_bin )
292
+
293
+
248
294
def update_ffmpeg () -> None :
249
295
"""
250
296
Handles checking for new versions of ffmpeg and requesting update.
@@ -256,6 +302,8 @@ def update_ffmpeg() -> None:
256
302
)
257
303
return
258
304
305
+ check_ffmpeg_running ()
306
+
259
307
print ("Checking for ffmpeg versions..." )
260
308
261
309
bundle_ffmpeg_bin = os .path .join (os .path .abspath ("bin" ), "ffmpeg.exe" )
@@ -403,6 +451,7 @@ def main() -> None:
403
451
"Would you like to reset the Source code, to allow MusicBot to update?"
404
452
)
405
453
if hard_reset :
454
+ check_ffmpeg_running ()
406
455
run_or_raise_error (
407
456
[git_bin , "reset" , "--hard" ],
408
457
"Could not hard reset the directory to a clean state.\n "
@@ -436,6 +485,7 @@ def main() -> None:
436
485
print (f"Updates are available, latest commit ID is: { updates [1 ]} " )
437
486
do_bot_upgrade = yes_or_no_input ("Would you like to update?" )
438
487
if do_bot_upgrade :
488
+ check_ffmpeg_running ()
439
489
run_or_raise_error (
440
490
[git_bin , "pull" ],
441
491
"Could not update the bot. You will need to run 'git pull' manually." ,
0 commit comments