Advertisement
magik6000

Untitled

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