Skip to content

Commit 0287534

Browse files
committed
[Windows] Check for ffmpeg running before doing update to the binary.
This likely is not a perfect solution but maybe it will work for now...
1 parent d145f26 commit 0287534

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

update.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import pathlib
45
import shutil
56
import subprocess
67
import sys
@@ -130,6 +131,41 @@ def check_bot_updates(git_bin: str, branch_name: str) -> Optional[Tuple[str, str
130131
return None
131132

132133

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+
133169
def update_deps() -> None:
134170
"""
135171
Tries to upgrade MusicBot dependencies using pip module.
@@ -245,6 +281,16 @@ def dl_windows_ffmpeg() -> None:
245281
os.unlink(tmp_ffmpeg_path)
246282

247283

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+
248294
def update_ffmpeg() -> None:
249295
"""
250296
Handles checking for new versions of ffmpeg and requesting update.
@@ -256,6 +302,8 @@ def update_ffmpeg() -> None:
256302
)
257303
return
258304

305+
check_ffmpeg_running()
306+
259307
print("Checking for ffmpeg versions...")
260308

261309
bundle_ffmpeg_bin = os.path.join(os.path.abspath("bin"), "ffmpeg.exe")
@@ -403,6 +451,7 @@ def main() -> None:
403451
"Would you like to reset the Source code, to allow MusicBot to update?"
404452
)
405453
if hard_reset:
454+
check_ffmpeg_running()
406455
run_or_raise_error(
407456
[git_bin, "reset", "--hard"],
408457
"Could not hard reset the directory to a clean state.\n"
@@ -436,6 +485,7 @@ def main() -> None:
436485
print(f"Updates are available, latest commit ID is: {updates[1]}")
437486
do_bot_upgrade = yes_or_no_input("Would you like to update?")
438487
if do_bot_upgrade:
488+
check_ffmpeg_running()
439489
run_or_raise_error(
440490
[git_bin, "pull"],
441491
"Could not update the bot. You will need to run 'git pull' manually.",

0 commit comments

Comments
 (0)