Last active
May 24, 2023 18:26
-
-
Save laplante-sean/3310ce79723fdb6ca08941f8a9467778 to your computer and use it in GitHub Desktop.
Automate updating Godot on Windows. Drag and drop onto python script. Does not require admin. Installs in user paths. Requires `pywin32`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Script to automate the things I do literally every time a new version of Godot comes out | |
1. Extracts godot from the provided zip | |
2. Removes the old version in the install directory and puts the new one there | |
3. Removes the old start menu shortcut and adds a new one | |
""" | |
import os | |
import sys | |
import time | |
import zipfile | |
import argparse | |
import traceback | |
from win32com.client import Dispatch | |
DEFAULT_INSTALL_PATH = os.path.join(os.path.expanduser("~"), "AppData", "Local", "Godot") | |
SHORTCUT_PATH = os.path.join(os.path.expanduser("~"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs") | |
def create_shortcut(target: str): | |
"""Create the shortcut.""" | |
print(f"Creating shortcut to: {target}") | |
# Remove old shortcut | |
for file in os.listdir(SHORTCUT_PATH): | |
if file == "Godot.lnk": | |
os.remove(os.path.join(SHORTCUT_PATH, file)) | |
path = os.path.join(SHORTCUT_PATH, "Godot.lnk") | |
wDir = os.path.abspath(os.path.dirname(target)) | |
shell = Dispatch('WScript.Shell') | |
shortcut = shell.CreateShortCut(path) | |
shortcut.Targetpath = target | |
shortcut.WorkingDirectory = wDir | |
shortcut.IconLocation = target | |
shortcut.save() | |
def main(): | |
"""Entry Point.""" | |
parser = argparse.ArgumentParser(description="Update Godot") | |
parser.add_argument("-p", "--install-path", help="Install directory for Godot", default=DEFAULT_INSTALL_PATH) | |
parser.add_argument("godot", nargs=1) | |
args = parser.parse_args() | |
zpath = args.godot[0] | |
if not zipfile.is_zipfile(zpath): | |
print(f"{zpath} is not a valid zip file.") | |
sys.exit(1) | |
# Make path if it doesn't exist | |
if not os.path.isdir(args.install_path): | |
os.makedirs(args.install_path, exist_ok=True) | |
# Remove old versions | |
for file in os.listdir(args.install_path): | |
if file.startswith("Godot") and file.endswith(".exe"): | |
os.remove(os.path.join(args.install_path, file)) | |
print(f"Installing to: {args.install_path}") | |
with zipfile.ZipFile(zpath) as zf: | |
zf.extractall(args.install_path) | |
godot_exe_path = None | |
for file in os.listdir(args.install_path): | |
if "console" in file: | |
continue | |
if file.endswith(".exe"): | |
godot_exe_path = os.path.join(args.install_path, file) | |
break | |
create_shortcut(godot_exe_path) | |
print("Done.") | |
if __name__ == "__main__": | |
try: | |
main() | |
except Exception as exc: | |
traceback.print_exc() | |
print("This window will close in 5 seconds") | |
time.sleep(5) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment