Created Tests to workout ProcessTree

Test work to see what is needed to create ProcessTree, should work on
*nix based systems, such as MacOS and Linux, since ps is available
there.  Need to work on windows with tasklist.
This commit is contained in:
Mario Steele 2026-03-02 02:01:30 -06:00
parent e02daac0ff
commit b033741431
2 changed files with 95 additions and 0 deletions

94
tools/pt_testing.gd Normal file
View file

@ -0,0 +1,94 @@
@tool
extends EditorScript
var finds: Array = [
"kitty",
"/opt/zen-browser-bin/zen-bin",
"zen-bin",
"bin/dolphin",
"Discord",
"discord"
]
class ProcessInfoTest:
extends RefCounted
var user: int
var pid: int
var ppid: int
var executable: String
func _init(_user: String, _pid: String, _ppid: String, name: String) -> void:
user = _user.to_int()
pid = _pid.to_int()
ppid = _ppid.to_int()
executable = name
func _to_string() -> String:
return "<ProcessInfo: pid:%d, ppid:%d, user:%d, exec:%s>" % [pid, ppid, user, executable]
func is_running() -> bool:
return OS.is_process_running(pid)
func kill() -> Error:
return OS.kill(pid)
func kill_parent() -> Error:
return OS.kill(ppid)
class ProcessTreeTest:
extends RefCounted
var processes: Array[ProcessInfoTest] = []
func _init() -> void:
refresh()
func refresh() -> void:
processes.clear()
var output := []
OS.execute("/usr/bin/ps", ["-eo", "uid,pid,ppid,exe"], output)
var lines: PackedStringArray = output[0].split("\n")
for line in lines:
var parts = line.split(" ",false,4)
if parts[0] == "UID":
continue
if parts[3] == "-":
continue
var proc: ProcessInfoTest = ProcessInfoTest.new(parts[0],parts[1],parts[2],parts[3])
processes.append(proc)
func has_process_id(id: int) -> bool:
return processes.any(func(x: ProcessInfoTest): return x.pid == id)
func has_process_name(name: String) -> bool:
return processes.any(func(x: ProcessInfoTest): return x.executable == name or name in x.executable)
func get_process(id: int) -> ProcessInfoTest:
for proc: ProcessInfoTest in processes:
if proc.pid == id:
return proc
return null
func get_process_by_name(name: String) -> ProcessInfoTest:
for proc: ProcessInfoTest in processes:
if proc.executable == name or name in proc.executable:
return proc
return null
func get_all_processes_by_name(name: String) -> Array[ProcessInfoTest]:
var procs: Array[ProcessInfoTest] = []
for proc: ProcessInfoTest in processes:
if proc.executable == name or name in proc.executable:
procs.append(proc)
return procs
# Called when the script is executed (using File -> Run in Script Editor).
func _run() -> void:
var tree := ProcessTreeTest.new()
for name in finds:
var has_proc := tree.has_process_name(name)
var proc := tree.get_process_by_name(name)
var procs := tree.get_all_processes_by_name(name)
print("Has Process '%s': %s" % [name, has_proc])
print("First Process '%s': %s" % [name, proc])
print("Processes '%s': %s" % [name, procs])
print("")

1
tools/pt_testing.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://b6xjdxwi4o27f