Advertisement
joserobjr

Configurable OPPM Repo Patch

May 12th, 2016
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local shell = require("shell")
  2.  
  3. local orig = [[return serial.unserialize(sRepos)]]
  4. local repl = [[local result = serial.unserialize(sRepos)
  5.   local config = io.open("/etc/oppm-repo.cfg","r")
  6.   if config then
  7.     config:close()
  8.     for line in io.lines("/etc/oppm-repo.cfg") do
  9.       result[line] = {repo=line}
  10.     end
  11.   end
  12.   return result]]
  13.  
  14. function escape(str)
  15.     return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
  16. end
  17.  
  18. local path, revert = ...
  19. if path == "revert" then
  20.   revert = true
  21.   path = nil
  22. end
  23.  
  24. if not path then
  25.   path = shell.resolve("oppm.lua", shell.getPath())
  26.   if not path then
  27.     print("oppm.lua not found")
  28.     os.exit(1)
  29.     return
  30.   end
  31. end
  32.  
  33. local file = io.open(path, "rb")
  34. if not file then
  35.   print("File not found: " .. path)
  36.   os.exit(1)
  37.   return
  38. end
  39.  
  40. print("Patching the file " .. path)
  41.  
  42. local content = file:read("*a")
  43. file:close()
  44.  
  45. local pattern = nil
  46. if revert then
  47.   pattern = escape(repl)
  48.   repl = orig
  49. else
  50.   pattern = escape(orig)
  51. end
  52.  
  53. local patched = content:gsub(pattern, repl)
  54.  
  55. if patched == content then
  56.   print("The file was not changed!")
  57.   return
  58. end
  59.  
  60. file = io.open(path, "w")
  61. file:write(patched)
  62. file:close()
  63.  
  64. print("The file was patched successfully")
  65.  
  66. file = io.open("/etc/oppm-repo.cfg", "a")
  67. file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement