-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathscript_manager.lua
More file actions
1630 lines (1268 loc) · 48 KB
/
script_manager.lua
File metadata and controls
1630 lines (1268 loc) · 48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
This file is part of darktable,
copyright (c) 2018, 2020, 2023, 2024 Bill Ferguson <wpferguson@gmail.com>
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
script_manager.lua - a tool for managing the darktable lua scripts
script_manager is designed to be called from the users luarc file and used to
manage the lua scripts.
On startup script_manager scans the lua scripts directory to see what scripts are present.
Scripts are sorted by 'folder' based on what sub-directory they are in. With no
additional script repositories iinstalled, the folders are contrib, examples, official
and tools. When a folder is selected the buttons show the script name and whether the
script is started or stopped. The button is a toggle, so if the script is stopped click
the button to start it and vice versa.
Features
* the number of script buttons shown can be changed to any number between 5 and 20. The
default is 10 buttons. This can be changed in the configuration action.
* additional repositories of scripts may be installed using from the install/update action.
* installed scripts can be updated from the install/update action. This includes extra
repositories that have been installed.
* the scripts can be disabled if desired from the install/update action. This can only
be reversed manually. To enable the "Disable Scripts" button, check the checkbox to
endable it. This is to prevent accidentally disabling the scripts. Click the
"Disable Scripts" button and the luarc file is renamed to luarc.disable. If at
a later time you want to enable the scripts again, simply rename the luarc.disabled
file to luarc and the scripts will run.
]]
local dt = require "darktable"
local du = require "lib/dtutils"
local df = require "lib/dtutils.file"
local ds = require "lib/dtutils.string"
local dtsys = require "lib/dtutils.system"
local log = require "lib/dtutils.log"
local dtdebug = require "darktable.debug"
local gettext = dt.gettext.gettext
local function _(msgid)
return gettext(msgid)
end
-- figure out where we are running from
local info = debug.getinfo(1, "S")
local scripts_path = info.source:sub(2)
dt.print_log("scripts path is " .. scripts_path)
-- assume user based
local system_based = false
if string.match(scripts_path, dt.configuration.data_dir) then
dt.print_log("script_manager using system based scripts")
system_based = true
end
-- api check
-- du.check_min_api_version("9.3.0", "script_manager")
-- - - - - - - - - - - - - - - - - - - - - - - -
-- C O N S T A N T S
-- - - - - - - - - - - - - - - - - - - - - - - -
-- script_manager required API version
local SM_API_VER_REQD <const> = "9.7.0"
-- path separator
local PS <const> = dt.configuration.running_os == "windows" and "\\" or "/"
-- command separator
local CS <const> = dt.configuration.running_os == "windows" and "&" or ";"
local MODULE <const> = "script_manager"
local MIN_BUTTONS_PER_PAGE <const> = 5
local MAX_BUTTONS_PER_PAGE <const> = 20
local DEFAULT_BUTTONS_PER_PAGE <const> = 10
local DEFAULT_LOG_LEVEL <const> = log.warn
local USER_LUA_DIR <const> = dt.configuration.config_dir .. PS .. "lua"
local SYSTEM_LUA_DIR <const> = dt.configuration.data_dir .. PS .. "lua-scripts"
local LUA_SCRIPT_REPO <const> = "https://github.com/darktable-org/lua-scripts.git"
local LUA_API_VER <const> = "API-" .. dt.configuration.api_version_string
-- local POWER_ICON = dt.configuration.config_dir .. "/lua/data/data/icons/power.png"
local USER_POWER_ICON <const> = dt.configuration.config_dir .. "/lua/data/icons/path20.png"
local USER_BLANK_ICON <const> = dt.configuration.config_dir .. "/lua/data/icons/blank20.png"
local SYSTEM_POWER_ICON <const> = dt.configuration.data_dir .. "/lua-scripts/data/icons/path20.png"
local SYSTEM_BLANK_ICON <const> = dt.configuration.data_dir .. "/lua-scripts/data/icons/blank20.png"
local LUA_DIR = USER_LUA_DIR
local POWER_ICON = USER_POWER_ICON
local BLANK_ICON = USER_BLANK_ICON
if system_based then
LUA_DIR = SYSTEM_LUA_DIR
POWER_ICON = SYSTEM_POWER_ICON
BLANK_ICON = SYSTEM_BLANK_ICON
end
-- - - - - - - - - - - - - - - - - - - - - - - -
-- P R E F E R E N C E S
-- - - - - - - - - - - - - - - - - - - - - - - -
dt.preferences.register(MODULE, "check_update", "bool",
_("check for updated scripts on start up"),
_("automatically update scripts to correct version"),
true)
local check_for_updates = dt.preferences.read(MODULE, "check_update", "bool")
-- - - - - - - - - - - - - - - - - - - - - - - -
-- L O G L E V E L
-- - - - - - - - - - - - - - - - - - - - - - - -
local old_log_level = log.log_level()
log.log_level(DEFAULT_LOG_LEVEL)
-- - - - - - - - - - - - - - - - - - - - - - - -
-- N A M E S P A C E
-- - - - - - - - - - - - - - - - - - - - - - - -
local script_manager = {}
local sm = script_manager
sm.executables = {}
sm.executables.git = df.check_if_bin_exists("git")
sm.module_installed = false
sm.event_registered = false
-- set up tables to contain all the widgets and choices
sm.widgets = {}
sm.folders = {}
sm.translated_folders = {}
-- set log level for functions
sm.log_level = DEFAULT_LOG_LEVEL
--[[
sm.scripts is a table of tables for containing the scripts
It is organized as into folder (folder) subtables containing
each script definition, which is a table
sm.scripts-
|
- folder------------|
| - script
- folder----| |
- script|
| - script
- script|
and a script table looks like
name the name of the script file without the lua extension
path folder (folder), path separator, path, name without the lua extension
doc the header comments from the script to be used as a tooltip
script_name the folder, path separator, and name without the lua extension
running true if running, false if not, hidden if running but the
lib/storage/action for the script is hidden
has_lib true if it creates a module
lib_name name of the created lib
has_storage true if it creates a storage (exporter)
storage_name name of the exporter (in the exporter storage menu)
has_action true if it creates an action
action_name name on the button
has_select true if it creates a select
select_name name on the button
has_event true if it creates an event handler
event_type type of event, shortcut, post-xxx, pre-xxx
callback name of the callback routine
initialized all of the above data has been retreived and set. If the
script is unloaded and reloaded we don't have to reparse the file
]]
sm.scripts = {}
sm.start_queue = {}
sm.page_status = {}
sm.page_status.num_buttons = DEFAULT_BUTTONS_PER_PAGE
sm.page_status.buttons_created = 0
sm.page_status.current_page = 0
sm.page_status.folder = ""
-- installed script repositories
sm.installed_repositories = {
{name = "lua-scripts", directory = LUA_DIR},
}
-- don't let it run until everything is in place
sm.run = false
-- - - - - - - - - - - - - - - - - - - - - - - -
-- F U N C T I O N S
-- - - - - - - - - - - - - - - - - - - - - - - -
-------------------
-- helper functions
-------------------
local function set_log_level(level)
local old_log_level = log.log_level()
log.log_level(level)
return old_log_level
end
local function restore_log_level(level)
log.log_level(level)
end
local function pref_read(name, pref_type)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "name is " .. name .. " and type is " .. pref_type)
local val = dt.preferences.read(MODULE, name, pref_type)
log.msg(log.debug, "read value " .. tostring(val))
restore_log_level(old_log_level)
return val
end
local function pref_write(name, pref_type, value)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "writing value " .. tostring(value) .. " for name " .. name)
dt.preferences.write(MODULE, name, pref_type, value)
restore_log_level(old_log_level)
end
----------------
-- git interface
----------------
local function get_repo_status(repo)
local old_log_level = set_log_level(sm.log_level)
local p = io.popen("cd " .. repo .. CS .. "git status")
if p then
local data = p:read("*a")
p:close()
return data
end
log.msg(log.error, "unable to get status of " .. repo)
restore_log_level(old_log_level)
return nil
end
local function get_current_repo_branch(repo)
local old_log_level = set_log_level(sm.log_level)
local branch = nil
local p = io.popen("cd " .. repo .. CS .. "git branch --all")
if p then
local data = p:read("*a")
p:close()
local branches = du.split(data, "\n")
for _, b in ipairs(branches) do
log.msg(log.debug, "branch for testing is " .. b)
branch = string.match(b, "^%* (.-)$")
if branch then
log.msg(log.info, "current repo branch is " .. branch)
return branch
end
end
end
if not branch then
log.msg(log.error, "no current branch detected in repo_data")
end
restore_log_level(old_log_level)
return nil
end
local function get_repo_branches(repo)
local old_log_level = set_log_level(sm.log_level)
local branches = {}
local p = io.popen("cd " .. repo .. CS .. "git pull --all" .. CS .. "git branch --all")
if p then
local data = p:read("*a")
p:close()
log.msg(log.debug, "data is \n" .. data)
local branch_data = du.split(data, "\n")
for _, line in ipairs(branch_data) do
log.msg(log.debug, "line is " .. line)
local branch = string.gsub(line, "%s+remotes/%a+/", "")
if string.match(branch, "API") then
log.msg(log.info, "found branch - " .. branch)
table.insert(branches, branch)
end
end
end
restore_log_level(old_log_level)
return branches
end
local function is_repo_clean(repo_data)
local old_log_level = set_log_level(sm.log_level)
if string.match(repo_data, "\n%s-%a.-%a:%s-%a%g-\n") then
log.msg(log.info, "repo is dirty")
return false
else
log.msg(log.info, "repo is clean")
return true
end
restore_log_level(old_log_level)
end
local function checkout_repo_branch(repo, branch)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.info, "checkout out branch " .. branch .. " from repository " .. repo)
os.execute("cd " .. repo .. CS .. "git checkout " .. branch)
restore_log_level(old_log_level)
end
--------------------
-- utility functions
--------------------
local function update_combobox_choices(combobox, choice_table, selected)
local old_log_level = set_log_level(sm.log_level)
local items = #combobox
local choices = #choice_table
for i, name in ipairs(choice_table) do
combobox[i] = name
end
if choices < items then
for j = items, choices + 1, -1 do
combobox[j] = nil
end
end
if not selected then
selected = 1
end
combobox.value = selected
restore_log_level(old_log_level)
end
local function string_trim(str)
local old_log_level = set_log_level(sm.log_level)
local result = string.gsub(str, "^%s+", "") -- trim leading spaces
result = string.gsub(result, "%s+$", "") -- trim trailing spaces
result = string.gsub(result, ",?%s+%-%-.+$", "") -- trim trailing comma and comments
restore_log_level(old_log_level)
return result
end
local function string_dequote(str)
return string.gsub(str, "['\"]", "")
end
local function string_dei18n(str)
return string.match(str, "%_%((.+)%)")
end
local function string_chop(str)
return str:sub(1, -2)
end
------------------
-- script handling
------------------
local function is_folder_known(folder_table, name)
local match = false
for _, folder_name in ipairs(folder_table) do
if name == folder_name then
match = true
end
end
return match
end
local function find_translated_name(folder)
local translated_name = nil
if folder == "contrib" then
translated_name = _("contributed")
elseif folder == "examples" then
translated_name = _("examples")
elseif folder == "official" then
translated_name = _("official")
elseif folder == "tools" then
translated_name = _("tools")
else
translated_name = _(folder) -- in case we get lucky and the string got translated elsewhere
end
return translated_name
end
local function add_script_folder(folder)
local old_log_level = set_log_level(sm.log_level)
if #sm.folders == 0 or not is_folder_known(sm.folders, folder) then
table.insert(sm.folders, folder)
table.insert(sm.translated_folders, find_translated_name(folder))
sm.scripts[folder] = {}
log.msg(log.debug, "created folder " .. folder)
end
restore_log_level(old_log_level)
end
local function get_script_metadata(script)
local old_log_level = set_log_level(sm.log_level)
-- set_log_level(log.debug)
log.msg(log.debug, "processing metatdata for " .. script)
local metadata_block = nil
local metadata = {}
f = io.open(LUA_DIR .. PS .. script .. ".lua")
if f then
-- slurp the file
local content = f:read("*all")
f:close()
-- grab the script_data.metadata table
metadata_block = string.match(content, "script_data%.metadata = %{\r?\n(.-)\r?\n%}")
else
log.msg(log.error, "cant read from " .. script)
end
if metadata_block then
-- break up the lines into key value pairs
local lines = du.split(metadata_block, "\n")
log.msg(log.debug, "got " .. #lines .. " lines")
for i = 1, #lines do
log.msg(log.debug, "splitting line " .. lines[i])
local parts = du.split(lines[i], " = ")
parts[1] = string_trim(parts[1])
parts[2] = string_trim(parts[2])
log.msg(log.debug, "got value " .. parts[1] .. " and data " .. parts[2])
if string.match(parts[2], "%_%(") then
parts[2] = _(string_dequote(string_dei18n(parts[2])))
else
parts[2] = string_dequote(parts[2])
end
if string.match(parts[2], ",$") then
parts[2] = string_chop(parts[2])
end
log.msg(log.debug, "parts 1 is " .. parts[1] .. " and parts 2 is " .. parts[2])
metadata[parts[1]] = parts[2]
log.msg(log.debug, "metadata " .. parts[1] .. " is " .. metadata[parts[1]])
end
log.msg(log.debug, "script data found for " .. metadata["name"])
end
restore_log_level(old_log_level)
return metadata_block and metadata or nil
end
local function get_script_doc(script)
local old_log_level = set_log_level(sm.log_level)
local description = nil
f = io.open(LUA_DIR .. PS .. script .. ".lua")
if f then
-- slurp the file
local content = f:read("*all")
f:close()
-- assume that the second block comment is the documentation
description = string.match(content, "%-%-%[%[.-%]%].-%-%-%[%[(.-)%]%]")
else
log.msg(log.error, "can't read from " .. script)
end
if description then
restore_log_level(old_log_level)
return description
else
restore_log_level(old_log_level)
return _("no documentation available")
end
end
local function activate(script)
local old_log_level = set_log_level(sm.log_level)
local status = nil -- status of start function
local err = nil -- error message returned if module doesn't start
log.msg(log.info, "activating " .. script.name)
if script.running == false then
script_manager_running_script = script.name
status, err = du.prequire(script.path)
log.msg(log.debug, "prequire returned " .. tostring(status) .. " and for err " .. tostring(err))
script_manager_running_script = nil
if status then
pref_write(script.script_name, "bool", true)
log.msg(log.screen, _(string.format("loaded %s", script.script_name)))
script.running = true
if err ~= true then
log.msg(log.debug, "got lib data")
script.data = err
if script.data.destroy_method and script.data.destroy_method == "hide" and script.data.show and dt.gui.current_view().id == "lighttable" then
script.data.show()
end
else
script.data = nil
end
else
log.msg(log.screen, _(string.format("%s failed to load", script.script_name)))
log.msg(log.error, "error loading " .. script.script_name)
log.msg(log.error, "error message: " .. err)
end
else -- script is a lib and loaded but hidden and the user wants to reload
script.data.restart()
script.running = true
status = true
pref_write(script.script_name, "bool", true)
end
script_manager_running_script = "script_manager"
restore_log_level(old_log_level)
return status
end
local function deactivate(script)
-- presently the lua api doesn't support unloading lib elements however, we
-- can hide libs, so we just mark those as hidden and hide the gui
-- can delete storages
-- can delete actions
-- can delete selects
-- and mark them inactive for the next time darktable starts
-- deactivate it....
local old_log_level = set_log_level(sm.log_level)
pref_write(script.script_name, "bool", false)
if script.data then
script.data.destroy()
if script.data.destroy_method then
if string.match(script.data.destroy_method, "hide") then
script.running = "hidden"
else
package.loaded[script.script_name] = nil
script.running = false
end
else
package.loaded[script.script_name] = nil
script.running = false
end
log.msg(log.info, "turned off " .. script.script_name)
log.msg(log.screen, _(string.format("%s stopped", script.name)))
else
script.running = false
log.msg(log.info, "setting " .. script.script_name .. " to not start")
log.msg(log.screen, _(string.format("%s will not start when darktable is restarted", script.name)))
end
restore_log_level(old_log_level)
end
local function start_scripts()
for _, script in ipairs(sm.start_queue) do
activate(script)
for i = 1, sm.page_status.num_buttons do
local name = script.metadata and script.metadata.name or script.name
if sm.widgets.labels[i].label == name then
sm.widgets.buttons[i].name = "pb_on"
break
end
end
end
sm.start_queue = {}
end
local function queue_script_to_start(script)
table.insert(sm.start_queue, script)
end
local function add_script_name(name, path, folder)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "folder is " .. folder)
log.msg(log.debug, "name is " .. name)
local script = {
name = name,
path = folder .. "/" .. path .. name,
running = false,
doc = get_script_doc(folder .. "/" .. path .. name),
metadata = get_script_metadata(folder .. "/" .. path .. name),
script_name = folder .. "/" .. name,
data = nil
}
table.insert(sm.scripts[folder], script)
if pref_read(script.script_name, "bool") then
queue_script_to_start(script)
else
pref_write(script.script_name, "bool", false)
end
restore_log_level(old_log_level)
end
local function process_script_data(script_file)
local old_log_level = set_log_level(sm.log_level)
-- the script file supplied is folder/filename.filetype
-- the following pattern splits the string into folder, path, name, fileename, and filetype
-- for example contrib/gimp.lua becomes
-- folder - contrib
-- path -
-- name - gimp.lua
-- filename - gimp
-- filetype - lua
-- Thanks Tobias Jakobs for the awesome regulary expression
local pattern = "(.-)/(.-)(([^\\/]-)%.?([^%.\\/]*))$"
if dt.configuration.running_os == "windows" then
-- change the path separator from / to \ for windows
pattern = "(.-)\\(.-)(([^\\]-)%.?([^%.\\]*))$"
end
log.msg(log.info, "processing " .. script_file)
-- add the script data
local folder,path,name,filename,filetype = string.match(script_file, pattern)
if folder and name and path then
log.msg(log.debug, "folder is " .. folder)
log.msg(log.debug, "name is " .. name)
add_script_folder(folder)
add_script_name(name, path, folder)
end
restore_log_level(old_log_level)
end
local function ensure_lib_in_search_path(line)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "line is " .. line)
if string.match(line, ds.sanitize_lua(dt.configuration.config_dir .. PS .. "lua/lib")) then
log.msg(log.debug, line .. " is already in search path, returning...")
return
end
local pattern = dt.configuration.running_os == "windows" and "(.+)\\lib\\.+lua" or "(.+)/lib/.+lua"
local path = string.match(line, pattern)
log.msg(log.debug, "extracted path is " .. path)
log.msg(log.debug, "package.path is " .. package.path)
if not string.match(package.path, ds.sanitize_lua(path)) then
log.msg(log.debug, "path isn't in package.path, adding...")
package.path = package.path .. ";" .. path .. "/?.lua"
log.msg(log.debug, "new package.path is " .. package.path)
end
restore_log_level(old_log_level)
end
local function scan_scripts(script_dir)
local old_log_level = set_log_level(sm.log_level)
local script_count = 0
local find_cmd = "find -L " .. script_dir .. " -name \\*.lua -print | sort"
if dt.configuration.running_os == "windows" then
find_cmd = "dir /b/s \"" .. script_dir .. "\\*.lua\" | sort"
end
log.msg(log.debug, "find command is " .. find_cmd)
-- scan the scripts
local output = io.popen(find_cmd)
for line in output:lines() do
log.msg(log.debug, "line is " .. line)
local l = string.gsub(line, ds.sanitize_lua(LUA_DIR) .. PS, "") -- strip the lua dir off
local script_file = l:sub(1,-5) -- strip off .lua\n
if not string.match(script_file, "script_manager") then -- let's not include ourself
if not string.match(script_file, "plugins") then -- skip plugins
if not string.match(script_file, "lib" .. PS) then -- let's not try and run libraries
if not string.match(script_file, "%.git") then -- don't match files in the .git directory
process_script_data(script_file)
script_count = script_count + 1
end
else
ensure_lib_in_search_path(line) -- but let's make sure libraries can be found
end
end
end
end
restore_log_level(old_log_level)
return script_count
end
local function update_scripts()
local old_log_level = set_log_level(sm.log_level)
local result = false
local git = sm.executables.git
if not git then
log.msg(log.screen, _("ERROR: git not found. Install or specify the location of the git executable."))
return
end
local git_command = "cd " .. LUA_DIR .. " " .. CS .. " " .. git .. " pull"
log.msg(log.debug, "update git command is " .. git_command)
if dt.configuration.running_os == "windows" then
result = dtsys.windows_command(git_command)
else
result = os.execute(git_command)
end
if result == 0 then
log.msg(log.screen, _("lua scripts successfully updated"))
end
restore_log_level(old_log_level)
return result
end
--------------
-- UI handling
--------------
local function update_script_update_choices()
local old_log_level = set_log_level(sm.log_level)
local installs = {}
local pref_string = ""
for i, repo in ipairs(sm.installed_repositories) do
table.insert(installs, repo.name)
pref_string = pref_string .. i .. "," .. repo.name .. "," .. repo.directory .. ","
end
update_combobox_choices(sm.widgets.update_script_choices, installs, 1)
log.msg(log.debug, "repo pref string is " .. pref_string)
pref_write("installed_repos", "string", pref_string)
restore_log_level(old_log_level)
end
local function scan_repositories()
local old_log_level = set_log_level(sm.log_level)
local script_count = 0
local find_cmd = "find -L " .. LUA_DIR .. " -name \\*.git -print | sort"
if dt.configuration.running_os == "windows" then
find_cmd = "dir /b/s /a:d " .. LUA_DIR .. PS .. "*.git | sort"
end
log.msg(log.debug, "find command is " .. find_cmd)
local output = io.popen(find_cmd)
for line in output:lines() do
local l = string.gsub(line, ds.sanitize_lua(LUA_DIR) .. PS, "") -- strip the lua dir off
local folder = string.match(l, "(.-)" .. PS) -- get everything to the first /
if folder then -- if we have a folder (.git doesn't)
log.msg(log.debug, "found folder " .. folder)
if not string.match(folder, "plugins") and not string.match(folder, "%.git") then -- skip plugins
if #sm.installed_repositories == 1 then
log.msg(log.debug, "only 1 repo, adding " .. folder)
table.insert(sm.installed_repositories, {name = folder, directory = LUA_DIR .. PS .. folder})
else
log.msg(log.debug, "more than 1 repo, we have to search the repos to make sure it's not there")
local found = nil
for _, repo in ipairs(sm.installed_repositories) do
if string.match(repo.name, ds.sanitize_lua(folder)) then
log.msg(log.debug, "matched " .. repo.name)
found = true
break
end
end
if not found then
table.insert(sm.installed_repositories, {name = folder, directory = LUA_DIR .. PS .. folder})
end
end
end
end
end
update_script_update_choices()
restore_log_level(old_log_level)
end
local function install_scripts()
local old_log_level = set_log_level(sm.log_level)
local url = sm.widgets.script_url.text
local folder = sm.widgets.new_folder.text
if string.match(du.join(sm.folders, " "), ds.sanitize_lua(folder)) then
log.msg(log.screen, _(string.format("folder %s is already in use. Please specify a different folder name.", folder)))
log.msg(log.error, "folder " .. folder .. " already exists, returning...")
restore_log_level(old_log_level)
return
end
local result = false
local git = sm.executables.git
if not git then
log.msg(log.screen, _("ERROR: git not found. Install or specify the location of the git executable."))
restore_log_level(old_log_level)
return
end
local git_command = "cd " .. LUA_DIR .. " " .. CS .. " " .. git .. " clone " .. url .. " " .. folder
log.msg(log.debug, "update git command is " .. git_command)
if dt.configuration.running_os == "windows" then
result = dtsys.windows_command(git_command)
else
result = dtsys.external_command(git_command)
end
log.msg(log.info, "result from import is " .. result)
if result == 0 then
local count = scan_scripts(LUA_DIR .. PS .. folder)
if count > 0 then
update_combobox_choices(sm.widgets.folder_selector, sm.folders, sm.widgets.folder_selector.selected)
dt.print(_(string.format("scripts successfully installed into folder %s"), folder))
table.insert(sm.installed_repositories, {name = folder, directory = LUA_DIR .. PS .. folder})
update_script_update_choices()
for i = 1, #sm.widgets.folder_selector do
if string.match(sm.widgets.folder_selector[i], ds.sanitize_lua(folder)) then
log.msg(log.debug, "setting folder selector to " .. i)
sm.widgets.folder_selector.selected = i
break
end
i = i + 1
end
log.msg(log.debug, "clearing text fields")
sm.widgets.script_url.text = ""
sm.widgets.new_folder.text = ""
sm.widgets.main_menu.selected = 3
else
log.msg(log.screen, _("no scripts found to install"))
log.msg(log.error, "scan_scripts returned " .. count .. " scripts found. Not adding to folder_selector")
end
else
log.msg(log.screen, _("failed to download scripts"))
end
restore_log_level(old_log_level)
return result
end
local function clear_button(number)
local old_log_level = set_log_level(sm.log_level)
local button = sm.widgets.buttons[number]
local label = sm.widgets.labels[number]
button.image = BLANK_ICON
button.tooltip = ""
button.sensitive = false
label.label = ""
button.name = ""
restore_log_level(old_log_level)
end
local function find_script(folder, name)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "looking for script " .. name .. " in folder " .. folder)
for _, script in ipairs(sm.scripts[folder]) do
if string.match(script.name, "^" .. ds.sanitize_lua(name) .. "$") then
return script
end
end
restore_log_level(old_log_level)
return nil
end
local function populate_buttons(folder, first, last)
local old_log_level = set_log_level(sm.log_level)
log.msg(log.debug, "folder is " .. folder .. " and first is " .. first .. " and last is " .. last)
local button_num = 1
for i = first, last do
local script = sm.scripts[folder][i]
local button = sm.widgets.buttons[button_num]
local label = sm.widgets.labels[button_num]
if script.running == true then
button.name = "pb_on"
else
button.name = "pb_off"
end
button.image = POWER_ICON
label.label = script.metadata and script.metadata.name or script.name
label.name = "pb_label"
button.ellipsize = "end"
button.sensitive = true
label.tooltip = script.metadata and script.metadata.purpose or script.doc