summarylogtreecommitdiffstats
path: root/fbrokendesktop.py
diff options
context:
space:
mode:
authorFirstAirBender2023-11-04 03:30:55 -0600
committerFirstAirBender2023-11-04 03:30:55 -0600
commit57ee25d81afdc30ed398bd5a8a346cd4cf17d390 (patch)
tree6f426d37865ea9a48a68e513c5649e9190de0cbe /fbrokendesktop.py
parent4b36ca25b34aa413c0dcb7b24f41fea4912cf9c1 (diff)
downloadaur-57ee25d81afdc30ed398bd5a8a346cd4cf17d390.tar.gz
update to v1.3
Diffstat (limited to 'fbrokendesktop.py')
-rwxr-xr-xfbrokendesktop.py99
1 files changed, 55 insertions, 44 deletions
diff --git a/fbrokendesktop.py b/fbrokendesktop.py
index 8c16a710115f..6f793c7177c2 100755
--- a/fbrokendesktop.py
+++ b/fbrokendesktop.py
@@ -8,80 +8,91 @@ import shutil
import subprocess
import sys
+from typing import cast
from os import path
from xdg import BaseDirectory
from xdg.DesktopEntry import DesktopEntry
# allow matching empty envs with .*
-env_re = re.compile(r'\w+=.*')
+env_re = re.compile(r"\w+=.*")
# installed gapps
gapps = []
-def is_valid_cmd(cmd_args: list[str]):
- if cmd_args[0] == 'exec':
- return is_valid_cmd(cmd_args[1:])
- if cmd_args[0] == 'env':
- return is_valid_cmd(cmd_args[1:])
-
- for tok in cmd_args:
- # ignore all attempts to set environment variables
- if not env_re.match(tok):
- if shutil.which(tok):
- return True
- return False
-
-def is_valid_gapp_cmd(cmd_args: list[str]):
- if is_gapp_cmd(cmd_args):
- app_id = cmd_args[2]
- if not gapps:
- try:
- output = subprocess.check_output(['gapplication', 'list-apps'], text=True)
- gapps.extend((output or '').split('\n'))
- except subprocess.CalledProcessError:
- gapps.append('')
- return app_id in gapps
- return False
+
+def strip_command_parent(cmd_args: list[str], is_first: bool = True) -> list[str]:
+ while cmd_args and env_re.match(cmd_args[0]):
+ cmd_args = cmd_args[1:]
+ try:
+ cmd = cmd_args[0]
+ except IndexError:
+ cmd = ""
+ if is_first and cmd == "exec":
+ return strip_command_parent(cmd_args[1:], is_first=False)
+ if cmd == "env" or cmd.endswith("/env"):
+ return strip_command_parent(cmd_args[1:], is_first=False)
+ return cmd_args
+
+
+def is_valid_gapp_cmd(cmd: str):
+ app_id = cmd
+ if not gapps:
+ try:
+ output = subprocess.check_output(["gapplication", "list-apps"], text=True)
+ gapps.extend((output or "").split("\n"))
+ except subprocess.CalledProcessError:
+ gapps.append("")
+ return app_id in gapps
+
def is_gapp_cmd(cmd_args: list[str]):
- return len(cmd_args) > 2 and cmd_args[0] == 'gapplication' and cmd_args[1] == 'launch'
+ return (
+ len(cmd_args) > 2
+ and (cmd_args[0] == "gapplication" or cmd_args[0].endswith("/gapplication"))
+ and cmd_args[1] == "launch"
+ )
+
def find_missing_desktop_files(desktop_dir: str, show_all: bool):
- for df in glob.iglob('*.desktop', root_dir=desktop_dir):
+ for df in glob.iglob("*.desktop", root_dir=desktop_dir):
file_path = path.join(desktop_dir, df)
de = DesktopEntry(file_path)
if show_all or not de.getNoDisplay():
- if exc := (de.getTryExec() or de.getExec()):
+ if exc := cast(str | None, (de.getExec() or de.getTryExec())):
try:
cmd = shlex.split(exc)
- except ValueError as err:
- print (f"Error parsing '{file_path}': {err}", file=sys.stderr)
- continue
- if is_gapp_cmd(cmd):
- if not is_valid_gapp_cmd(cmd):
+ cmd = strip_command_parent(cmd)
+ if is_gapp_cmd(cmd):
+ if not is_valid_gapp_cmd(cmd[2]):
+ yield shlex.quote(de.getFileName())
+ elif not shutil.which(cmd[0]):
yield shlex.quote(de.getFileName())
- elif not is_valid_cmd(cmd):
- yield shlex.quote(de.getFileName())
+ except ValueError as err:
+ print(f"Error parsing '{file_path}': {err}", file=sys.stderr)
+
def find_desktop_directories():
"""
https://wiki.archlinux.org/title/desktop_entries#Modify_desktop_files
https://wiki.archlinux.org/title/XDG_Autostart#Directories
"""
- yield from BaseDirectory.load_data_paths('applications')
- yield from BaseDirectory.load_config_paths('autostart')
+ yield from BaseDirectory.load_data_paths("applications")
+ yield from BaseDirectory.load_config_paths("autostart")
+
-if __name__ == '__main__':
+if __name__ == "__main__":
parser = argparse.ArgumentParser(
- description='Find desktop entries files with broken executables'
+ description="Find desktop entries files with broken executables"
)
- parser.add_argument('-a', '--all',
- action='store_true',
- help='show all desktop entries regardless of "NoDisplay" value',
- default=False
+ parser.add_argument(
+ "-a",
+ "--all",
+ action="store_true",
+ help='show all desktop entries regardless of "NoDisplay" value',
+ default=False,
)
args = parser.parse_args()
for d in find_desktop_directories():
for df in find_missing_desktop_files(d, args.all):
- print (df)
+ print(df)