-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeagisub
executable file
·368 lines (293 loc) · 10.4 KB
/
peagisub
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
#!/usr/bin/env lua
-----------------------------------------------------------------------------
-- This is the wrapper to our lua library trigger it as a command
--
-- Part of https://github.com/sosie-js/peagisub-vs
-- Discussion on https://github.com/luarocks/luarocks/issues/1694
-- (c) 2024 Sosie / sos-productions.com)
-- requires optparse from G.V.Vaughan
-- uses luarocks powers from Hisham Muhammad
--
-- Version 1.7
--
-- License: MIT/X,
-----------------------------------------------------------------------------
-- Aegisub script declaration style matching declaration in the builtin
script_name="Generate Aegisub config file"
script_description="Exports paths specifiers to a config file to be used by python"
script_author="SoSie-js / github"
script_version="1.7"
--Command extension, provides more details
script_command="peagisub"
script_copyright=[[Copyright © 2024 sos-productions.com]]
script_warranty= [[This program comes with ABSOLUTELY NO WARRANTY."]]
script_footer=[[See documentation at <http://github.com/sosie-js/peagisub/>
Please report bugs at <http://github.com/sosie-js/peagisub/issues>.]]
------------------------------------------------------------------------------
require("os")
---Execute a command and handle errors
-- This add stderr support to io.popen,
-- i tried many but none worked
--@param cmd string shell command
--@return output
--@return err where output is stdout and err is stderr message
local function os_execute(cmd)
local _stdout, _stderr
local file_stdout=os.tmpname()
local file_stderr=os.tmpname()
mask_stdout=true
mask_stderr=true
--io.stdout:setvbuf 'no'
--io.stderr:setvbuf 'no'
local out=' > '..file_stdout
local err= ' 2> '..file_stderr
--local pipe = io.popen(cmd.. out.. err)
--local exe = tonumber(pipe:read())
--pipe:close()
os.execute(cmd.. out.. err)
--io.stdout:flush()
--io.stderr:flush()
_stdout,err=io.open(file_stdout,"r")
if not _stdout then
os.remove(file_stdout)
os.remove(file_stderr)
return nil,"Could not open stdout: "..err
else
out=tostring(_stdout:read("*all"))
out= (out:gsub("\r\n", "\n"):gsub("\n$", "")) -- remove final newline
io.close(_stdout)
os.remove(file_stdout)
end
_stderr,err=io.open(file_stderr,"r")
if not _stderr then
os.remove(file_stderr)
return nil, "Could not open stderr: "..err
else
_stderr:flush()
err=tostring(_stderr:read("*all"))
err= (err:gsub("\r\n", "\n"):gsub("\n$", "")) -- remove final newline
io.close(_stderr)
os.remove(file_stderr)
end
return out, err
end
--[[We could have used require "luarocks.loader"
but it is *NOT* recognized in aegisub/luajit at least
when luarock is installed as user so we have
to built our own loader by hand using monkey patching technique]]
-- @return nothing
local luarocks_loader=function()
local ok, mymod = pcall(require, "luarocks.loader")
-- Debug helpers
--dump locals
--for x, v in pairs(locals()) do print(x, v) end
function locals()
local variables = {}
local idx = 1
while true do
local ln, lv = debug.getlocal(2, idx)
if ln ~= nil then
variables[ln] = lv
else
break
end
idx = 1 + idx
end
return variables
end
--dump globals
--for x, v in pairs(globals()) do print(x, v) end
--for x, v in pairs(getfenv()) do print(x, v) end
function globals()
return _G
end
function dump(someuserdata)
local report=getmetatable(someuserdata)
if report ~=nil then
print(inspect(report))
else
print(tostring(report))
end
end
local executable=function(cmd)
--[[local bindir = debug.getinfo(2, "S").source:sub(2):gsub("peagisub","")
local f=io.open(bindir..cmd,"r")
if f~=nil then io.close(f) return true else return false end]]
out, err= os_execute('type '..cmd)
return not string.find(err,"not found")
end
if(not ok and not executable('luarocks') and aegisub == nil) then
error("Module luarock is missing install it with the install.sh script")
end
--LUA_LIBEXT = package.cpath:match("%p[\\|/]?%p(%a+)$")
LUA_LIBEXT = package.cpath:match("\.(%a+)$")
--local
IS_LINUX= false
IS_MACOSX = false
if LUA_LIBEXT== "dll" then
-- IS_WINDOWS is set later by require "lua-path"
-- luarocks working direcory
LUAROCKS_BASE = os.getenv( "APPDATA" ) .. "\\luarocks" --not sure
elseif LUA_LIBEXT == "so" then
IS_LINUX=true
LUAROCKS_BASE = os.getenv( "HOME" ) .. "/.luarocks"
elseif LUA_LIBEXT == "dylib" then
IS_MACOSX=true
LUAROCKS_BASE = os.getenv( "HOME" ) .. "/.luarocks" --not sure
else
--for x, v in pairs(globals()) do print(x, v) end
error("luarocks_loader error: unsupported LUA_LIBEXT '"..LUA_LIBEXT.."' read from package.cpath '".. package.cpath.."'")
end
-- Declares local path in case of lua-path was installed as user in local
LUA_VERSION=tostring(_VERSION:gsub("Lua ", ""))
function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str -- str:match("(.*[/\\])")
end
--Lightweight version cfg.init_package_paths()
--of https://github.com/luarocks/luarocks/blob/master/src/luarocks/core/cfg.lua
--limited to the user tree where all is installed in local
if not ok then
--LUA_PATH
package.path = package.path .. ';' .. LUAROCKS_BASE .. "/share/lua/" .. LUA_VERSION.."/?.lua".. ';' .. LUAROCKS_BASE .. "/share/lua/".. LUA_VERSION .."/?/init.lua"
--LUA_CPATH
package.cpath = package.cpath .. ';' .. LUAROCKS_BASE.. "/lib/lua/"..LUA_VERSION .."/?."..LUA_LIBEXT .. ';' .. LUAROCKS_BASE .. "/lib/lua/"..LUA_VERSION .."/?/init."..LUA_LIBEXT
else
--print(package.path)
--print(package.cpath)
end
end
luarocks_loader()
--Now we are in luarocks world, we can load dependencies
local ok, peagisub = pcall(require, 'peagisub')
if not ok then
error("Module peagisub is missing install it with\n luarocks install --local peagisub \n Check error:"..peagisub)
end
--[[
local options={
vsvars={
userplugin="retrieve the userplugin path from vsvar config file",
systemplugin="retrieve the systemplugin path from vsvars config file",
cache="retrieve the cache path where lwi index are saved",
luadir="retrieve the automation lua dir whre aegisub-vs.lua is stored",
vsdir="retrieve the vapoursynth dir where aegisub-vs.py is stored"
},
vscmds={
createconfigfile="creates the vsvars config file that stores all the useful paths to set up vapoursynth on python side.",
fixconfigfile="Fix vsvars.json configfile and vapoursynth.conf it depends on"
}
}]]
local options=peagisub.options
local ok, OptionParser = pcall(require, 'optparse')
if not ok then
error("Module optparse is missing install it with\n luarocks install --local optparse")
end
local help = [[
]]..script_command..[[ ]]..script_version..[[
]]..script_copyright..[[
]]..script_warranty..[[
Usage: ]]..script_command..[[ [<options>]
]]..script_name ..[[
]]..script_description..[[
Options:
-h, --help display this help, then exit
--version display version information, then exit]]
local configs={"config-path";"config-cpath"}
--- Get the config param
-- @param name of the variable
-- @return string content
function config(name)
--This may not be sufficient, because if luarocks is not
-- installed in the same tree, this last that all depend on ,
-- will not be reachable
if name =="config-path" then
return package.path
end
if name =="config-cpath" then
return package.cpath
end
return "'"..name:gsub(0,15).."' is not defined in config "
end
function add_option(options, type, name, desc)
if options[type] == nil then
options[type]={}
end
options[type][name]=desc
return options
end
--mandatory
options=add_option(options, "config", "config-path", "retrieve package.path installed by the wrap_script")
options=add_option(options, "config", "config-cpath", "retrieve package.cpath installed by the wrap_script")
if(#options.vsvars) then
help = help..[[
vars : ]]
end
for k,v in pairs(options.vsvars) do
help = help..[[
--]]..k..[[ ]]..v
end
--- Get the content of a variable from the config file (vsvars.conf)
-- @param name of the variable
-- @return string content
function vsvar(name)
--[[cmd='print(peagisub.vsvar("'..name..'")); os.exit()'
cmd="lua -lluarocks.loader -lpeagisub -e '"..cmd.."'"
--cmd="lua -lluarocks.loader -lpeagisub -e 'os.exit()'"
local out, err= os_execute(cmd)
if(err~="") then
error("Error vsvar'"..tostring(err).."'")
end}}
return out]]
return peagisub.vsvar(name)
end
if(#options.vscmds) then
help = help..[[
commands : ]]
end
for k,v in pairs(options.vscmds) do
help = help..[[
--]]..k..[[ ]]..v
end
help = help..[[
-- end of options
]]..script_footer
local parser = OptionParser (help)
for k,v in pairs(options.config) do
parser:on('--'..k, parser.optional, parser.boolean)
end
for k,v in pairs(options.vsvars) do
parser:on('--'..k, parser.optional, parser.boolean)
end
for k,v in pairs(options.vscmds) do
parser:on('--'..k, parser.optional, parser.boolean)
end
-- Executes a given command
-- @param cmd a command such as 'createconfigfile' or 'fixconfigfile'
-- @return output of the command
function vscmd(cmd)
--[[cmd='print(peagisub.vscmd("'..cmd..'")); os.exit()'
cmd="lua -lluarocks.loader -lpeagisub -e '"..cmd.."'"
local out, err= os_execute(cmd)
if(err~="") then
error("Error vscmd'"..tostring(err).."'")
end
return out]]
return peagisub.vscmd(cmd)
end
--Handle now provided args if given, else do nothing
--if _G.arg ~= nil then
local arg, opts = parser:parse (_G.arg)
io.stdout:setvbuf("no")
for x, v in pairs(opts) do
--print("'"..x.."'",v)
if configs[x] ~= nil and v == true then
print(config(x))
end
if options.vsvars[x] ~= nil and v == true then
print(vsvar(x))
end
if options.vscmds[x] ~= nil and v == true then
print(vscmd(x))
end
end
io.stdout:flush()