Advertisement
magik6000

Untitled

Nov 22nd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 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. print("SPT:",act)
  79. rt[#rt+1]=act
  80. act=""
  81. else
  82. act = act .. text:sub(x,x)
  83. end
  84. end
  85. if act ~= "" then
  86. rt[#rt+1] = act
  87. end
  88. return rt;
  89. end
  90.  
  91. ---Intarnal functions
  92.  
  93. local function update_list()
  94. local sync = CGetS("/etc/ccpt/config","master")
  95. if sync then
  96. download("/etc/ccpt/list",sync)
  97. else
  98. error("Update failed: master server not set!")
  99. end
  100. end
  101.  
  102. local function base_find(name)
  103. local base = fs.open("/etc/ccpt/list","r")
  104.  
  105. if not base then
  106. error("Could not open base file: /etc/ccpt/list")
  107. end
  108.  
  109. local x = true;
  110. while x do
  111. local line = base.readLine()
  112. if line == nil then
  113. x = false;
  114. else
  115. local entry = split(line,":")
  116. if entry[1] == "p" then
  117. if entry[2] == name then
  118. local ret = {name=entry[2],url=entry[4],version=tonumber(entry(3))}
  119. return ret
  120. end
  121. end
  122. end
  123. end
  124. end
  125.  
  126. ---MAIN CODE
  127.  
  128. local argv = {...}
  129. if argv[1] == "init" then
  130. print("Installing directories")
  131. fs.makeDir("/etc/")
  132. fs.makeDir("/etc/ccpt")
  133. fs.makeDir("/bin/")
  134. fs.makeDir("/usr/")
  135. fs.makeDir("/usr/bin")
  136. fs.makeDir("/usr/lib")
  137. fs.makeDir("/usr/share")
  138. print("Downloading default config")
  139. download("/etc/ccpt/config","http://cc.nativehttp.org/fresh/config")
  140. print("Updating package list")
  141. update_list()
  142. elseif argv[1] == "update" then
  143. print("Updating package list")
  144. update_list()
  145. elseif argv[1] == "install" then
  146. if argv[2] == nil then
  147. print("Usage: ccpk install [name]")
  148. else
  149. print("Reading Database")
  150. local entry = base_find(argv[2])
  151. if entry then
  152. print("Downloading package header")
  153. local header = download(nil, entry.url..entry.name.."/index")
  154. else
  155. print("Package not found!")
  156. end
  157. end
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement