-
Notifications
You must be signed in to change notification settings - Fork 0
/
minepackapi.lua
1017 lines (938 loc) · 30.9 KB
/
minepackapi.lua
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
The MIT License (MIT)
Copyright (c) 2012 Lyqyd, (c) 2017 Wilma456
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You can find the original here:
https://github.com/lyqyd/cc-packman/blob/master/package
--]]
list = {}
installed = {}
installRoot = "/"
config = {}
local unpack = unpack or table.unpack
local function postStatus(type, text)
os.queueEvent("package_status", type, text)
while true do
local event = {os.pullEvent("package_status")}
if event[1] == "package_status" then break end
end
end
local function printInformation(text)
postStatus("info", text)
end
local function printWarning(text)
postStatus("warning", text)
end
local function printError(text)
postStatus("error", text)
end
local function findFileEntry(fileList, path)
local entryFound = false
for i = 1, #fileList do
if fileList[i].path == path then
entryFound = i
break
end
end
return entryFound
end
local function updateFileInfo(fileList, path, version)
local entry = findFileEntry(fileList, path)
if entry then
fileList[entry].path = path
fileList[entry].version = version
else
table.insert(fileList, {path = path, version = version})
end
end
local transactionQueue = {}
local currentPackage
local Transaction = {
writeFile = function(self)
local path = fs.combine(minepackapi.installRoot, self.path)
if fs.exists(path) then
local handle = io.open(path, "r")
if handle then
self.backup = handle:read("*a")
handle:close()
end
end
local handle = io.open(path, "w")
if handle then
printInformation("Writing file "..path)
handle:write(self.contents)
handle:close()
else
return false
end
return true
end,
deleteFile = function(self)
local path = fs.combine(minepackapi.installRoot, self.path)
if fs.exists(path) then
local handle = io.open(path, "r")
if handle then
self.backup = handle:read("*a")
handle:close()
end
end
printInformation("Deleting file "..path)
fs.delete(path)
return not fs.exists(path)
end,
makeDirectory = function(self)
local path = fs.combine(minepackapi.installRoot, self.path)
if not fs.exists(path) then
printInformation("Creating directory "..path)
fs.makeDir(path)
end
return fs.isDir(path)
end,
removeDirectory = function(self)
local path = fs.combine(minepackapi.installRoot, self.path)
if fs.exists(path) and fs.isDir(path) and #(fs.list(path)) == 0 then
printInformation("Removing directory "..path)
fs.delete(path)
end
return not fs.exists(path)
end,
updateInfo = function(self)
newLine = self.path..(self.version and ";"..self.version or "")
local lineFound = false
for i = 1, #self.contents do
if self.path == string.match(self.contents[i], "^([^;]+)") then
--found the right entry, modify correctly now.
lineFound = true
self.contents[i] = newLine
updateFileInfo(minepackapi.installed[self.pack.fullName].files, self.path, self.version)
break
end
end
if not lineFound and newLine then
--didn't find a matching line in the loop, add a new line at the end.
table.insert(self.contents, newLine)
end
end,
removeInfo = function(self)
for i = 1, #self.contents do
if self.path == string.match(self.contents[i], "^([^;]+)") then
table.remove(self.contents, i)
local entry = findFileEntry(minepackapi.installed[self.pack.fullName].files, self.path)
if entry then
table.remove(minepackapi.installed[self.pack.fullName].files, entry)
end
break
end
end
end,
}
function Transaction.finish(self)
if Transaction[self.type] then
return Transaction[self.type](self)
else
return false
end
end
function Transaction.rollback(self)
if Transaction[self.type] then
if self.type == "writeFile" then
if self.backup ~= nil then
self.contents = self.backup
return Transaction.writeFile(self)
else
return Transaction.deleteFile(self)
end
elseif self.type == "deleteFile" then
if self.backup ~= nil then
self.contents = self.backup
return Transaction.writeFile(self)
end
elseif self.type == "makeDirectory" then
return Transaction.removeDirectory(self)
elseif self.type == "removeDirectory" then
return Transaction.makeDirectory(self)
end
else
return false
end
end
local tmeta = {__index = Transaction}
local function newTransaction(pack, path, type, contents, version)
local transaction = {
pack = pack,
path = path,
type = type,
contents = contents,
version = version,
backup = nil,
}
setmetatable(transaction, tmeta)
return transaction
end
local function loadConfig(sPath,sDefault)
if not fs.exists(sPath) then
local writefi = fs.open(sPath,"w")
writefi.write(sDefault)
writefi.close()
end
local fileh = io.open(sPath,"r")
for linecon in fileh:lines() do
if linecon:find("#") ~= 1 then
local sHead, sBody = linecon:match("([^=]+)=([^=]+)")
config[sHead] = sBody
end
end
fileh:close()
end
loadConfig("/etc/minepack/config.conf",[[
#Warning: If you edit the Path, you might have Problems
#with removing a Package. Plese move all files to the
#new location, if you edit the Path.
#
#Set the default target
defaultTarget=/usr/bin
#Set the Path for Help Files
helpPath=/usr/help/
#Write a Log
writeLog=true
#Select the Directory, in which minepack should
#place his files
minepackDirectory=/var/minepack
]])
local TransQueue = {
addFile = function(self, path, contents, version)
table.insert(self.transactions, newTransaction(self.pack, path, "writeFile", contents, version))
end,
removeFile = function(self, path)
table.insert(self.transactions, newTransaction(self.pack, path, "deleteFile"))
end,
makeDir = function(self, path)
if string.match(path, "(.-)/[^/]+$") and not fs.exists(fs.combine(minepackapi.installRoot, string.match(path, "(.-)/[^/]+$"))) then
self:makeDir(string.match(path, "(.-)/[^/]+$"))
end
if not fs.exists(fs.combine(minepackapi.installRoot, path)) then
table.insert(self.transactions, newTransaction(self.pack, path, "makeDirectory"))
end
end,
removeDir = function(self, path)
table.insert(self.transactions, newTransaction(self.pack, path, "removeDirectory"))
end,
env = function(self)
return {
addFile = function(path, contents, version)
self:addFile(path, contents, version)
end,
removeFile = function(path)
self:removeFile(path)
end,
makeDir = function(path)
self:makeDir(path)
end,
removeDir = function(path)
self:removeDir(path)
end,
}
end,
finish = function(self)
local installedFile = fs.combine(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"), self.pack.fullName..".txt")
local installedData = {}
if installed[self.pack.fullName] and fs.exists(installedFile) then
local handle = io.open(installedFile, "r")
if handle then
for line in handle:lines() do
table.insert(installedData, line)
end
handle:close()
--strip version number if present
if #installedData >= 1 then
table.remove(installedData, 1)
end
end
end
--ensure installed table entry exists
if not minepackapi.installed[self.pack.fullName] then
minepackapi.installed[self.pack.fullName] = {
version = self.pack.version,
files = {},
}
if not minepackapi.installed[self.pack.name] then minepackapi.installed[self.pack.name] = {[self.pack.repo] = minepackapi.installed[self.pack.fullName]} end
end
local fileInfoUpdates = {}
local lastTransaction = false
for i = 1, #self.transactions do
if not self.transactions[i]:finish() then
--clean up already-processed transactions and exit
printWarning("Transaction failed! Rolling back...")
for j = i, 1, -1 do
self.transactions[i]:rollback()
return false
end
end
--construct new line (or nil to remove entry, if present)
local newLine
if self.transactions[i].type == "writeFile" then
table.insert(fileInfoUpdates, newTransaction(self.pack, self.transactions[i].path, "updateInfo", installedData, self.transactions[i].version))
elseif self.transactions[i].type == "makeDirectory" then
table.insert(fileInfoUpdates, newTransaction(self.pack, self.transactions[i].path, "updateInfo", installedData))
elseif self.transactions[i].type == "deleteFile" or self.transactions[i].type == "removeDirectory" then
table.insert(fileInfoUpdates, newTransaction(self.pack, self.transactions[i].path, "removeInfo", installedData))
end
end
--modify installed data to match the transactions succesfully executed.
printInformation("Updating Database")
for i = 1, #fileInfoUpdates do
fileInfoUpdates[i]:finish()
end
if self.removing and #installedData == 0 then
fs.delete(installedFile)
--remove entries from installed packages table if removing minepackapi.
minepackapi.installed[self.pack.name][self.pack.repo] = nil
local othersWithName = false
for k, v in pairs(minepackapi.installed[self.pack.name]) do
if v then
othersWithName = true
break
end
end
if not othersWithName then
minepackapi.installed[self.pack.name] = nil
end
minepackapi.installed[self.pack.fullName] = nil
else
--write out file again, if any content exists for it.
table.insert(installedData, 1, tostring(self.pack.version))
if not fs.exists(fs.combine(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"), self.pack.repo..".txt")) then fs.makeDir(fs.combine(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"), self.pack.repo..".txt")) end
local handle = io.open(installedFile, "w")
if handle then
for k, v in ipairs(installedData) do
handle:write(v.."\n")
end
handle:close()
end
end
return true
end,
}
local queueMeta = {__index = TransQueue}
function newTransactionQueue(packName, removing)
local pack
if minepackapi.list[packName] and minepackapi.list[packName].version then pack = minepackapi.list[packName] else return nil, "No such package!" end
local queue = {
pack = pack,
removing = removing,
transactions = {},
}
setmetatable(queue, queueMeta)
return queue
end
local downloadTypes = {
github = {
author = true,
repository = true,
branch = true,
},
bitbucket = {
author = true,
repository = true,
branch = true,
},
pastebin = {
url = true,
filename = true,
},
raw = {
url = true,
filename = true,
},
multi = {},
grin = {
author = true,
repository = true,
},
meta = {},
}
local updateTypes = {
github = "incremental",
bitbucket = "incremental",
grin = "replace",
raw = "overwrite",
multi = "overwrite",
pastebin = "overwrite",
meta = "overwrite",
}
local lookupFunctions = {}
lookupFunctions.github = function(info)
local function getDirectoryContents(path)
local fType, fPath, fVer = {}, {}, {}
local response = http.get("https://api.github.com/repos/"..info.author.."/"..info.repository.."/contents/"..path.."?ref="..info.branch)
if response then
response = response.readAll()
if response ~= nil then
for str in response:gmatch('"type":%s*"(%w+)",') do table.insert(fType, str) end
for str in response:gmatch('"path":%s*"([^\"]+)",') do table.insert(fPath, str) end
for str in response:gmatch('"sha":%s*"([^\"]+)",') do table.insert(fVer, str) end
end
else
printWarning("Can't fetch repository information")
return nil
end
local directoryContents = {}
for i=1, #fType do
directoryContents[i] = {type = fType[i], path = fPath[i], version = fVer[i]}
end
return directoryContents
end
local function addDirectoryContents(path, contentsTable)
local contents = getDirectoryContents(path)
if not contents then return nil, "no contents" end
for n, file in ipairs(contents) do
if file.type == "dir" then
addDirectoryContents(file.path, contentsTable)
else
table.insert(contentsTable, {path = file.path, version = file.version})
end
end
return contentsTable
end
return addDirectoryContents("", {})
end
lookupFunctions.bitbucket = function(info)
local function getDirectoryContents(path)
local directoryContents = {}
local response = http.get("https://api.bitbucket.org/1.0/repositories/"..info.author.."/"..info.repository.."/src/"..info.branch..path)
if response then
response = response.readAll()
if response ~= nil then
for str in string.gmatch(string.match(response, '"directories": %[(.-)%]'), '"([^,\"]+)"') do table.insert(directoryContents, {type = "dir", path = str}) end
for str, ver in string.gmatch(string.match(response, '"files": %[(.-)%]'), '"path": "([^\"]+)".-"revision": "([^\"]+)"') do table.insert(directoryContents, {type = "file", path = str, version = ver}) end
end
else
printWarning("Can't fetch repository information")
return nil
end
return directoryContents
end
local function addDirectoryContents(path, contentsTable)
local contents = getDirectoryContents(path)
for n, file in ipairs(contents) do
if file.type == "dir" then
addDirectoryContents(path..file.path.."/", contentsTable)
else
table.insert(contentsTable, {path = file.path, version = file.version})
end
end
return contentsTable
end
return addDirectoryContents("/", {})
end
-- Local function to download a url raw
local function raw(url)
printInformation("Fetching: "..url)
http.request(url)
while true do
local event = {os.pullEvent()}
if event[1] == "http_success" then
printInformation("Done!")
return event[3].readAll()
elseif event[1] == "http_failure" then
printWarning("Unable to fetch file "..event[2])
return false
end
end
end
local downloadFunctions = {}
downloadFunctions.raw = function(pack, env, queue, info)
-- Delegate to local raw
local path = fs.combine(pack.target, info.filename)
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
local content = raw(info.url)
if content then
queue:addFile(path, content)
return true
else
return false
end
end
downloadFunctions.multi = function(pack, env, queue, info)
local files = info.files
for i = 1, #files do
local path = fs.combine(pack.target, files[i].name)
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
local content = raw(files[i].url)
if content then
queue:addFile(path, content)
else
return false
end
end
return true
end
downloadFunctions.github = function(pack, env, queue, info)
local contents = lookupFunctions.github(info)
if not contents then return nil, "content fetch failure" end
local localTarget = pack.target or ""
for num, file in ipairs(contents) do
local path = fs.combine(localTarget, file.path)
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
local content = raw("https://raw.github.com/"..info.author.."/"..info.repository.."/"..info.branch.."/"..file.path)
if content then
queue:addFile(path, content, file.version)
else
return false
end
end
return true
end
downloadFunctions.bitbucket = function(pack, env, queue, info)
local contents = lookupFunctions.bitbucket(info)
local localTarget = pack.target or ""
for num, file in ipairs(contents) do
local path = fs.combine(localTarget, file.path)
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
local content = raw("https://bitbucket.org/"..info.author.."/"..info.repository.."/raw/"..info.branch.."/"..file.path)
if content then
queue:addFile(path, content, file.version)
else
return false
end
end
return true
end
downloadFunctions.pastebin = function(pack, env, queue, info)
local path = fs.combine(pack.target, info.filename)
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
local content = raw("http://pastebin.com/raw.php?i="..info.url)
if content then
queue:addFile(path, content)
return true
else
return false
end
end
downloadFunctions.grin = function(pack, env, queue, info)
local fullName = pack.repo.."/"..pack.name
local status
parallel.waitForAny(function()
status = env.shell.run("pastebin run VuBNx3va -e -u", info.author, "-r", info.repository, fs.combine(fs.combine(minepackapi.installRoot, pack.target), pack.name))
end, function()
while true do
local e, msg = os.pullEvent("grin_install_status")
printInformation(msg)
end
end)
return status
end
downloadFunctions.meta = function(pack, env, queue)
return true
end
local function findInstalledVersionByPath(packName, path)
for i, file in ipairs(minepackapi.installed[packName].files) do
if file.path == path then return file.version end
end
end
local new_fs = {}
local Package = {
install = function(self, env)
if fs.getFreeSpace(self.target) < tonumber(self.size) then
error("Not enough free space",0)
end
local queue
if downloadFunctions[self.download.type.type] then
queue = newTransactionQueue(self.fullName)
if not downloadFunctions[self.download.type.type](self, env, queue, self.download.type) then return false end
else
return false
end
if not queue:finish() then return false end
--execute startup script, if present.
if self.setup then
local queue = newTransactionQueue(self.fullName)
--packman key included solely for backwards compatibility, usage is deprecated in favor of pack.
local setupArgs = {}
for match in string.gmatch(self.setup, "(%S+)") do
table.insert(setupArgs, match)
end
setupArgs[1] = fs.combine(fs.combine(minepackapi.installRoot, self.target), setupArgs[1])
local envQueue = queue:env()
if not os.run({shell = env.shell, packman = envQueue, pack = envQueue, fs = new_fs}, unpack(setupArgs)) then
--setup script threw an error.
printWarning("Package "..self.fullName.." failed to install, removing")
return self:remove(env)
end
--this must be done a second time to finalize any changes made by the install script.
return queue:finish()
end
if self.man then
local helph = http.get(self.man)
if helph then
local writeh = fs.open(minepackapi.config.helpPath..self.name,"w")
writeh.write(helph.readAll())
writeh.close()
helph.close()
end
end
if minepackapi.config.writeLog == "true" then
local logh = fs.open(minepackapi.config.minepackDirectory.."/log.txt","a")
logh.write("Installed "..self.fullName.."\n")
logh.close()
end
return true
end,
remove = function(self, env)
if not minepackapi.installed[self.fullName] then return false end
local queue = newTransactionQueue(self.fullName, true)
if self.cleanup then
local queue = newTransactionQueue(self.fullName, true)
local cleanupArgs = {}
for match in string.gmatch(self.cleanup, "(%S+)") do
table.insert(cleanupArgs, match)
end
cleanupArgs[1] = fs.combine(fs.combine(minepackapi.installRoot, self.target), cleanupArgs[1])
local envQueue = queue:env()
os.run({shell = env.shell, packman = envQueue, pack = envQueue, fs = new_fs}, unpack(cleanupArgs))
if not queue:finish() then return false end
end
local fileList = minepackapi.installed[self.fullName].files
for i = #fileList, 1, -1 do
if fs.exists(fs.combine(minepackapi.installRoot, fileList[i].path)) and fs.isDir(fs.combine(minepackapi.installRoot, fileList[i].path)) then
queue:removeDir(fileList[i].path)
else
queue:removeFile(fileList[i].path)
end
end
if fs.exists(minepackapi.config.helpPath..self.name) then
fs.delete(minepackapi.config.helpPath..self.name)
end
if minepackapi.config.writeLog == "true" then
local logh = fs.open(minepackapi.config.minepackDirectory.."/log.txt","a")
logh.write("Removed "..self.fullName.."\n")
logh.close()
end
return queue:finish()
end,
upgrade = function(self, env)
if not minepackapi.installed[self.fullName] then return false end
local queue = newTransactionQueue(self.fullName)
if minepackapi.config.writeLog == "true" then
local logh = fs.open(minepackapi.config.minepackDirectory.."/log.txt","a")
logh.write("Updated "..self.fullName.."\n")
logh.close()
end
if updateTypes[self.download.type.type] == "incremental" then
local updatedFiles = {}
local contents = lookupFunctions[self.download.type.type](self.download.type)
for num, file in ipairs(contents) do
local path = fs.combine(self.target, file.path)
if file.version ~= findInstalledVersionByPath(self.fullName, path) then
if string.match(path, "(.-)/[^/]+$") then
queue:makeDir(string.match(path, "(.-)/[^/]+$"))
end
if self.download.type.type == "github" then
local content = raw("https://raw.github.com/"..self.download.type.author.."/"..self.download.type.repository.."/"..self.download.type.branch.."/"..file.path)
if content then
queue:addFile(path, content, file.version)
else
return false
end
elseif self.download.type.type == "bitbucket" then
local content = raw("https://bitbucket.org/"..self.download.type.author.."/"..self.download.type.repository.."/raw/"..self.download.type.branch.."/"..file.path)
if content then
queue:addFile(path, content, file.version)
else
return false
end
end
end
updatedFiles[path] = true
end
for i, fileInfo in ipairs(minepackapi.installed[self.fullName].files) do
if not updatedFiles[fileInfo.path] and fileInfo.version ~= minepackapi.installed[self.fullName].version then
if not fs.isDir(fs.combine(minepackapi.installRoot, fileInfo.path)) or (fs.isDir(fs.combine(minepackapi.installRoot, fileInfo.path)) and #(fs.list(fs.combine(minepackapi.installRoot, fileInfo.path))) == 0) then
queue:removeFile(fileInfo.path)
end
end
end
return queue:finish()
elseif updateTypes[self.download.type.type] == "overwrite" then
if not downloadFunctions[self.download.type.type](self, env, queue, self.download.type) then return false end
return queue:finish()
elseif updateTypes[self.download.type.type] == "replace" then
return self:remove(env) and self:install(env)
end
end,
}
local pmetatable = {__index = Package}
function new(name, repo)
local p = {
name = name,
repo = repo,
fullName = repo.."/"..name,
version = "",
size = 0,
category = {},
dependencies = {},
--installation folder target
target = minepackapi.config.defaultTarget,
setup = nil,
remove = nil,
man = nil,
description = "None",
download = {}
}
setmetatable(p, pmetatable)
return p
end
function findDependencies(packageName, _dependencyTable)
local dependencyTable = _dependencyTable or {}
if minepackapi.list[packageName] then
dependencyTable[packageName] = true
for packName in pairs(minepackapi.list[packageName].dependencies) do
packName = packName:lower()
if packName ~= "none" and not dependencyTable[packName] then
dependencyTable, errmsg = minepackapi.findDependencies(packName, dependencyTable)
if not dependencyTable then return nil, errmsg end
end
end
else
return nil, packageName
end
return dependencyTable
end
if not fs.exists("/usr/bin") then fs.makeDir("/usr/bin") end
--process package list
local function addPacks(file)
--fs,getName(file).sub(1,-5) doesn't work
local tmpstr = fs.getName(file)
local packName = tmpstr:sub(1,-5)
local state = ""
local typeState = nil
local listHandle = io.open(file, "r")
local entryTable
local lineCount = 1
if listHandle then
for line in listHandle:lines() do
if state == "type" or state == "help" then
local allAttributes = true
for attribute in pairs(downloadTypes[entryTable.download[state].type]) do
if not entryTable.download[state][attribute] then
allAttributes = false
break
end
end
if allAttributes then
state = "main"
end
end
local property,hasValue,value = string.match(line, "^%s*([^=%s]+)%s*(=?)%s*(.-)%s*$")
if typeState and property ~= "file" then typeState = nil end
hasValue=hasValue~="" or nil
if property == "name" and state == "" then
if state == "" then
entryTable = new(string.lower(value), packName)
entryTable.target = minepackapi.config.defaultTarget
state = "main"
else
if state ~= "dirty" then
printWarning("Unexpected 'name' at line "..lineCount.." in "..file)
state = "dirty"
end
end
elseif property == "type" or property == "help" then
if state == "main" then
entryTable.download[property] = {type = string.match(value, "^(%S*)$")}
if downloadFunctions[entryTable.download[property].type] then
if entryTable.download[property].type == "multi" then
entryTable.download[property].files = {}
typeState = property
state = "main"
else
state = property
end
else
if state ~= "dirty" then
printWarning("Unknown Repository Format at line "..lineCount.." in "..file)
state = "dirty"
end
end
else
if state ~= "dirty" then
printWarning("Unexpected 'type' at line "..lineCount.." in "..file)
state = "dirty"
end
end
elseif property == "file" then
if typeState then
local fileTable = entryTable.download[typeState].files
local name, url = string.match(value, "(%S+)%s+(.*)")
fileTable[#fileTable + 1] = {name = name, url = url}
else
printWarning("Unexpected "..property.." at line "..lineCount.." in "..file)
state = "dirty"
end
elseif property == "target" or property == "setup" or property == "update" or property == "cleanup" or property == "version" or property == "size" or property == "man" or property == "description" then
if state == "main" then
entryTable[property] = value
else
if state ~= "dirty" then
printWarning("Unexpected "..property.." at line "..lineCount.." in "..file)
state = "dirty"
end
end
elseif property == "dependencies" or property == "category" then
if state == "main" then
for str in string.gmatch(value, "(%S+)") do
entryTable[property][str] = true
end
else
if state ~= "dirty" then
printWarning("Unexpected "..property.." at line "..lineCount.." in "..file)
state = "dirty"
end
end
elseif property == "end" then
if state == "dirty" then
state = ""
elseif state == "type" then
printWarning("Unexpected end at line "..lineCount.." in "..file)
state = ""
elseif state == "main" then
--this line is the required entries for a valid repolist entry.
if entryTable.download.type and (#entryTable.version > 0 and (tonumber(entryTable.size) > 0 or entryTable.download.type.type == "meta")) then
local i
for name in pairs(entryTable.dependencies) do
i = true
break
end
if i then
list[packName.."/"..entryTable.name] = entryTable
if list[entryTable.name] then
list[entryTable.name][packName] = entryTable
else
list[entryTable.name] = {[packName] = entryTable}
end
end
else
entryTable = nil
end
state = ""
end
elseif state == "type" or state == "help" then
local propertyFound = false
for prop in pairs(downloadTypes[entryTable.download[state].type]) do
if property == prop then
propertyFound = true
break
end
end
if propertyFound then
entryTable.download[state][property] = value
else
printWarning("Unexpected "..property.." at line "..lineCount.." in "..file)
state = "dirty"
end
end
lineCount = lineCount + 1
end
if state ~= "" then
printWarning("Expected 'end' at line "..lineCount.." in "..file)
end
listHandle:close()
else
printError("Could not open repository list!")
end
end
function load()
for k, v in pairs(minepackapi.list) do
minepackapi.list[k] = nil
end
if fs.exists(minepackapi.config.minepackDirectory.."/repositories") then
for _, file in ipairs(fs.list(minepackapi.config.minepackDirectory.."/repositories")) do
addPacks(minepackapi.config.minepackDirectory.."/repositories/"..file)
end
end
for k, v in pairs(minepackapi.installed) do
minepackapi.installed[k] = nil
end
if fs.exists(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed")) and fs.isDir(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed")) then
for _, repo in ipairs(fs.list(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"))) do
for _, file in ipairs(fs.list(fs.combine(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"), repo))) do
local name = repo.."/"..file:sub(1,-5)
local handle = io.open(fs.combine(fs.combine(fs.combine(minepackapi.installRoot, minepackapi.config.minepackDirectory.."/installed"), repo), file), "r")
if handle then
installed[name] = {files = {}}
local packVersion
for line in handle:lines() do
if not packVersion then
packVersion = line
installed[name].version = packVersion
else
local path, version = string.match(line, "([^;]+);(.*)")
if path and version then
installed[name].files[#installed[name].files + 1] = {path = path, version = version}
else
installed[name].files[#installed[name].files + 1] = {path = line, version = packVersion}
end
end
end
handle:close()
if installed[file:sub(1,-5)] then
installed[file:sub(1,-5)][repo] = installed[name]
else
installed[file:sub(1,-5)] = {[repo] = installed[name]}
end
else
printWarning("Couldn't open package db file: "..file)
end
end
end
end
for k, v in pairs(new_fs) do
new_fs[k] = nil
end
do
local root = minepackapi.installRoot
--override fs api to use installRoot, recreated when loading to accomodate installRoot changes.
local function fsWrap(name,f,n)
return function(...)
local args = { ... }