-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
157 lines (128 loc) · 3.81 KB
/
util.lua
File metadata and controls
157 lines (128 loc) · 3.81 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
-- SPDX-License-Identifier: GPL-2.0
-- Copyright 2022-2024 - Fábio Rodrigues Ribeiro and contributors
local op = io.open
local pop = io.popen
local function success(cmd)
local r1, r2, r3 = os.execute(cmd)
if type(r1) == "boolean" then
return r1
elseif type(r1) == "number" then
return r1 == 0
else
return false
end
end
local function getoutput_base(cmd, opts)
local handle = assert(pop(cmd))
if not handle then error(("Erro ao chamar o comando: %s."):format(cmd)) end
if not success(cmd) then return nil end
local result = handle:read(opts)
handle:close()
-- handle = nil
if not result or result == "" then return nil, "Command output is empty" end
return result
end
local function getoutput_base2(cmd, opts)
local handle = assert(op(cmd))
if not handle then error(("Erro ao chamar o comando: %s."):format(cmd)) end
if not success(cmd) then return nil end
return handle:read(opts)
-- handle:close()
-- handle = nil
end
local function getoutput(cmd, opts)
return getoutput_base(cmd, "*l")
end
local function getoutput_all(cmd)
return getoutput_base(cmd, "*a")
end
local function open_stream(filename, opts)
local f = assert(op(filename, opts))
if not f then error(("Erro ao abrir o arquivo %s para leitura."):format(filename)) else return f end
end
local function shell_read(cmd)
local tmp = os.tmpname()
local sanitize_cmd = ("%s > %s"):format(cmd, tmp)
if not success(sanitize_cmd) then
os.remove(tmp)
return nil
end
local handle = getoutput_base2(sanitize_cmd, "*a")
local content = handle:gsub("^%s*(.-)%s*$", "%1")
handle:close()
handle = nil
os.remove(tmp)
return content
end
local function open_iter(filename)
local f = open_stream(filename, "r")
return
function()
f:seek("set", 0) -- volta pro início
local line = f and f:read "*a"
if not line then
f:close()
f = nil
end
return line
end
end
local function openfile_match(filename, string)
local result
for line in open_iter(filename) do
result = line:match(string)
if result then break end
end
return result
end
local function xargs()
local opts = ""
for i = 2, #arg do opts = ("%s %s"):format(opts, arg[i]) end
return opts
end
local function get_fullkernelversion()
return getoutput "uname -r":match "(%d+)%.(%d+)%.(%d+)" -- Executa o comando uname -r para obter a versão do kernel
end
local function get_kernelminorwithoutpatch()
local major, minor = get_fullkernelversion()
return ("%s.%d"):format(major, minor) -- Converte o valor do Minor para número e incrementa 1 e constrói a nova versão do kernel
end
local function get_nextkernelversion()
local major, minor, patch = get_fullkernelversion()
return ("%s.%s.%d"):format(major, minor, patch + 1) -- Converte o valor do Patch para número e incrementa 1 e constrói a nova versão do kernel
end
local function get_nextkernelminorwithoutpatch()
local major, minor = get_fullkernelversion()
return ("%s.%d"):format(major, minor + 1) -- Converte o valor do Minor para número e incrementa 1 e constrói a nova versão do kernel
end
local function sbversion() return getoutput "rpm -E %fedora" end
local function arch() return getoutput "uname -m" end
local function writecmd_x(cmd)
io.write(cmd .. "\n")
x(cmd)
end
local function writemsg_x(cmd, msg)
io.write(msg)
x(cmd)
end
local function x_writemsg(cmd, msg)
x(cmd)
io.write(msg)
end
local function escape(str) return "'" .. str:gsub("'", "'\\''") .. "'" end
return {
sbversion = sbversion,
arch = arch,
writecmd_x = writecmd_x,
writemsg_x = writemsg_x,
x_writemsg = x_writemsg,
openfile_match = openfile_match,
xargs = xargs,
get_nextkernelversion = get_nextkernelversion,
get_nextkernelminorwithoutpatch = get_nextkernelminorwithoutpatch,
get_kernelminorwithoutpatch = get_kernelminorwithoutpatch,
shell_read = shell_read,
escape = escape,
getoutput_all = getoutput_all,
getoutput = getoutput
}