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
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file linuxos.lua
--
-- define module: linuxos
local linuxos = linuxos or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local semver = require("base/semver")
-- get lsb_release information
--
-- e.g.
--
-- Distributor ID: Ubuntu
-- Description: Ubuntu 16.04.7 LTS
-- Release: 16.04
-- Codename: xenial
--
function linuxos._lsb_release()
local lsb_release = linuxos._LSB_RELEASE
if lsb_release == nil then
local ok, result = os.iorun("lsb_release -a")
if ok then
lsb_release = result
end
if lsb_release then
lsb_release = lsb_release:trim():lower()
end
linuxos._LSB_RELEASE = lsb_release or false
end
return lsb_release or nil
end
-- get uname information
function linuxos._uname_r()
local uname_r = linuxos._UNAME_R
if uname_r == nil then
local ok, result = os.iorun("uname -r")
if ok then
uname_r = result
end
if uname_r then
uname_r = uname_r:trim():lower()
end
linuxos._UNAME_R = uname_r or false
end
return uname_r or nil
end
-- get system name
--
-- e.g.
-- - ubuntu
-- - debian
-- - archlinux
-- - rhel
-- - centos
-- - fedora
-- - opensuse
-- - ...
function linuxos.name()
local name = linuxos._NAME
if name == nil then
-- get it from /etc/os-release first
if name == nil and os.isfile("/etc/os-release") then
local os_release = io.readfile("/etc/os-release")
if os_release then
os_release = os_release:trim():lower()
if os_release:find("arch linux", 1, true) or os_release:find("archlinux", 1, true) then
name = "archlinux"
elseif os_release:find("centos linux", 1, true) or os_release:find("centos", 1, true) then
name = "centos"
elseif os_release:find("fedora", 1, true) then
name = "fedora"
elseif os_release:find("uos", 1, true) then
name = "uos"
elseif os_release:find("deepin", 1, true) then
name = "deepin"
elseif os_release:find("linux mint", 1, true) or os_release:find("linuxmint", 1, true) then
name = "linuxmint"
-- kylin contains keyword 'UBUNTU_CODENAME', so we check kylin before ubuntu
elseif os_release:find("kylin", 1, true) then
name = "kylin"
elseif os_release:find("ubuntu", 1, true) then
name = "ubuntu"
elseif os_release:find("debian", 1, true) then
name = "debian"
elseif os_release:find("gentoo linux", 1, true) then
name = "gentoo"
elseif os_release:find("opensuse", 1, true) then
name = "opensuse"
elseif os_release:find("manjaro", 1, true) then
name = "manjaro"
end
end
end
-- get it from lsb release
if name == nil then
local lsb_release = linuxos._lsb_release()
if lsb_release and lsb_release:find("ubuntu", 1, true) then
name = "ubuntu"
end
end
-- is archlinux?
if name == nil and os.isfile("/etc/arch-release") then
name = "archlinux"
end
-- unknown
name = name or "unknown"
linuxos._NAME = name
end
return name
end
-- get system version
function linuxos.version()
local version = linuxos._VERSION
if version == nil then
-- get it from /etc/os-release first
if version == nil and os.isfile("/etc/os-release") then
local os_release = io.readfile("/etc/os-release")
if os_release then
os_release = os_release:trim():lower():split("\n")
for _, line in ipairs(os_release) do
-- ubuntu: VERSION="16.04.7 LTS (Xenial Xerus)"
-- fedora: VERSION="32 (Container Image)"
-- debian: VERSION="9 (stretch)"
-- kylin : VERSION="V10(kylin)"
if line:find("version=") then
line = line:sub(9)
version = semver.match(line)
if not version then
version = line:match("\"(%d+)%s+.*\"")
if version then
version = semver.new(version .. ".0")
end
end
-- is kylin?
if not version and line:find("kylin", 1, true) then
version = line:match("\"v(%d+)%(kylin%)\"")
if version then
version = semver.new(version .. ".0")
end
end
if version then
break
end
end
end
end
end
-- get it from lsb release
if version == nil then
local lsb_release = linuxos._lsb_release()
if lsb_release and lsb_release:find("ubuntu", 1, true) then
for _, line in ipairs(lsb_release:split("\n")) do
-- release: 16.04
if line:find("release:") then
version = semver.match(line, 9)
if version then
break
end
end
end
end
end
linuxos._VERSION = version
end
return version
end
-- get linux kernel version
function linuxos.kernelver()
local version = linuxos._KERNELVER
if version == nil then
if version == nil then
local uname_r = linuxos._uname_r()
if uname_r then
version = semver.match(uname_r)
end
end
linuxos._KERNELVER = version
end
return version
end
-- return module: linuxos
return linuxos
|