Skip to content

Commit 63b8a73

Browse files
refactor(fem): simplify GetDP executable probing and validation
1 parent 52c2cff commit 63b8a73

1 file changed

Lines changed: 32 additions & 93 deletions

File tree

src/engine/fem/helpers.jl

Lines changed: 32 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -264,112 +264,51 @@ function archive_frequency_results(workspace::FEMWorkspace, frequency::Float64)
264264
end
265265
end
266266

267-
# Run a command, capture stdout+stderr, never throw. Returns (ok::Bool, log::String).
268-
_run_cmd_capture(cmd::Cmd) = begin
269-
io = IOBuffer()
270-
ok = success(pipeline(cmd; stdout=io, stderr=io))
271-
return ok, String(take!(io))
272-
end
273-
274-
# Heuristic: does text look like a plain version (e.g., "3.5.0", "3.6")?
275-
_looks_like_version(s::AbstractString) =
276-
occursin(r"^\s*\d+(?:\.\d+){0,2}\s*$", s)
277-
278-
# Parse version from the `-info` output (line like "Version : 3.5.0")
279-
function _parse_info_version(info::AbstractString)
280-
m = match(r"(?mi)^\s*Version\s*:\s*([0-9]+(?:\.[0-9]+){0,2})\s*$", info)
281-
return m === nothing ? nothing : m.captures[1]
282-
end
283-
284-
function _probe_getdp(exe::AbstractString)
285-
tried = String[]
286-
287-
# 1) Preferred: rich banner via -info
288-
infocmd = Cmd(`$exe -info`; windows_verbatim=true)
289-
push!(tried, string(infocmd))
290-
ok, info = _run_cmd_capture(infocmd)
291-
@debug "Probe (-info)" cmd = string(infocmd) ok = ok out_first_200 = first(
292-
info,
293-
min(lastindex(info), 200),
294-
)
295-
296-
if ok
297-
ver = _parse_info_version(info)
298-
base = splitpath(exe)[end]
299-
mentions_getdp =
300-
occursin(r"getdp\.info"i, info) || occursin(r"\bGetDP\b"i, info) ||
301-
occursin(r"getdp"i, base)
302-
if ver !== nothing && mentions_getdp
303-
return true, "info:$ver", tried
304-
end
305-
end
306-
307-
# 2) Fallback: version-only
308-
vcmd = Cmd(`$exe -version`; windows_verbatim=true)
309-
push!(tried, string(vcmd))
310-
vok, vout = _run_cmd_capture(vcmd)
311-
@debug "Probe (-version)" cmd = string(vcmd) ok = vok out = vout
312-
313-
if vok && _looks_like_version(vout)
314-
base = splitpath(exe)[end]
315-
if occursin(r"getdp"i, base)
316-
return true, "version:$vout", tried
317-
end
267+
# Run a command quietly; return true if it starts and exits with code 0.
268+
_run_ok(cmd::Cmd) =
269+
try
270+
success(pipeline(cmd; stdout=devnull, stderr=devnull))
271+
catch
272+
false # covers "file not found", spawn failures, etc.
318273
end
319274

320-
return false, "no identifying output", tried
275+
# Does this path behave like a GetDP executable?
276+
_is_valid_getdp_exe(path::AbstractString) = begin
277+
@debug "Probing GetDP via -info" path = path
278+
_run_ok(`$path -info`)
321279
end
322280

323-
# Internal helper to find the executable path
281+
# Resolve the GetDP path:
282+
# 1) If user provided :getdp_executable and it runs with -info, use it.
283+
# 2) Else ask GetDP.jl for its executable and use it if it runs with -info.
284+
# 3) Else, error.
324285
function _resolve_getdp_path(opts::NamedTuple)
325286
user_path = get(opts, :getdp_executable, nothing)
326-
@debug "Resolving GetDP path" opts = opts user_path = user_path
287+
@debug "Resolving GetDP path (simple probe)" user_path = user_path
327288

328-
# 1) User override
329-
user_path = get(opts, :getdp_executable, nothing)
330-
if user_path isa String
331-
@debug "User-specified path" path = user_path exists = isfile(user_path)
332-
if isfile(user_path)
333-
ok, info, tried = _probe_getdp(user_path)
334-
if ok
335-
@debug "Using user-specified GetDP executable" path = user_path info = info
336-
return user_path
337-
else
338-
@warn "User-specified executable failed identity check" path = user_path tried_cmds = tried info = info
339-
end
289+
if user_path isa AbstractString
290+
if _is_valid_getdp_exe(user_path)
291+
@debug "Using user-specified GetDP executable" path = user_path
292+
return user_path
340293
else
341-
@warn "User-specified path is not a file" path = user_path
294+
@warn "User-specified GetDP executable failed when invoked with -info" path = user_path
342295
end
343296
else
344297
@debug "No user-specified GetDP path"
345298
end
346299

347-
# 2) Fallback: query GetDP for its executable path
348-
fallback_path = GetDP.get_getdp_executable()
349-
@debug "GetDP.get_getdp_executable() returned" path = fallback_path exists = isfile(
350-
fallback_path,
351-
)
300+
fallback = try
301+
GetDP.get_getdp_executable()
302+
catch
303+
nothing
304+
end
305+
@debug "GetDP.get_getdp_executable() returned" path = fallback
352306

353-
if isfile(fallback_path)
354-
ok, info, tried = _probe_getdp(fallback_path)
355-
if ok
356-
@debug "Using dependency-provided GetDP executable" path = fallback_path info = info
357-
return fallback_path
358-
else
359-
@warn "Dependency-provided executable failed identity check" path = fallback_path tried_cmds = tried info = info
360-
end
307+
if fallback isa AbstractString && _is_valid_getdp_exe(fallback)
308+
@debug "Using dependency-provided GetDP executable" path = fallback
309+
return fallback
361310
end
362311

363-
# 3) Last-resort diagnostics
364-
path_env = get(ENV, "PATH", "(unset)")
365-
gmsh_on_path = Sys.which("gmsh")
366-
gmsh_ver =
367-
gmsh_on_path === nothing ? "(n/a)" :
368-
(last(_run_cmd_capture(Cmd(`$(gmsh_on_path) -version`; windows_verbatim=true))))
369-
@debug "Diagnostics before error" PATH = path_env gmsh = gmsh_on_path gmsh_version = gmsh_ver
370-
371-
Base.error(
372-
"GetDP executable not found or not identifiable. " *
373-
"Provide :getdp_executable in opts or ensure GetDP.jl package is properly deployed and returns a valid binary.",
374-
)
375-
end
312+
error("GetDP executable not found or not working (invocation with -info failed). " *
313+
"Provide :getdp_executable in opts or ensure GetDP.jl is properly deployed.")
314+
end

0 commit comments

Comments
 (0)