a lot of applications seem to hardcode this location, so it's better to have something default, or default-adjasoned.
67 lines
1.5 KiB
Python
Executable File
67 lines
1.5 KiB
Python
Executable File
#!/bin/python
|
|
|
|
import os
|
|
from sys import stderr
|
|
|
|
def error(msg: str):
|
|
print(f"\033[91m{msg}\033[0m", file=stderr)
|
|
|
|
def validate_path(path: str, outdir: str, outpath: str) -> bool:
|
|
if (os.path.isdir(outdir) == False):
|
|
error(f"'{outdir}' does not exist! can't write .desktop file!")
|
|
return False
|
|
|
|
if (os.path.exists(outpath)):
|
|
error(f"'{outpath}' already exists!")
|
|
return False
|
|
|
|
if (os.path.isfile(path) == False):
|
|
error(f"file does not exist: '{path}'")
|
|
return False
|
|
return True
|
|
|
|
|
|
def create_desktop_file(path: str) -> int:
|
|
outdir = os.environ['HOME'] + "/.local/share/applications"
|
|
outpath = outdir + "/" + os.path.basename(path)
|
|
|
|
if (validate_path(path, outdir, outpath) == False):
|
|
return 1
|
|
|
|
f_in = open(path, "r")
|
|
f_out = open(outpath, "w")
|
|
|
|
while (True):
|
|
ln = f_in.readline();
|
|
|
|
if (len(ln) == 0):
|
|
break
|
|
|
|
if (ln.startswith("Exec=")):
|
|
lns = ln.split('=', 1)
|
|
ln = f"{lns[0]}=prime-run {lns[1]}"
|
|
|
|
f_out.write(ln)
|
|
|
|
f_in.close()
|
|
f_out.close()
|
|
return 0
|
|
|
|
def main(argc: int, argv: list[str]) -> int:
|
|
|
|
if (argc <= 1):
|
|
error("incorrect amount of arguments! expected: 'desktop file path'")
|
|
return 1
|
|
|
|
err = 0
|
|
i = 1
|
|
while (i < argc):
|
|
err |= create_desktop_file(argv[i])
|
|
i += 1
|
|
|
|
return err
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
exit(main(len(sys.argv), sys.argv));
|