Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework executable path handling #489

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 91 additions & 4 deletions lib/dtutils/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ end
]]

local function _check_path_for_wine_bin(path)
local result = false
local result = false

if string.len(path) > 0 then
-- check for windows executable to run under wine
Expand Down Expand Up @@ -294,7 +294,7 @@ local function _check_path_for_bin(bin)
path = bin
else
path = dtutils_file.get_executable_path_preference(bin)
-- reset path preference is the returned preference is a directory
-- reset path preference if the returned preference is a directory
if dtutils_file.test_file(path, "d") then
dtutils_file.set_executable_path_preference(bin, "")
path = nil
Expand All @@ -318,6 +318,19 @@ local function _check_path_for_bin(bin)
return result
end

local function _check_preferences_for_bin(bin)

local result = dtutils_file.get_executable_path_preference(bin)

if result:len() == 0 then
result = false
end

return result
end



--[[
local function to the old check_if_bin_exists functionality
on windows in order to decrease the amount of windows being
Expand Down Expand Up @@ -385,12 +398,22 @@ function dtutils_file.check_if_bin_exists(bin)
result = _old_check_if_bin_exists(bin)
else

result = _check_path_for_bin(bin)
result = _check_preferences_for_bin(bin)

if not result then
result = _check_path_for_bin(bin)
end

if not result then
result = _search_for_bin(bin)
end
end

if not result then
-- make an entry so that executable_manager can be used to fix it
dtutils_file.set_executable_path_preference(bin, "")
end

return result
end

Expand Down Expand Up @@ -674,6 +697,62 @@ function dtutils_file.create_unique_filename(filepath)
return filepath
end

-- sorted list of executable names
dtutils_file.known_executables = {}

-- executable name/path pairs
dtutils_file.executable_paths = {}

-- initialized flag
dtutils_file.executables_initialized = false

local function _find_known_executable(name)
local found = false
for _, bin in ipairs(dtutils_file.known_executables) do
if name == bin then
found = true
end
end

return found
end

local function _get_known_executables()
-- read the known executables preference and populate the tables

local executables = dt.preferences.read("executable_manager", "known_executables", "string")

if executables then
local names = du.split(executables, ",")

for _, name in ipairs(names) do
table.insert(dtutils_file.known_executables, name)
dtutils_file.executable_paths[name] = dtutils_file.get_executable_path_preference(name)
end

table.sort(dtutils_file.known_executables)

dtutils_file.executables_initialized = true
end
end

local function _save_known_executables()
-- write the preference with the known executable names
local pref = du.join(dtutils_file.known_executables, ",")
dt.preferences.write("executable_manager", "known_executables", "string", pref)
end

local function _add_known_executable(name)
-- add a known executable to the table
if not dtutils_file.executables_initialized then
_get_known_executables()
end

table.insert(dtutils_file.known_executables, name)
table.sort(dtutils_file.known_executables)
_save_known_executables()
end


dtutils_file.libdoc.functions["set_executable_path_preference"] = {
Name = [[set_executable_path_preference]],
Expand All @@ -696,6 +775,10 @@ dtutils_file.libdoc.functions["set_executable_path_preference"] = {

function dtutils_file.set_executable_path_preference(executable, path)
dt.preferences.write("executable_paths", executable, "string", path)
dtutils_file.executable_paths[executable] = path
if not _find_known_executable(executable) then
_add_known_executable(executable)
end
end


Expand All @@ -718,7 +801,11 @@ dtutils_file.libdoc.functions["get_executable_path_preference"] = {
}

function dtutils_file.get_executable_path_preference(executable)
return dt.preferences.read("executable_paths", executable, "string")
if dtutils_file.executables_initialized then
return dtutils_file.executable_paths(executable)
else
return dt.preferences.read("executable_paths", executable, "string")
end
end


Expand Down
51 changes: 37 additions & 14 deletions tools/executable_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,53 @@ end
-- M A I N P R O G R A M
-- - - - - - - - - - - - - - - - - - - - - - - - - - - -

local DARKTABLERC = dt.configuration.config_dir .. PS .. "darktablerc"
local exec_table = {}

local DARKTABLERC <const> = dt.configuration.config_dir .. PS .. "darktablerc"

-- grep the executable_paths statements out of the darktablerc file
-- see if we have known executables

local matches = grep(DARKTABLERC, "executable_paths")
local known_executables = dt.preferences.read("executable_manager", "known_executables", "string")

-- check if we have something to manage and exit if not
dt.print_log("known_executables is " .. tostring(known_executables))

if #matches == 0 then
dt.print(_("no executable paths found, exiting..."))
return
end
if not known_executables or known_executables:len() == 0 then

-- build a table of the path preferences
known_executables = ""

local exec_table = {}
-- grep the executable_paths statements out of the darktablerc file

local matches = grep(DARKTABLERC, "executable_paths")

for _,pref in ipairs(matches) do
local parts = du.split(pref, "=")
local tmp = du.split(parts[1], "/") -- preferences are stored with forward slashes
table.insert(exec_table, tmp[#tmp])
-- check if we have something to manage and exit if not

if #matches == 0 then
dt.print(_("no executable paths found, exiting..."))
return
end

for _,pref in ipairs(matches) do
dt.print_log("processing pref " .. pref)
local parts = du.split(pref, "=")
dt.print_log("parts 1 is " .. parts[1])
local tmp = du.split(parts[1], "/") -- preferences are stored with forward slashes
known_executables = known_executables .. tmp[#tmp] .. ","
end

-- remove the trailing comma

known_executables = known_executables:sub(1, -2)

dt.preferences.write("executable_manager", "known_executables", "string", known_executables)
end

dt.print_log("known_executables is " .. known_executables)


-- build a table of the path preferences

local exec_table = du.split(known_executables, ",")

local executable_path_widgets = {}
local executable_path_values = {}
local placeholder_text = dt.configuration.running_os == windows and _("select an executable") or _("search path for executable")
Expand Down