Advertisement
magik6000

Untitled

Nov 22nd, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. --CCPT - ComputerCraft Packaging Tool
  2.  
  3. if not http then
  4. error("HTTP API MUST be enabled to use this program")
  5. end
  6.  
  7. --Pseudo enums
  8.  
  9. local validator = {ok = 0, installed = 1, file_conflict = 2}
  10.  
  11. ---UTILITIES
  12.  
  13. local function CGetS(file,name)
  14. local _cfg = fs.open(file,"r")
  15.  
  16. if not _cfg then
  17. error("Could not open configuration file: "..file)
  18. end
  19.  
  20. local x = true;
  21.  
  22. while x do
  23. local line = _cfg.readLine()
  24. if line == nil then
  25. x = false;
  26. else
  27.  
  28. local side = false
  29. local prop = ""
  30. local val = ""
  31. for a=1,#line do
  32. if line:sub(a,a) == '=' then
  33. side = true
  34. elseif side then
  35. val = val .. line:sub(a,a)
  36. else
  37. prop = prop .. line:sub(a,a)
  38. end
  39. end
  40.  
  41. if prop == name then
  42. _cfg.close()
  43. return val
  44. end
  45. end
  46. end
  47. _cfg.close()
  48. end
  49.  
  50. local function CGetN(file,name)
  51. return tonumber(CGetS(file,name))
  52. end
  53.  
  54. local function download(file, url)
  55. local res = http.get(url)
  56. if res then
  57. if file ~= nil then
  58. local fhnd = fs.open(file, "w");
  59. if fhnd then
  60. fhnd.write(res.readAll())
  61. fhnd.close()
  62. return res.readAll()
  63. else
  64. res.close()
  65. error("Could not open "..file.." for writing")
  66. end
  67. else
  68. return res.readAll()
  69. end
  70. else
  71. error("Download failed for: "..url)
  72. end
  73. res.close()
  74. end
  75.  
  76. function split(text,splitter)
  77. local rt = {}
  78. local act = ""
  79. for x=1,#text do
  80. if text:sub(x,x+#splitter-1) == splitter then
  81. rt[#rt+1]=act
  82. act=""
  83. else
  84. act = act .. text:sub(x,x)
  85. end
  86. end
  87. if act ~= "" then
  88. rt[#rt+1] = act
  89. end
  90. return rt;
  91. end
  92.  
  93. ---Intarnal functions
  94.  
  95. local function update_list()
  96. local sync = CGetS("/etc/ccpt/config","master")
  97. if sync then
  98. download("/etc/ccpt/list",sync)
  99. else
  100. error("Update failed: master server not set!")
  101. end
  102. end
  103.  
  104. local function base_find(name)
  105. local base = fs.open("/etc/ccpt/list","r")
  106.  
  107. if not base then
  108. error("Could not open base file: /etc/ccpt/list")
  109. end
  110.  
  111. local x = true;
  112. while x do
  113. local line = base.readLine()
  114. if line == nil then
  115. x = false;
  116. else
  117. local entry = split(line,";")
  118. if entry[1] == "p" then
  119. if entry[2] == name then
  120. local ret = {name=entry[2],url=entry[4],version=tonumber(entry[3])}
  121. return ret
  122. end
  123. end
  124. end
  125. end
  126. end
  127.  
  128. local function validate(pname,header)
  129. local instbase = fs.open("/etc/ccpt/installed","r")
  130. if instbase then
  131. local x = true
  132. while x do
  133. local tline = instbase.readLine()
  134. if tline == nil then
  135. x = false
  136. else
  137. if pname == split(tline,";")[1] then
  138. return validator.installed
  139. end
  140. end
  141. end
  142. end
  143. --local filebase = fs.open("/etc/ccpt/files","r")
  144. lhead = split(header,"\n")
  145. local x = 1
  146. for x = 1, #lhead do
  147. print("CCK:",lhead[x])
  148. if split(lhead[x],";")[1] == "f" then
  149. if fs.exists(split(lhead[x],";")[2]) then
  150. return validator.file_conflict
  151. end
  152. end
  153. end
  154.  
  155. return validator.ok
  156. end
  157. ---MAIN CODE
  158.  
  159. local argv = {...}
  160. if argv[1] == "init" then
  161. print("Installing directories")
  162. fs.makeDir("/etc/")
  163. fs.makeDir("/etc/ccpt")
  164. fs.makeDir("/bin/")
  165. fs.makeDir("/usr/")
  166. fs.makeDir("/usr/bin")
  167. fs.makeDir("/usr/lib")
  168. fs.makeDir("/usr/share")
  169. print("Downloading default config")
  170. download("/etc/ccpt/config","http://cc.nativehttp.org/fresh/config")
  171. print("Updating package list")
  172. update_list()
  173. elseif argv[1] == "update" then
  174. print("Updating package list")
  175. update_list()
  176. elseif argv[1] == "install" then
  177. if argv[2] == nil then
  178. print("Usage: ccpk install [name]")
  179. else
  180. print("Reading Database")
  181. local entry = base_find(argv[2])
  182. if entry then
  183. print("Downloading package header")
  184. local header = download(nil, entry.url..entry.name.."/index")
  185. print("Checking for conflicts")
  186. validate(entry.name,header)
  187. else
  188. print("Package not found!")
  189. end
  190. end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement