Advertisement
magik6000

Untitled

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