new custom_commands
authorNiki Roo <niki@nikiroo.be>
Tue, 25 Mar 2025 15:38:10 +0000 (16:38 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 25 Mar 2025 15:38:10 +0000 (16:38 +0100)
gamiki/support/commands.py
gamiki/support/custom_commands.py [new file with mode: 0644]

index 9b4f98749bfa85d5050040831167c700b8c9c163..613b7dd759eda0191e6660d2e88e64bb68d75d9d 100644 (file)
@@ -3,26 +3,16 @@ from shutil     import which
 from os         import environ
 from pathlib    import PurePath
 
-__programs: dict[str, str] = None
-
-def program(program: str) -> str:
-    global __programs
-    
-    if (__programs == None):
-        __programs = {}
-
-    if (program not in __programs):
-        __programs[program] = which(program)
-
-    return __programs[program]
+import gamiki.support.custom_commands as custom
 
 def dosbox(dir: PurePath, link: PurePath) -> int:
-    if (program("app.sh") != None):
-        cmd = [ program("app.sh"),"--wait","dosbox","--link",link.as_posix() ]
-    elif(program("dosbox") != None):
-        cmd = [ program("dosbox") ]
-    else:
-        raise RuntimeError("DosBox not found")
+    
+    cmd, env = custom.cmd("dosbox", link, None)    
+    if (not cmd):
+        if (prog := custom.program("dosbox")):
+            cmd = [ prog ]
+        else:
+            raise RuntimeError("DosBox not found")
     
     base = dir.joinpath("base.conf")
     if (base.exists()):
@@ -31,32 +21,28 @@ def dosbox(dir: PurePath, link: PurePath) -> int:
     cmd.append("-conf")
     cmd.append(dir.joinpath("start.conf").as_posix())
     
-    return run(cmd, cwd=dir).returncode
-
-def start_sh(start: PurePath) -> int:
-    env = environ.copy()
-    
-    if (program("launch.sh") != None):
-        cmd = [ program("launch.sh"), "games", "--dlink", start.as_posix() ]
-        env["OPTS"] = "--entrypoint=bash"
-    else:
-        cmd = [ start.as_posix() ]
-    
-    return run(cmd, cwd=start.parent, env=env).returncode
+    return run(cmd, cwd=dir, env=env).returncode
 
 def wine(dir: PurePath) -> int:
-    env = environ.copy()
     
-    wine_bat = dir.joinpath("wine.bat").as_posix()
-    wine_prefix = dir.joinpath("wine.prefix").as_posix()
+    wine_bat = dir.joinpath("wine.bat")
+    wine_prefix = dir.joinpath("wine.prefix")
     
-    if (program("launch.sh") != None):
-        cmd = [ program("launch.sh"), "wine", "--dlink", wine_bat ]
-        env["OPTS"] = "-e WINEPREFIX='" + wine_prefix.replace("'", "\\'") + "'"
-    elif (program("wine") != None):
-        cmd = [ "wine", wine_bat ]
-    else:
-        raise RuntimeError("wine not found")
+    cmd, env = custom.cmd("wine", wine_bat, wine_prefix)
+    if (not cmd):
+        if (prog := custom.program("wine")):
+            cmd = [ prog, wine_bat.as_posix() ]
+            env["WINEPREFIX"] = wine_prefix.as_posix()
+        else:
+            raise RuntimeError("wine not found")
     
     return run(cmd, cwd=dir, env=env).returncode
 
+def start_sh(start: PurePath) -> int:
+    
+    cmd, env = custom.cmd("start_sh", start, None)
+    if (not cmd):
+        cmd = [ start.as_posix() ]
+    
+    return run(cmd, cwd=start.parent, env=env).returncode
+
diff --git a/gamiki/support/custom_commands.py b/gamiki/support/custom_commands.py
new file mode 100644 (file)
index 0000000..e033557
--- /dev/null
@@ -0,0 +1,44 @@
+from typing     import Optional
+from subprocess import run
+from shutil     import which
+from os         import environ
+from pathlib    import PurePath
+
+__programs__: dict[str, Optional[str]] = { }
+
+def program(program: str) -> Optional[str]:
+    global __programs__
+
+    if (program not in __programs__):
+        __programs__[program] = which(program)
+
+    return __programs__[program]
+
+def cmd (
+        prog: str, p1: PurePath, p2: Optional[PurePath]
+        ) -> (Optional[list[str]], dict[str, str]):
+    
+    cmd = None
+    env = environ.copy()
+
+    match prog:
+        case "dosbox":
+            if (program("app.sh")):
+                cmd = [ 
+                    program("app.sh"),"--wait","dosbox","--link",p1.as_posix()
+                ]
+        case "wine":
+            if (program("launch.sh")):
+                cmd = [ program("launch.sh"), "wine", "--dlink", p1.as_posix() ]
+                env["OPTS"] = (
+                    "-e WINEPREFIX='" + p2.as_posix().replace("'", "\\'") + "'"
+                )
+        case "start_sh":
+            if (program("launch.sh")):
+                cmd = [ 
+                    program("launch.sh"), "games", "--dlink", p1.as_posix()
+                ]
+                env["OPTS"] = "--entrypoint=bash"
+    
+    return cmd, env
+