aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-07-30 13:05:10 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:51:31 +0300
commita50299e1d6b280f5ca635b2c7ac9fe889befd6d6 (patch)
tree16a7fcc25397673ba41d6157e9c4863b22d4496d
parent7a42b3bc6709000c4809a3ca4ec9fbaed2c3f0a8 (diff)
downloadluarocks-a50299e1d6b280f5ca635b2c7ac9fe889befd6d6.tar.gz
luarocks-a50299e1d6b280f5ca635b2c7ac9fe889befd6d6.tar.bz2
luarocks-a50299e1d6b280f5ca635b2c7ac9fe889befd6d6.zip
test persist
-rw-r--r--src/luarocks/core/cfg.d.tl2
-rw-r--r--src/luarocks/fs.d.tl2
-rw-r--r--src/luarocks/persist-original.lua259
-rw-r--r--src/luarocks/persist.lua171
4 files changed, 352 insertions, 82 deletions
diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl
index 33b9be66..fccfe0e3 100644
--- a/src/luarocks/core/cfg.d.tl
+++ b/src/luarocks/core/cfg.d.tl
@@ -54,7 +54,7 @@ local record cfg
54 record cache 54 record cache
55 luajit_version_checked: boolean 55 luajit_version_checked: boolean
56 luajit_version: string 56 luajit_version: string
57 rocks_provided: {string: string} --? right type? infered from 57 rocks_provided: {string: string} --? right type? infered from util
58 end 58 end
59 59
60 record variables 60 record variables
diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl
index 80747f9b..88034b65 100644
--- a/src/luarocks/fs.d.tl
+++ b/src/luarocks/fs.d.tl
@@ -8,7 +8,7 @@ local record fs
8 end 8 end
9 -- util 9 -- util
10 is_dir: function(dir: string): boolean 10 is_dir: function(dir: string): boolean
11 dir: function(dir: string): function(): string --? right iterator 11 dir: function(dir: string): function(): string
12 make_dir: function(string): boolean, string 12 make_dir: function(string): boolean, string
13 is_file: function(file: string): boolean 13 is_file: function(file: string): boolean
14 current_dir: function(): string 14 current_dir: function(): string
diff --git a/src/luarocks/persist-original.lua b/src/luarocks/persist-original.lua
new file mode 100644
index 00000000..4dcd930a
--- /dev/null
+++ b/src/luarocks/persist-original.lua
@@ -0,0 +1,259 @@
1
2--- Utility module for loading files into tables and
3-- saving tables into files.
4local persist = {}
5
6local core = require("luarocks.core.persist")
7local util = require("luarocks.util")
8local dir = require("luarocks.dir")
9local fs = require("luarocks.fs")
10
11persist.run_file = core.run_file
12persist.load_into_table = core.load_into_table
13
14local write_table
15
16--- Write a value as Lua code.
17-- This function handles only numbers and strings, invoking write_table
18-- to write tables.
19-- @param out table or userdata: a writer object supporting :write() method.
20-- @param v: the value to be written.
21-- @param level number: the indentation level
22-- @param sub_order table: optional prioritization table
23-- @see write_table
24function persist.write_value(out, v, level, sub_order)
25 if type(v) == "table" then
26 level = level or 0
27 write_table(out, v, level + 1, sub_order)
28 elseif type(v) == "string" then
29 if v:match("[\r\n]") then
30 local open, close = "[[", "]]"
31 local equals = 0
32 local v_with_bracket = v.."]"
33 while v_with_bracket:find(close, 1, true) do
34 equals = equals + 1
35 local eqs = ("="):rep(equals)
36 open, close = "["..eqs.."[", "]"..eqs.."]"
37 end
38 out:write(open.."\n"..v..close)
39 else
40 out:write(("%q"):format(v))
41 end
42 else
43 out:write(tostring(v))
44 end
45end
46
47local is_valid_plain_key
48do
49 local keywords = {
50 ["and"] = true,
51 ["break"] = true,
52 ["do"] = true,
53 ["else"] = true,
54 ["elseif"] = true,
55 ["end"] = true,
56 ["false"] = true,
57 ["for"] = true,
58 ["function"] = true,
59 ["goto"] = true,
60 ["if"] = true,
61 ["in"] = true,
62 ["local"] = true,
63 ["nil"] = true,
64 ["not"] = true,
65 ["or"] = true,
66 ["repeat"] = true,
67 ["return"] = true,
68 ["then"] = true,
69 ["true"] = true,
70 ["until"] = true,
71 ["while"] = true,
72 }
73 function is_valid_plain_key(k)
74 return type(k) == "string"
75 and k:match("^[a-zA-Z_][a-zA-Z0-9_]*$")
76 and not keywords[k]
77 end
78end
79
80local function write_table_key_assignment(out, k, level)
81 if is_valid_plain_key(k) then
82 out:write(k)
83 else
84 out:write("[")
85 persist.write_value(out, k, level)
86 out:write("]")
87 end
88
89 out:write(" = ")
90end
91
92--- Write a table as Lua code in curly brackets notation to a writer object.
93-- Only numbers, strings and tables (containing numbers, strings
94-- or other recursively processed tables) are supported.
95-- @param out table or userdata: a writer object supporting :write() method.
96-- @param tbl table: the table to be written.
97-- @param level number: the indentation level
98-- @param field_order table: optional prioritization table
99write_table = function(out, tbl, level, field_order)
100 out:write("{")
101 local sep = "\n"
102 local indentation = " "
103 local indent = true
104 local i = 1
105 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
106 out:write(sep)
107 if indent then
108 for _ = 1, level do out:write(indentation) end
109 end
110
111 if k == i then
112 i = i + 1
113 else
114 write_table_key_assignment(out, k, level)
115 end
116
117 persist.write_value(out, v, level, sub_order)
118 if type(v) == "number" then
119 sep = ", "
120 indent = false
121 else
122 sep = ",\n"
123 indent = true
124 end
125 end
126 if sep ~= "\n" then
127 out:write("\n")
128 for _ = 1, level - 1 do out:write(indentation) end
129 end
130 out:write("}")
131end
132
133--- Write a table as series of assignments to a writer object.
134-- @param out table or userdata: a writer object supporting :write() method.
135-- @param tbl table: the table to be written.
136-- @param field_order table: optional prioritization table
137-- @return true if successful; nil and error message if failed.
138local function write_table_as_assignments(out, tbl, field_order)
139 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
140 if not is_valid_plain_key(k) then
141 return nil, "cannot store '"..tostring(k).."' as a plain key."
142 end
143 out:write(k.." = ")
144 persist.write_value(out, v, 0, sub_order)
145 out:write("\n")
146 end
147 return true
148end
149
150--- Write a table using Lua table syntax to a writer object.
151-- @param out table or userdata: a writer object supporting :write() method.
152-- @param tbl table: the table to be written.
153local function write_table_as_table(out, tbl)
154 out:write("return {\n")
155 for k, v, sub_order in util.sortedpairs(tbl) do
156 out:write(" ")
157 write_table_key_assignment(out, k, 1)
158 persist.write_value(out, v, 1, sub_order)
159 out:write(",\n")
160 end
161 out:write("}\n")
162end
163
164--- Save the contents of a table to a string.
165-- Each element of the table is saved as a global assignment.
166-- Only numbers, strings and tables (containing numbers, strings
167-- or other recursively processed tables) are supported.
168-- @param tbl table: the table containing the data to be written
169-- @param field_order table: an optional array indicating the order of top-level fields.
170-- @return persisted data as string; or nil and an error message
171function persist.save_from_table_to_string(tbl, field_order)
172 local out = {buffer = {}}
173 function out:write(data) table.insert(self.buffer, data) end
174 local ok, err = write_table_as_assignments(out, tbl, field_order)
175 if not ok then
176 return nil, err
177 end
178 return table.concat(out.buffer)
179end
180
181--- Save the contents of a table in a file.
182-- Each element of the table is saved as a global assignment.
183-- Only numbers, strings and tables (containing numbers, strings
184-- or other recursively processed tables) are supported.
185-- @param filename string: the output filename
186-- @param tbl table: the table containing the data to be written
187-- @param field_order table: an optional array indicating the order of top-level fields.
188-- @return boolean or (nil, string): true if successful, or nil and a
189-- message in case of errors.
190function persist.save_from_table(filename, tbl, field_order)
191 local prefix = dir.dir_name(filename)
192 fs.make_dir(prefix)
193 local out = io.open(filename, "w")
194 if not out then
195 return nil, "Cannot create file at "..filename
196 end
197 local ok, err = write_table_as_assignments(out, tbl, field_order)
198 out:close()
199 if not ok then
200 return nil, err
201 end
202 return true
203end
204
205--- Save the contents of a table as a module.
206-- The module contains a 'return' statement that returns the table.
207-- Only numbers, strings and tables (containing numbers, strings
208-- or other recursively processed tables) are supported.
209-- @param filename string: the output filename
210-- @param tbl table: the table containing the data to be written
211-- @return boolean or (nil, string): true if successful, or nil and a
212-- message in case of errors.
213function persist.save_as_module(filename, tbl)
214 local out = io.open(filename, "w")
215 if not out then
216 return nil, "Cannot create file at "..filename
217 end
218 write_table_as_table(out, tbl)
219 out:close()
220 return true
221end
222
223function persist.load_config_file_if_basic(filename, cfg)
224 local env = {
225 home = cfg.home
226 }
227 local result, err, errcode = persist.load_into_table(filename, env)
228 if errcode == "load" or errcode == "run" then
229 -- bad config file or depends on env, so error out
230 return nil, "Could not read existing config file " .. filename
231 end
232
233 local tbl
234 if errcode == "open" then
235 -- could not open, maybe file does not exist
236 tbl = {}
237 else
238 tbl = result
239 tbl.home = nil
240 end
241
242 return tbl
243end
244
245function persist.save_default_lua_version(prefix, lua_version)
246 local ok, err = fs.make_dir(prefix)
247 if not ok then
248 return nil, err
249 end
250 local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w")
251 if not fd then
252 return nil, err
253 end
254 fd:write('return "' .. lua_version .. '"\n')
255 fd:close()
256 return true
257end
258
259return persist
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua
index 4dcd930a..eb501f2e 100644
--- a/src/luarocks/persist.lua
+++ b/src/luarocks/persist.lua
@@ -1,26 +1,38 @@
1local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table
2
1 3
2--- Utility module for loading files into tables and
3-- saving tables into files.
4local persist = {} 4local persist = {}
5 5
6
7
8
6local core = require("luarocks.core.persist") 9local core = require("luarocks.core.persist")
7local util = require("luarocks.util") 10local util = require("luarocks.util")
8local dir = require("luarocks.dir") 11local dir = require("luarocks.dir")
9local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
13local cfg = require("luarocks.core.cfg")
14
15
16
10 17
11persist.run_file = core.run_file 18persist.run_file = core.run_file
12persist.load_into_table = core.load_into_table 19persist.load_into_table = core.load_into_table
13 20
21
22
23
24
25
14local write_table 26local write_table
15 27
16--- Write a value as Lua code. 28
17-- This function handles only numbers and strings, invoking write_table 29
18-- to write tables. 30
19-- @param out table or userdata: a writer object supporting :write() method. 31
20-- @param v: the value to be written. 32
21-- @param level number: the indentation level 33
22-- @param sub_order table: optional prioritization table 34
23-- @see write_table 35
24function persist.write_value(out, v, level, sub_order) 36function persist.write_value(out, v, level, sub_order)
25 if type(v) == "table" then 37 if type(v) == "table" then
26 level = level or 0 38 level = level or 0
@@ -29,13 +41,13 @@ function persist.write_value(out, v, level, sub_order)
29 if v:match("[\r\n]") then 41 if v:match("[\r\n]") then
30 local open, close = "[[", "]]" 42 local open, close = "[[", "]]"
31 local equals = 0 43 local equals = 0
32 local v_with_bracket = v.."]" 44 local v_with_bracket = v .. "]"
33 while v_with_bracket:find(close, 1, true) do 45 while v_with_bracket:find(close, 1, true) do
34 equals = equals + 1 46 equals = equals + 1
35 local eqs = ("="):rep(equals) 47 local eqs = ("="):rep(equals)
36 open, close = "["..eqs.."[", "]"..eqs.."]" 48 open, close = "[" .. eqs .. "[", "]" .. eqs .. "]"
37 end 49 end
38 out:write(open.."\n"..v..close) 50 out:write(open .. "\n" .. v .. close)
39 else 51 else
40 out:write(("%q"):format(v)) 52 out:write(("%q"):format(v))
41 end 53 end
@@ -71,14 +83,13 @@ do
71 ["while"] = true, 83 ["while"] = true,
72 } 84 }
73 function is_valid_plain_key(k) 85 function is_valid_plain_key(k)
74 return type(k) == "string" 86 return k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") and
75 and k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") 87 not keywords[k]
76 and not keywords[k]
77 end 88 end
78end 89end
79 90
80local function write_table_key_assignment(out, k, level) 91local function write_table_key_assignment(out, k, level)
81 if is_valid_plain_key(k) then 92 if type(k) == "string" and is_valid_plain_key(k) then
82 out:write(k) 93 out:write(k)
83 else 94 else
84 out:write("[") 95 out:write("[")
@@ -89,26 +100,26 @@ local function write_table_key_assignment(out, k, level)
89 out:write(" = ") 100 out:write(" = ")
90end 101end
91 102
92--- Write a table as Lua code in curly brackets notation to a writer object. 103
93-- Only numbers, strings and tables (containing numbers, strings 104
94-- or other recursively processed tables) are supported. 105
95-- @param out table or userdata: a writer object supporting :write() method. 106
96-- @param tbl table: the table to be written. 107
97-- @param level number: the indentation level 108
98-- @param field_order table: optional prioritization table 109
99write_table = function(out, tbl, level, field_order) 110write_table = function(out, tbl, level, sort_by)
100 out:write("{") 111 out:write("{")
101 local sep = "\n" 112 local sep = "\n"
102 local indentation = " " 113 local indentation = " "
103 local indent = true 114 local indent = true
104 local i = 1 115 local i = 1
105 for k, v, sub_order in util.sortedpairs(tbl, field_order) do 116 for k, v, sub_order in util.sortedpairs(tbl, sort_by) do
106 out:write(sep) 117 out:write(sep)
107 if indent then 118 if indent then
108 for _ = 1, level do out:write(indentation) end 119 for _ = 1, level do out:write(indentation) end
109 end 120 end
110 121
111 if k == i then 122 if type(k) == "number" then
112 i = i + 1 123 i = i + 1
113 else 124 else
114 write_table_key_assignment(out, k, level) 125 write_table_key_assignment(out, k, level)
@@ -130,26 +141,26 @@ write_table = function(out, tbl, level, field_order)
130 out:write("}") 141 out:write("}")
131end 142end
132 143
133--- Write a table as series of assignments to a writer object. 144
134-- @param out table or userdata: a writer object supporting :write() method. 145
135-- @param tbl table: the table to be written. 146
136-- @param field_order table: optional prioritization table 147
137-- @return true if successful; nil and error message if failed. 148
138local function write_table_as_assignments(out, tbl, field_order) 149local function write_table_as_assignments(out, tbl, sort_by)
139 for k, v, sub_order in util.sortedpairs(tbl, field_order) do 150 for k, v, sub_order in util.sortedpairs(tbl, sort_by) do
140 if not is_valid_plain_key(k) then 151 if not (type(k) == "string" and is_valid_plain_key(k)) then
141 return nil, "cannot store '"..tostring(k).."' as a plain key." 152 return nil, "cannot store '" .. tostring(k) .. "' as a plain key."
142 end 153 end
143 out:write(k.." = ") 154 out:write(k .. " = ")
144 persist.write_value(out, v, 0, sub_order) 155 persist.write_value(out, v, 0, sub_order)
145 out:write("\n") 156 out:write("\n")
146 end 157 end
147 return true 158 return true
148end 159end
149 160
150--- Write a table using Lua table syntax to a writer object. 161
151-- @param out table or userdata: a writer object supporting :write() method. 162
152-- @param tbl table: the table to be written. 163
153local function write_table_as_table(out, tbl) 164local function write_table_as_table(out, tbl)
154 out:write("return {\n") 165 out:write("return {\n")
155 for k, v, sub_order in util.sortedpairs(tbl) do 166 for k, v, sub_order in util.sortedpairs(tbl) do
@@ -161,40 +172,40 @@ local function write_table_as_table(out, tbl)
161 out:write("}\n") 172 out:write("}\n")
162end 173end
163 174
164--- Save the contents of a table to a string. 175
165-- Each element of the table is saved as a global assignment. 176
166-- Only numbers, strings and tables (containing numbers, strings 177
167-- or other recursively processed tables) are supported. 178
168-- @param tbl table: the table containing the data to be written 179
169-- @param field_order table: an optional array indicating the order of top-level fields. 180
170-- @return persisted data as string; or nil and an error message 181
171function persist.save_from_table_to_string(tbl, field_order) 182function persist.save_from_table_to_string(tbl, sort_by)
172 local out = {buffer = {}} 183 local out = { buffer = {} }
173 function out:write(data) table.insert(self.buffer, data) end 184 function out:write(data) table.insert(self.buffer, data) end
174 local ok, err = write_table_as_assignments(out, tbl, field_order) 185 local ok, err = write_table_as_assignments(out, tbl, sort_by)
175 if not ok then 186 if not ok then
176 return nil, err 187 return nil, err
177 end 188 end
178 return table.concat(out.buffer) 189 return table.concat(out.buffer)
179end 190end
180 191
181--- Save the contents of a table in a file. 192
182-- Each element of the table is saved as a global assignment. 193
183-- Only numbers, strings and tables (containing numbers, strings 194
184-- or other recursively processed tables) are supported. 195
185-- @param filename string: the output filename 196
186-- @param tbl table: the table containing the data to be written 197
187-- @param field_order table: an optional array indicating the order of top-level fields. 198
188-- @return boolean or (nil, string): true if successful, or nil and a 199
189-- message in case of errors. 200
190function persist.save_from_table(filename, tbl, field_order) 201function persist.save_from_table(filename, tbl, sort_by)
191 local prefix = dir.dir_name(filename) 202 local prefix = dir.dir_name(filename)
192 fs.make_dir(prefix) 203 fs.make_dir(prefix)
193 local out = io.open(filename, "w") 204 local out = io.open(filename, "w")
194 if not out then 205 if not out then
195 return nil, "Cannot create file at "..filename 206 return nil, "Cannot create file at " .. filename
196 end 207 end
197 local ok, err = write_table_as_assignments(out, tbl, field_order) 208 local ok, err = write_table_as_assignments(out, tbl, sort_by)
198 out:close() 209 out:close()
199 if not ok then 210 if not ok then
200 return nil, err 211 return nil, err
@@ -202,37 +213,37 @@ function persist.save_from_table(filename, tbl, field_order)
202 return true 213 return true
203end 214end
204 215
205--- Save the contents of a table as a module. 216
206-- The module contains a 'return' statement that returns the table. 217
207-- Only numbers, strings and tables (containing numbers, strings 218
208-- or other recursively processed tables) are supported. 219
209-- @param filename string: the output filename 220
210-- @param tbl table: the table containing the data to be written 221
211-- @return boolean or (nil, string): true if successful, or nil and a 222
212-- message in case of errors. 223
213function persist.save_as_module(filename, tbl) 224function persist.save_as_module(filename, tbl)
214 local out = io.open(filename, "w") 225 local out = io.open(filename, "w")
215 if not out then 226 if not out then
216 return nil, "Cannot create file at "..filename 227 return nil, "Cannot create file at " .. filename
217 end 228 end
218 write_table_as_table(out, tbl) 229 write_table_as_table(out, tbl)
219 out:close() 230 out:close()
220 return true 231 return true
221end 232end
222 233
223function persist.load_config_file_if_basic(filename, cfg) 234function persist.load_config_file_if_basic(filename, config)
224 local env = { 235 local env = {
225 home = cfg.home 236 home = config.home,
226 } 237 }
227 local result, err, errcode = persist.load_into_table(filename, env) 238 local result, _, errcode = persist.load_into_table(filename, env)
228 if errcode == "load" or errcode == "run" then 239 if errcode == "load" or errcode == "run" then
229 -- bad config file or depends on env, so error out 240
230 return nil, "Could not read existing config file " .. filename 241 return nil, "Could not read existing config file " .. filename
231 end 242 end
232 243
233 local tbl 244 local tbl
234 if errcode == "open" then 245 if errcode == "open" then
235 -- could not open, maybe file does not exist 246
236 tbl = {} 247 tbl = {}
237 else 248 else
238 tbl = result 249 tbl = result
@@ -243,13 +254,13 @@ function persist.load_config_file_if_basic(filename, cfg)
243end 254end
244 255
245function persist.save_default_lua_version(prefix, lua_version) 256function persist.save_default_lua_version(prefix, lua_version)
246 local ok, err = fs.make_dir(prefix) 257 local ok, err_makedir = fs.make_dir(prefix)
247 if not ok then 258 if not ok then
248 return nil, err 259 return nil, err_makedir
249 end 260 end
250 local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") 261 local fd, err_open = io.open(dir.path(prefix, "default-lua-version.lua"), "w")
251 if not fd then 262 if not fd then
252 return nil, err 263 return nil, err_open
253 end 264 end
254 fd:write('return "' .. lua_version .. '"\n') 265 fd:write('return "' .. lua_version .. '"\n')
255 fd:close() 266 fd:close()