Skip to content

Commit 21358d6

Browse files
[qa] Added lua formatter #148
Closes #148
1 parent 34b1e3a commit 21358d6

19 files changed

Lines changed: 1141 additions & 1128 deletions

.github/workflows/ci.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ jobs:
3131
run: pip install openwisp-utils[qa]>=0.7
3232

3333
- name: QA-Checks
34-
run: openwisp-qa-check --skip-checkmigrations --skip-isort --skip-flake8 --skip-black
34+
run: ./run-qa-checks
35+
env:
36+
CI: 1
37+
38+
- name: Run sh-checker
39+
uses: luizm/action-sh-checker@master
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
with:
43+
sh_checker_comment: true
44+
sh_checker_shellcheck_disable: true
3545

3646
- name: Tests
3747
run: ./runtests

README.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,23 @@ You can inspect the version of openwisp-config currently installed with::
428428
429429
openwisp_config --version
430430
431+
Quality Assurance Checks
432+
------------------------
433+
434+
We use `LuaFormatter <https://luarocks.org/modules/tammela/luaformatter>`_ and `shfmt <https://github.com/mvdan/sh#shfmt>`_ to format lua files and shell scripts respectively.
435+
436+
Once they are installed, you can format all files by::
437+
438+
./qa-format
439+
440+
Run quality assurance tests with::
441+
442+
#install openwisp-utils QA tools first
443+
pip install openwisp-utils[qa]
444+
445+
#run QA checks before committing code
446+
./run-qa-checks
447+
431448
Run tests
432449
---------
433450

openwisp-config/files/lib/openwisp/net.lua

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ local uci = require('uci')
33
local net = {}
44

55
function net.get_interface(name, family)
6-
local uci_cursor = uci.cursor()
7-
local ip_family = family or 'inet'
8-
-- if UCI network name is a bridge, the ifname won't be the name of the bridge
9-
local is_bridge = uci_cursor:get('network', name, 'type') == 'bridge'
10-
local ifname
11-
if is_bridge then
12-
ifname = 'br-' .. name
13-
else
14-
-- get ifname from network configuration or
15-
-- default to supplied name if none is found
16-
ifname = uci_cursor:get('network', name, 'ifname') or name
6+
local uci_cursor = uci.cursor()
7+
local ip_family = family or 'inet'
8+
-- if UCI network name is a bridge, the ifname won't be the name of the bridge
9+
local is_bridge = uci_cursor:get('network', name, 'type') == 'bridge'
10+
local ifname
11+
if is_bridge then
12+
ifname = 'br-' .. name
13+
else
14+
-- get ifname from network configuration or
15+
-- default to supplied name if none is found
16+
ifname = uci_cursor:get('network', name, 'ifname') or name
17+
end
18+
-- get list of interfaces and loop until found
19+
local interfaces = nixio.getifaddrs()
20+
for _, interface in pairs(interfaces) do
21+
if interface.name == ifname and interface.family == ip_family then
22+
return interface
1723
end
18-
-- get list of interfaces and loop until found
19-
local interfaces = nixio.getifaddrs()
20-
for _, interface in pairs(interfaces) do
21-
if interface.name == ifname and interface.family == ip_family then
22-
return interface
23-
end
24-
end
25-
-- return nil if nothing is found
26-
return nil
24+
end
25+
-- return nil if nothing is found
26+
return nil
2727
end
2828

2929
return net

openwisp-config/files/lib/openwisp/utils.lua

Lines changed: 105 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,38 @@ local lfs = require('lfs')
44

55
local utils = {}
66

7-
function utils.starts_with_dot(str)
8-
return str:sub(1, 1) == '.'
9-
end
7+
function utils.starts_with_dot(str) return str:sub(1, 1) == '.' end
108

119
function utils.split(input, sep)
12-
if input == '' or input == nil then
13-
return {}
14-
end
15-
if sep == nil then
16-
sep = '%s'
17-
end
18-
local t={}; local i=1
19-
for str in string.gmatch(input, '([^' .. sep .. ']+)') do
20-
t[i] = str
21-
i = i + 1
22-
end
23-
return t
10+
if input == '' or input == nil then return {} end
11+
if sep == nil then sep = '%s' end
12+
local t = {};
13+
local i = 1
14+
for str in string.gmatch(input, '([^' .. sep .. ']+)') do
15+
t[i] = str
16+
i = i + 1
17+
end
18+
return t
2419
end
2520

2621
function utils.basename(path)
27-
local parts = utils.split(path, '/')
28-
return parts[table.getn(parts)]
22+
local parts = utils.split(path, '/')
23+
return parts[table.getn(parts)]
2924
end
3025

3126
function utils.dirname(path)
32-
local parts = utils.split(path, '/')
33-
local returnPath = '/'
34-
local length = table.getn(parts)
35-
for i, part in ipairs(parts) do
36-
if i < length then
37-
returnPath = returnPath..part..'/'
38-
end
39-
end
40-
return returnPath
27+
local parts = utils.split(path, '/')
28+
local returnPath = '/'
29+
local length = table.getn(parts)
30+
for i, part in ipairs(parts) do
31+
if i < length then returnPath = returnPath .. part .. '/' end
32+
end
33+
return returnPath
4134
end
4235

4336
function utils.add_values_to_set(set, values)
44-
for _, el in pairs(values) do
45-
set[el] = true
46-
end
47-
return set
37+
for _, el in pairs(values) do set[el] = true end
38+
return set
4839
end
4940

5041
-- writes uci section, eg:
@@ -64,148 +55,129 @@ end
6455
-- option ifname 'eth0.2'
6556
--
6657
function utils.write_uci_section(cursor, config, section, merge_list)
67-
local name
68-
-- add named section
69-
if not section['.anonymous'] then
70-
name = section['.name']
71-
cursor:set(config, name, section['.type'])
58+
local name
59+
-- add named section
60+
if not section['.anonymous'] then
61+
name = section['.name']
62+
cursor:set(config, name, section['.type'])
7263
-- add anonymous section
73-
else
74-
name = cursor:add(config, section['.type'])
75-
end
76-
-- write options for section
77-
for key, value in utils.sorted_pairs(section) do
78-
utils.write_uci_option(cursor, config, name, key, value, merge_list)
79-
end
64+
else
65+
name = cursor:add(config, section['.type'])
66+
end
67+
-- write options for section
68+
for key, value in utils.sorted_pairs(section) do
69+
utils.write_uci_option(cursor, config, name, key, value, merge_list)
70+
end
8071
end
8172

8273
-- abstraction for "uci set" which handles corner cases
8374
function utils.write_uci_option(cursor, config, name, key, value, merge_list)
84-
-- ignore properties starting with .
85-
if utils.starts_with_dot(key) then
86-
return
87-
end
88-
-- avoid duplicate list settings
89-
if type(value) == 'table' and merge_list then
90-
-- create set with unique values
91-
local set = {}
92-
-- read existing value
93-
local current = cursor:get(config, name, key)
94-
if type(current) == 'table' then
95-
set = utils.add_values_to_set(set, current)
96-
end
97-
set = utils.add_values_to_set(set, value)
98-
-- reset value var with set contents
99-
value = {}
100-
for item_value, present in pairs(set) do
101-
table.insert(value, item_value)
102-
end
103-
end
104-
cursor:set(config, name, key, value)
75+
-- ignore properties starting with .
76+
if utils.starts_with_dot(key) then return end
77+
-- avoid duplicate list settings
78+
if type(value) == 'table' and merge_list then
79+
-- create set with unique values
80+
local set = {}
81+
-- read existing value
82+
local current = cursor:get(config, name, key)
83+
if type(current) == 'table' then set = utils.add_values_to_set(set, current) end
84+
set = utils.add_values_to_set(set, value)
85+
-- reset value var with set contents
86+
value = {}
87+
for item_value, present in pairs(set) do table.insert(value, item_value) end
88+
end
89+
cursor:set(config, name, key, value)
10590
end
10691

10792
-- returns true if uci section is empty
10893
function utils.is_uci_empty(table)
109-
for key, value in pairs(table) do
110-
if not utils.starts_with_dot(key) then return false end
111-
end
112-
return true
94+
for key, value in pairs(table) do
95+
if not utils.starts_with_dot(key) then return false end
96+
end
97+
return true
11398
end
11499

115100
-- removes uci options
116101
-- and removes section if empty
117102
-- this is the inverse operation of `write_uci_section`
118103
function utils.remove_uci_options(cursor, config, section)
119-
local name = section['.name']
120-
-- loop over keys in section and
121-
-- remove each one from cursor
122-
for key, value in pairs(section) do
123-
if not utils.starts_with_dot(key) then
124-
cursor:delete(config, name, key)
125-
end
126-
end
127-
-- remove entire section if empty
128-
local uci = cursor:get_all(config, name)
129-
if uci and utils.is_uci_empty(uci) then
130-
cursor:delete(config, name)
131-
end
104+
local name = section['.name']
105+
-- loop over keys in section and
106+
-- remove each one from cursor
107+
for key, value in pairs(section) do
108+
if not utils.starts_with_dot(key) then cursor:delete(config, name, key) end
109+
end
110+
-- remove entire section if empty
111+
local uci = cursor:get_all(config, name)
112+
if uci and utils.is_uci_empty(uci) then cursor:delete(config, name) end
132113
end
133114

134115
-- returns true if a table is empty
135116
function utils.is_table_empty(t)
136-
if next(t) == nil then
137-
return true
138-
else
139-
return false
140-
end
117+
if next(t) == nil then
118+
return true
119+
else
120+
return false
121+
end
141122
end
142123

143124
-- Code by David Kastrup
144125
-- http://lua-users.org/wiki/DirTreeIterator
145126
function utils.dirtree(dir_param)
146-
assert(dir_param and dir_param ~= '', 'directory parameter is missing or empty')
147-
if string.sub(dir_param, -1) == '/' then
148-
dir_param = string.sub(dir_param, 1, -2)
149-
end
150-
local function yieldtree(dir)
151-
for entry in lfs.dir(dir) do
152-
if entry ~= '.' and entry ~= '..' then
153-
entry = dir .. '/' ..entry
154-
local attr = lfs.attributes(entry)
155-
coroutine.yield(entry,attr)
156-
if attr.mode == 'directory' then
157-
yieldtree(entry)
158-
end
159-
end
160-
end
127+
assert(dir_param and dir_param ~= '', 'directory parameter is missing or empty')
128+
if string.sub(dir_param, -1) == '/' then dir_param = string.sub(dir_param, 1, -2) end
129+
local function yieldtree(dir)
130+
for entry in lfs.dir(dir) do
131+
if entry ~= '.' and entry ~= '..' then
132+
entry = dir .. '/' .. entry
133+
local attr = lfs.attributes(entry)
134+
coroutine.yield(entry, attr)
135+
if attr.mode == 'directory' then yieldtree(entry) end
136+
end
161137
end
162-
return coroutine.wrap(function() yieldtree(dir_param) end)
138+
end
139+
return coroutine.wrap(function() yieldtree(dir_param) end)
163140
end
164141

165142
function utils.file_exists(path)
166-
local f = io.open(path, 'r')
167-
if f ~= nil then io.close(f) return true end
168-
return false
143+
local f = io.open(path, 'r')
144+
if f ~= nil then
145+
io.close(f)
146+
return true
147+
end
148+
return false
169149
end
170150

171151
function utils.file_to_set(path)
172-
local f = io.open(path, 'r')
173-
local set = {}
174-
for line in f:lines() do
175-
set[line] = true
176-
end
177-
return set
152+
local f = io.open(path, 'r')
153+
local set = {}
154+
for line in f:lines() do set[line] = true end
155+
return set
178156
end
179157

180158
function utils.set_to_file(set, path)
181-
local f = io.open(path, 'w')
182-
for file, bool in pairs(set) do
183-
f:write(file, '\n')
184-
end
185-
io.close(f)
186-
return true
159+
local f = io.open(path, 'w')
160+
for file, bool in pairs(set) do f:write(file, '\n') end
161+
io.close(f)
162+
return true
187163
end
188164

189-
function utils.starts_with(str, start)
190-
return str:sub(1, #start) == start
191-
end
165+
function utils.starts_with(str, start) return str:sub(1, #start) == start end
192166

193167
-- iterates over a table in alphabeticaly order
194168
function utils.sorted_pairs(t)
195-
-- collect keys
196-
local keys = {}
197-
for key in pairs(t) do keys[#keys+1] = key end
198-
199-
table.sort(keys)
200-
201-
-- return the iterator function
202-
local i = 0
203-
return function()
204-
i = i + 1
205-
if keys[i] then
206-
return keys[i], t[keys[i]]
207-
end
208-
end
169+
-- collect keys
170+
local keys = {}
171+
for key in pairs(t) do keys[#keys + 1] = key end
172+
173+
table.sort(keys)
174+
175+
-- return the iterator function
176+
local i = 0
177+
return function()
178+
i = i + 1
179+
if keys[i] then return keys[i], t[keys[i]] end
180+
end
209181
end
210182

211183
return utils

0 commit comments

Comments
 (0)