diff --git a/python-programming/BB_plus_watch_temp_files.py b/python-programming/BB_plus_watch_temp_files.py new file mode 100644 index 0000000..fa736d7 --- /dev/null +++ b/python-programming/BB_plus_watch_temp_files.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 + +# BB_plus_watch_temp_files.py +# +# Watch for any new files created by Baldi's Basics Plus +# +# The video game Baldi's Basics Plus is created by Basically Games +# +# Author: Intel A80486DX2-66 +# License: Unlicense + +from dotenv import load_dotenv +from os import environ, listdir, makedirs +from os.path import exists, join as path_join +from subprocess import Popen +from tempfile import gettempdir +from time import sleep +from sys import exit + +if exists(".env"): + load_dotenv() + +EXEC_PATH = environ.get("BBIEAL_EXECUTABLE_PATH", None) + +PATH_TO_WATCH = path_join(gettempdir(), + "Basically Games", "Baldi's Basics Plus") + +def respectful_makedirs(path: str) -> None: + if not exists(path): + makedirs(path) + + +if __name__ == "__main__": + if EXEC_PATH is None: + print("Please create a .env file in the current directory with key " + "BBIEAL_EXECUTABLE_PATH, containing path to the executable of " + "your copy of the video game Baldi's Basics Plus, or set such an " + "environmental variable.") + exit(1) + + print("Path to watch:", PATH_TO_WATCH) + respectful_makedirs(PATH_TO_WATCH) + + print("Starting the game", flush=True) + handle = Popen([EXEC_PATH]) + + print("Watching directory") + files = set(listdir(PATH_TO_WATCH)) + while handle.poll() is None: + sleep(1) + new_files = set(listdir(PATH_TO_WATCH)) - files + if new_files: + for f in new_files: + print("[+]", f) + files |= new_files