From 311481f8000c1caf4cbb6b9f768cd68be54844ca Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 2 Oct 2023 13:54:53 +0530 Subject: repo: os.execute/io.popen -> util.run --- lib/util.lua | 82 ++++++++++++++++++++++++++++++------------------------------ main.lua | 2 +- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/util.lua b/lib/util.lua index f80ae87..a4d3034 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -1,5 +1,41 @@ #!/usr/bin/env lua +local table_to_cmd = function (t) + local str = "" + + for _, v in ipairs(t) do + v = v:gsub("'", "\'") + str = str .. " '" .. v .. "' " + end + + return str +end + +local run = function (args) + local sig, rc, stdout, cmd + + if mp then + cmd = mp.command_native({ + name = 'subprocess', + capture_stdout = true, + args = args, + }) + if cmd then + stdout = cmd.stdout + rc = (cmd.status >= 0) + end + else + cmd = io.popen(table_to_cmd(args)) + if cmd then + stdout = cmd:read('*all') + _, sig, rc = cmd:close() + rc = (sig == 'signal') or (sig == 'exit' and rc == 0) + end + end + + return stdout or "", rc +end + local table_merge = function (t1, t2) local t = {} t1 = t1 or {} @@ -48,15 +84,15 @@ local table_match_or_any = function (t, key) end local zip_ext_first = function (zip, out) - local dir, rc, srt + local dir, rc, srt, findcmd dir = os.tmpname() os.remove(dir) - os.execute('mkdir -p ' .. dir) + findcmd = { 'find', dir, '-type', 'f', '-name', '*.srt' } - rc = os.execute('unzip -qq ' .. zip .. ' -d ' .. dir) - srt = io.popen('find ' .. dir .. ' -type f -name *.srt'):read('*l') - os.execute("mv '" .. srt .. "' '" .. out .. "'") + _, rc = run({ 'unzip', '-qq', zip, '-d', dir }) + srt = run(findcmd):match('[^\n]*') + run({ 'mv', srt, out }) os.remove(dir) return rc @@ -141,42 +177,6 @@ local opensubtitles_hash = function (fileName) return string.format("%08x%08x", hi,lo), size end -local table_to_cmd = function (t) - local str = "" - - for _, v in ipairs(t) do - v = v:gsub("'", "\'") - str = str .. " '" .. v .. "' " - end - - return str -end - -local run = function (args) - local sig, rc, stdout, cmd - - if mp then - cmd = mp.command_native({ - name = 'subprocess', - capture_stdout = true, - args = args, - }) - if cmd then - stdout = cmd.stdout - rc = (cmd.status >= 0) - end - else - cmd = io.popen(table_to_cmd(args)) - if cmd then - stdout = cmd:read('*all') - _, sig, rc = cmd:close() - rc = (sig == 'signal') or (sig == 'exit' and rc == 0) - end - end - - return stdout or "", rc -end - return { table_merge = table_merge, table_print = table_print, diff --git a/main.lua b/main.lua index 975f96c..097a53a 100644 --- a/main.lua +++ b/main.lua @@ -9,7 +9,7 @@ local mkdir = function (path) os.remove(path) end - return os.execute('mkdir -p ' .. path) + return util.run({ 'mkdir', '-p', path }) end local sub_needed = function () -- cgit v1.2.3