Advertisement
magik6000

Untitled

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