Advertisement
theinsekt

installerAPI

Mar 6th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.76 KB | None | 0 0
  1. --pastebin get 2WLg1AWL theinsektAPIs/installerAPI
  2.  
  3. --theinsektAPIs/installerAPI contains functions for using installfiles
  4. --experimental
  5. --only tested a little
  6. --may stop working when testing new stuff
  7.  
  8. --programs
  9. --installer and updateinstaller:
  10. --do: pastebin run fjSV9kHj
  11.  
  12.  
  13. --some result codes
  14. --returns 0 if the line is empty or should be ignored
  15. --returns -2 if unrecognised command
  16. --returns 1 if already installed
  17. --returns 2 if installed
  18. --returns -1 if two different files have the same path
  19. --positive number are good, negative are bad
  20.  
  21. --How an installfile works
  22. --Each line are run in order. Empty lines are skipped. Invalid commands should cause an error.
  23. --valid commands
  24. --examle lines:
  25. --
  26. --pastebin <code> <path>
  27. --installfile <code>
  28. --comment fgfhdfhdhf jnjn
  29.  
  30.  
  31.  
  32.  
  33. function loadLines(path)
  34.  if type(path)~="string" then error("Error (loadLines): path should be a string",2) end
  35.  if  not fs.exists(path) then
  36.   return nil
  37.  end
  38.  local h=fs.open(path, "r")
  39.  if h==nil then
  40.   return nil
  41.  end
  42.  local res={}
  43.  while(true) do
  44.   local line=h.readLine()
  45.   if line==nil then break end
  46.   res[#res+1]=line
  47.  end
  48.  h.close()
  49.  return res
  50. end
  51.  
  52.  
  53.  
  54. function getWords(line)
  55.  if type(line)~="string" then error("Error (getWords): line should be a string",2) end
  56.  local words={}
  57.  --"%S+"
  58.  --"%a+"
  59.  for word in string.gmatch(line, "%S+") do
  60.   words[#words+1]=word
  61.  end
  62.  return words
  63. end
  64.  
  65.  
  66.  
  67. --do the commands and add to the installed
  68. --table if something was installed
  69. --installed[path]=code
  70. --should have the same code if it's the same file
  71. --that way the same install file can be referenced twice
  72. commands={
  73.  
  74.  
  75. installfile=function(shellObj,installed,words)
  76.   --get parameters
  77.   local command=words[1]
  78.   local code=words[2]
  79.   --download and run installfile
  80.   return standardInstall(shellObj,installed,code)
  81.  end,
  82.  
  83.  
  84.  pastebin=function(shellObj,installed,words)
  85.   --get parameters
  86.   local command=words[1]
  87.   local code=words[2]
  88.   local path=words[3]
  89.   --download the file and mark it as installed
  90.   return standardDownload(shellObj,installed,path,code,false)
  91.  end,
  92.  
  93.  
  94.  ["comment"]=function(shellObj,installed,words)
  95.   return 0
  96.  end,
  97.  
  98.  
  99. }
  100.  
  101.  
  102. --returns 0 if the line is empty or should be ignored
  103. --returns -2 if unrecognised command
  104. --returns 1 if already installed
  105. --returns 2 if installed
  106. --returns -1 if two different files have the same path
  107. --positive number are good, negative are bad
  108. resCodeMeanings={
  109. [-3]="file wasn't downloaded",
  110. [-2]="unrecognised command",
  111. [-1]="two different files have the same path",
  112. [0]="line was empty or ignored",
  113. [1]="skipped because already installed",
  114. [2]="performed installation",
  115. }
  116.  
  117. function getRescodeMeaning(resCode)
  118.  local res=resCodeMeanings[resCode]
  119.  if res==nil then
  120.   return "unrecognized resCode"
  121.  end
  122.  return res
  123. end
  124.  
  125.  
  126.  
  127. --returns 0 if the line is empty or should be ignored
  128. --returns -2 if unrecognised command
  129. --returns 1 if already installed
  130. --returns 2 if installed
  131. --returns -1 if two different files have the same path
  132. --positive number are good, negative are bad
  133. function doInstallLine(shellObj,installed,line)
  134.   --get the words on the line
  135.   local words=getWords(line)
  136.   --
  137.   return doInstallWords(shellObj,installed,words)
  138. end
  139.  
  140.  
  141.  
  142. --install using words from command line
  143. function doInstallWords(shellObj,installed,words)
  144.   --DEBUG
  145.   --for k,v in ipairs(words) do
  146.   -- print(k,": ",v)
  147.   --end
  148.   --if line has no commands return 0
  149.   if words==nil or #words==0 then
  150.    return 0
  151.   end
  152.   --get the command on the line
  153.   local command=words[1]
  154.   --get the command function
  155.   local f=commands[command]
  156.   --if that command function does not exist return -2
  157.   if f==nil then
  158.    return -2
  159.   end
  160.   --run the command function
  161.   return f(shellObj, installed, words)
  162. end
  163.  
  164.  
  165. --should have checked path and code before calling this function
  166. --like in downloadHelp2
  167. function doInstallFile(shellObj, installed, path)
  168.  --check arguments
  169.  if installed==nil then installed={} end
  170.  if type(path)~="string" then error("Error: path should be a string",2) end
  171.  if type(installed)~="table" then error("Error: installed should be a table or nil",2) end
  172.  --load lines from install file
  173.  local lines=loadLines(path)
  174.  --for each line
  175.  for lineNr,line in ipairs(lines) do
  176.   local resCode = doInstallLine(shellObj, installed, line)
  177.   --throw errors here depending on the if the resCode is an error
  178.   if resCode < 0 then
  179.    local resCodeString="resCode "..tostring(resCode).." ("..installerAPI.getRescodeMeaning(resCode)..")"
  180.    error("Error: "..resCodeString.." in file "..path.." on line "..tostring(lineNr))
  181.   end
  182.  end
  183.  return 2
  184. end
  185.  
  186.  
  187. --returns 0 if the line is empty or should be ignored
  188. --returns -2 if unrecognised command
  189. --returns 1 if already installed
  190. --returns 2 if installed
  191. --returns -1 if two different files have the same path
  192. --returns -3 if the file wasn't downloaded
  193. --positive number are good, negative are bad
  194. --d should be a single file download function, takes params path, code and dextra
  195. function downloadHelp(shellObj,installed, isInstallfile, d, path, code, dextra)
  196.   --check parameters
  197.   if type(isInstallfile)~="boolean" and type(isInstallfile)~="nil" then
  198.     error("Error: the parameter called isInstallfile, should be true, false or nil.",2)
  199.   end
  200.   if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",2) end
  201.   if type(code)~="string" then error("Error: the parameter called code, should be a string",2) end
  202.   if type(path)~="string" then error("Error: the parameter called path, should be a string",2) end
  203.   if type(d)~="function" then error("Error: the parameter called d, should be a function",2) end
  204.   --check if already installed
  205.   if installed[path]~=nil then
  206.    if installed[path]==code then
  207.     --return have already installed code
  208.     return 1
  209.    else
  210.     --have already installed a different file
  211.     --with the same path
  212.     return -1
  213.    end
  214.   end
  215.   --remove if already exists NEW 2015-06-06
  216.   if fs.exists(path) then fs.delete(path) end
  217.   --download file
  218.   local resCode = d(shellObj, path, code, dextra)
  219.   --check rescode
  220.   if resCode < 0 then return resCode end
  221.   --check that file was downloaded NEW 2015-06-06
  222.   if not fs.exists(path) then return -3 end
  223.   --mark as installed
  224.   markAsInstalled(installed, path,code)
  225.   --do stuff depending on if downloaded installfile
  226.   if isInstallfile then
  227.    return doInstallFile(shellObj, installed, path)
  228.   else
  229.    return resCode
  230.   end
  231.  
  232. end
  233.  
  234. function markAsInstalled(installed, path,code)
  235.   --check parameters
  236.   if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",2) end
  237.   if type(code)~="string" then error("Error: the parameter called code, should be a string",2) end
  238.   if type(path)~="string" then error("Error: the parameter called path, should be a string",2) end
  239.   --do the marking
  240.   installed[path]=code
  241.   --return result code
  242.   return 2
  243. end
  244.  
  245.  
  246. --downloads the given file and then returns 2
  247. function pastebin(shellObj, path,code,overwrite)
  248.   if overwrite then
  249.    fs.delete(path)
  250.   end
  251.   shellObj.run("pastebin","get", code, path)
  252.   return 2
  253. end
  254.  
  255.  
  256. --returns 0 if the line is empty or should be ignored
  257. --returns -2 if unrecognised command
  258. --returns 1 if already installed
  259. --returns 2 if installed
  260. --returns -1 if two different files have the same path
  261. --positive number are good, negative are bad
  262. --downloads the file with the given code from pastebin to the given path
  263. function standardDownload(shellObj,installed,path,code,isInstallfile)
  264.  return downloadHelp(shellObj,installed,isInstallfile,pastebin,path,code,true)
  265. end
  266.  
  267. function standardInstall(shellObj,installed,code)
  268.   local path="installfiles/"..code
  269.   return standardDownload(shellObj,installed,path,code,true)
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement