forked from irr/lua-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwlua.lua
More file actions
executable file
·75 lines (61 loc) · 1.92 KB
/
Copy pathwlua.lua
File metadata and controls
executable file
·75 lines (61 loc) · 1.92 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
#!/usr/bin/env luajit
local posix = require('posix')
function run(l, t, s, f)
local cmd, status = string.format("wget -c --timeout=%d ", t), nil
if #l > 0 then
cmd = cmd .. string.format("--limit-rate=%s ", l)
end
if f:find("http://") then
cmd = cmd .. f
elseif f:find("https://") then
cmd = cmd .. string.format("--no-check-certificate %s", f)
else
cmd = cmd .. string.format("--no-check-certificate --user=%s --password=%s \"https://enderseed.shell.seedhost.eu/enderseed/downloads/irr/%s\"", os.getenv("U"), os.getenv("P"), f)
end
repeat
_, status = pcall(os.execute, cmd)
if status ~= 0 then
print(string.format("wlua: waiting for %d seconds [%s]...", s, f))
pcall(os.execute, "sleep " .. tonumber(s))
end
until (status == 0)
print(string.format("wlua: [%s] finished ok.", f))
end
function go(fn,...)
local cpid = posix.fork()
if cpid == 0 then
local res = fn(...)
posix._exit(res or 0)
else
return cpid
end
end
local l, t, s, f = "", 5, 5, nil
for i = 1, #arg do
if arg[i]:find("-t") == 1 and #arg[i] > 2 then
t = tonumber(arg[i]:sub(3))
elseif arg[i]:find("-s") == 1 and #arg[i] > 2 then
s = tonumber(arg[i]:sub(3))
elseif arg[i]:find("-l") == 1 and #arg[i] > 2 then
l = arg[i]:sub(3)
else
f = arg[i]
end
end
if f == nil then
print("Usage: wlua.lua [OPTIONS] <file or url (http/https)>")
print(" -l<s> = limit rate")
print(" -t<n> = timeout in seconds")
print(" -s<n> = retry interval in seconds")
os.exit(1)
end
print(string.format("wlua: downloading [%s] with timeout=[%d] and retry=[%d]...", f, t, s))
local ok, cpid = pcall(go, function() run(l, t, s, f) end)
if ok then
posix.signal(posix.SIGINT, function()
posix.kill(cpid)
end)
posix.wait(cpid)
else
os.exit(-1)
end