-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathoppm.lua
More file actions
411 lines (390 loc) · 10.2 KB
/
oppm.lua
File metadata and controls
411 lines (390 loc) · 10.2 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
--[[
OpenPrograms package manager, browser and downloader, for easy access to many programs
This is the library providing all the important functions.
Author: Vexatos
]]
local event = require("event")
local fs = require("filesystem")
local process = require("process")
local serial = require("serialization")
local shell = require("shell")
local wget = loadfile("/bin/wget.lua")
if not component.isAvailable("internet") then
error("No internet card found")
return
end
local internet = require("internet")
local oppm = {}
local function getContent(url)
local sContent = ""
local result, response = pcall(internet.request, url)
if not result then
return nil
end
for chunk in response do
sContent = sContent..chunk
end
return sContent
end
local function getRepos()
local success, sRepos = pcall(getContent,"https://raw.githubusercontent.com/OpenPrograms/openprograms.github.io/master/repos.cfg")
if not success then
io.stderr:write("Could not connect to the Internet. Please ensure you have an Internet connection.")
return -1
end
return serial.unserialize(sRepos)
end
local function getPackages(repo)
local success, sPackages = pcall(getContent,"https://raw.githubusercontent.com/"..repo.."/master/programs.cfg")
if not success or not sPackages then
success, sPackages = pcall(getContent,"https://raw.githubusercontent.com/"..repo.."/main/programs.cfg")
if not success or not sPackages then
return -1
end
end
return serial.unserialize(sPackages)
end
--For sorting table values by alphabet
local function compare(a,b)
for i=1,math.min(#a,#b) do
if a:sub(i,i)~=b:sub(i,i) then
return a:sub(i,i) < b:sub(i,i)
end
end
return #a < #b
end
local function downloadFile(url,path,force)
if force then
return wget("-fq",url,path)
else
return wget("-q",url,path)
end
end
local function readFromFile(fNum)
local path
if fNum == 1 then
path = "/etc/opdata.svd"
elseif fNum == 2 then
path = "/etc/oppm.cfg"
if not fs.exists(path) then
local tProcess = process.running()
path = fs.concat(fs.path(shell.resolve(tProcess)),"/etc/oppm.cfg")
end
end
if not fs.exists(fs.path(path)) then
fs.makeDirectory(fs.path(path))
end
if not fs.exists(path) then
return {-1}
end
local file,msg = io.open(path,"rb")
if not file then
error("error while trying to read file at "..path..": "..msg)
end
local sPacks = file:read("*a")
file:close()
return serial.unserialize(sPacks) or {-1}
end
local function saveToFile(packs)
local file,msg = io.open("/etc/opdata.svd","wb")
if not file then
error("error while trying to save package names: "..msg)
end
local sPacks = serial.serialize(packs)
file:write(sPacks)
file:close()
end
function oppm.listPackages(filter,installed)
filter = filter or false
if filter then
filter = string.lower(filter)
end
local packages = {}
if not installed then
local success, repos = pcall(getRepos)
if not success or repos==-1 then
error("unable to connect to the Internet")
elseif repos==nil then
error("error while trying to receive repository list")
end
for _,j in pairs(repos) do
if j.repo then
local lPacks = getPackages(j.repo)
if lPacks==nil then
error("error while trying to receive package list for " .. j.repo)
elseif type(lPacks) == "table" then
for k in pairs(lPacks) do
if not k.hidden then
table.insert(packages,k)
end
end
end
end
end
local lRepos = readFromFile(2)
if lRepos and lRepos.repos then
for _,j in pairs(lRepos.repos) do
for k in pairs(j) do
if not k.hidden then
table.insert(packages,k)
end
end
end
end
else
local lPacks = {}
local packs = readFromFile(1)
for i in pairs(packs) do
table.insert(lPacks,i)
end
packages = lPacks
end
if filter then
local lPacks = {}
for i,j in ipairs(packages) do
if (#j>=#filter) and string.find(j,filter,1,true)~=nil then
table.insert(lPacks,j)
end
end
packages = lPacks
end
table.sort(packages,compare)
return packages
end
local function getFullInformation(pack)
local success, repos = pcall(getRepos)
if not success or repos==-1 then
error("unable to connect to the Internet")
end
for _,j in pairs(repos) do
if j.repo then
local lPacks = getPackages(j.repo)
if lPacks==nil then
error("error while trying to receive package list for "..j.repo)
elseif type(lPacks) == "table" then
for k in pairs(lPacks) do
if k==pack then
return lPacks[k],j.repo
end
end
end
end
end
local lRepos = readFromFile(2)
if lRepos then
for i,j in pairs(lRepos.repos) do
for k in pairs(j) do
if k==pack then
return j[k],i
end
end
end
end
return nil
end
function oppm.getInformation(pack)
if not pack then
return false, "no input given"
end
pack = string.lower(pack)
local info = getFullInformation(pack)
if not info then
return false, "package does not exist"
end
local done = false
local tInfo = {}
if info.name then
tInfo.name = info.name
done = true
end
if info.description then
tInfo.description = info.description
done = true
end
if info.authors then
tInfo.authors = info.authors
done = true
end
if info.note then
tInfo.note = info.note
done = true
end
if not done then
return false, "no information provided"
end
return tInfo
end
local tPacks = readFromFile(1)
local function installPack(pack,path,update,force)
update = update or false
if not pack then
return false, "no input given"
end
if not path and not update then
local lConfig = readFromFile(2)
path = lConfig.path or "/usr"
elseif not update then
path = shell.resolve(path)
end
pack = string.lower(pack)
if not tPacks then
error("error while trying to read local package names")
elseif tPacks[1]==-1 then
table.remove(tPacks,1)
end
local info,repo = getFullInformation(pack)
if not info then
return false, "package does not exist"
end
if update then
path = nil
for i,j in pairs(info.files) do
if tPacks[pack] then
for k,v in pairs(tPacks[pack]) do
if k==i then
path = string.gsub(fs.path(v),j.."/?$","/")
break
end
end
if path then
break
end
else
error("error while checking update path")
end
end
path = shell.resolve(string.gsub(path,"^/?","/"),nil)
end
if not update and fs.exists(path) then
if not fs.isDirectory(path) then
if force then
path = fs.concat(fs.path(path),pack)
fs.makeDirectory(path)
else
error("path points to a file, needs to be a directory")
return
end
end
elseif not update then
if force then
fs.makeDirectory(path)
else
error("directory does not exist")
return
end
end
if tPacks[pack] and (not update) then
return false, "package has already been installed"
elseif not tPacks[pack] and update then
return false, "package has not been installed"
end
if update then
for i,j in pairs(tPacks[pack]) do
fs.remove(j)
end
end
tPacks[pack] = {}
for i,j in pairs(info.files) do
local nPath
if string.find(j,"^//") then
local lPath = string.sub(j,2)
if not fs.exists(lPath) then
fs.makeDirectory(lPath)
end
nPath = fs.concat(lPath,string.gsub(i,".+(/.-)$","%1"),nil)
else
local lPath = fs.concat(path,j)
if not fs.exists(lPath) then
fs.makeDirectory(lPath)
end
nPath = fs.concat(path,j,string.gsub(i,".+(/.-)$","%1"),nil)
end
local success,response = pcall(downloadFile,"https://raw.githubusercontent.com/"..repo.."/"..i,nPath)
if success and response then
tPacks[pack][i] = nPath
else
fs.remove(nPath)
for o,p in pairs(tPacks[pack]) do
fs.remove(p)
tPacks[pack][o]=nil
end
error("Error while installing files for package '"..i.."'. Installation reverted")
end
end
if info.dependencies then
for i,j in pairs(info.dependencies) do
local nPath
if string.find(j,"^//") then
nPath = string.sub(j,2)
else
nPath = fs.concat(path,j,string.gsub(i,".+(/.-)$","%1"),nil)
end
if string.lower(string.sub(i,1,4))=="http" then
local success,response = pcall(downloadFile,i,nPath)
if success and response then
tPacks[pack][i] = nPath
else
fs.remove(nPath)
for o,p in pairs(tPacks[pack]) do
fs.remove(p)
tPacks[pack][o]=nil
end
error("Error while installing dependency package '"..i.."'. Installation reverted")
end
else
local depInfo = getFullInformation(string.lower(i))
if depInfo then
installPack(string.lower(i),fs.concat(path,j),update)
end
end
end
end
saveToFile(tPacks)
return true
end
function oppm.installPackage(pack,path)
installPack(pack,path,false)
end
function oppm.uninstallPackage(pack)
local info,repo = getFullInformation(pack)
if not info then
return false, "package does not exist"
end
local tFiles = readFromFile(1)
if not tFiles then
error("error while trying to read package names")
elseif tFiles[1]==-1 then
table.remove(tFiles,1)
end
if not tFiles[pack] then
return false, "package has not been installed"
end
for i,j in pairs(tFiles[pack]) do
fs.remove(j)
end
tFiles[pack]=nil
saveToFile(tFiles)
return true
end
function oppm.updatePackage(pack)
if pack=="all" then
local tFiles = readFromFile(1)
if not tFiles then
error("error while trying to read package names")
return
elseif tFiles[1]==-1 then
table.remove(tFiles,1)
end
local done = false
for i in pairs(tFiles) do
installPack(i,nil,true)
done = true
end
if not done then
return false, "No package has been installed so far"
end
else
installPack(pack,nil,true)
end
end
return oppm