ui: align app_key canonicalization with backend
This commit is contained in:
@@ -1108,17 +1108,74 @@ RU: Применяет policy-rules и проверяет health. При оши
|
||||
self._emit_log(line)
|
||||
|
||||
def _infer_app_key_from_cmdline(self, cmdline: str) -> str:
|
||||
# Keep this aligned with backend canonicalization (flatpak/snap/env/path).
|
||||
cmd = (cmdline or "").strip()
|
||||
if not cmd:
|
||||
return ""
|
||||
try:
|
||||
args = shlex.split(cmd)
|
||||
if args:
|
||||
return str(args[0] or "").strip()
|
||||
except Exception:
|
||||
pass
|
||||
# Fallback: first token
|
||||
return (cmd.split() or [""])[0].strip()
|
||||
args = cmd.split()
|
||||
return self._canonical_app_key_from_tokens(args)
|
||||
|
||||
def _canonical_app_key_from_tokens(self, tokens: list[str]) -> str:
|
||||
toks = [str(x or "").strip() for x in (tokens or []) if str(x or "").strip()]
|
||||
if not toks:
|
||||
return ""
|
||||
|
||||
def base(t: str) -> str:
|
||||
return os.path.basename(str(t or "").strip())
|
||||
|
||||
def extract_run_target(toks2: list[str]) -> str:
|
||||
idx = -1
|
||||
for i, t in enumerate(toks2):
|
||||
if t == "run":
|
||||
idx = i
|
||||
break
|
||||
if idx < 0:
|
||||
return ""
|
||||
for j in range(idx + 1, len(toks2)):
|
||||
t = toks2[j].strip()
|
||||
if not t or t == "--":
|
||||
continue
|
||||
if t.startswith("-"):
|
||||
continue
|
||||
return t
|
||||
return ""
|
||||
|
||||
primary = toks[0]
|
||||
b = base(primary).lower()
|
||||
|
||||
if b == "env":
|
||||
# env VAR=1 /usr/bin/app ...
|
||||
for j in range(1, len(toks)):
|
||||
t = toks[j].strip()
|
||||
if not t or t == "--":
|
||||
continue
|
||||
if t.startswith("-"):
|
||||
continue
|
||||
if "=" in t: # VAR=VAL
|
||||
continue
|
||||
return self._canonical_app_key_from_tokens(toks[j:])
|
||||
return "env"
|
||||
|
||||
if b == "flatpak":
|
||||
appid = extract_run_target(toks)
|
||||
return f"flatpak:{appid}" if appid else "flatpak"
|
||||
|
||||
if b == "snap":
|
||||
name = extract_run_target(toks)
|
||||
return f"snap:{name}" if name else "snap"
|
||||
|
||||
if b == "gtk-launch" and len(toks) >= 2:
|
||||
did = toks[1].strip()
|
||||
if did and not did.startswith("-"):
|
||||
return f"desktop:{did}"
|
||||
|
||||
if "/" in primary:
|
||||
return base(primary) or primary
|
||||
|
||||
return primary
|
||||
|
||||
def _launch_and_mark(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user