PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/luarocks/validate.lua

http://github.com/keplerproject/luarocks
Lua | 159 lines | 142 code | 16 blank | 1 comment | 23 complexity | 70683b7a71afcef87860ea3e5a3b8e4f MD5 | raw file
Possible License(s): Unlicense
  1. --- Sandboxed test of build/install of all packages in a repository (unfinished and disabled).
  2. local validate = {}
  3. package.loaded["luarocks.validate"] = validate
  4. local fs = require("luarocks.fs")
  5. local dir = require("luarocks.dir")
  6. local path = require("luarocks.path")
  7. local cfg = require("luarocks.cfg")
  8. local build = require("luarocks.build")
  9. local install = require("luarocks.install")
  10. local util = require("luarocks.util")
  11. util.add_run_function(validate)
  12. validate.help_summary = "Sandboxed test of build/install of all packages in a repository."
  13. validate.help = [[
  14. <argument>, if given, is a local repository pathname.
  15. ]]
  16. local function save_settings(repo)
  17. local protocol, path = dir.split_url(repo)
  18. table.insert(cfg.rocks_servers, 1, protocol.."://"..path)
  19. return {
  20. root_dir = cfg.root_dir,
  21. rocks_dir = cfg.rocks_dir,
  22. deploy_bin_dir = cfg.deploy_bin_dir,
  23. deploy_lua_dir = cfg.deploy_lua_dir,
  24. deploy_lib_dir = cfg.deploy_lib_dir,
  25. }
  26. end
  27. local function restore_settings(settings)
  28. cfg.root_dir = settings.root_dir
  29. cfg.rocks_dir = settings.rocks_dir
  30. cfg.deploy_bin_dir = settings.deploy_bin_dir
  31. cfg.deploy_lua_dir = settings.deploy_lua_dir
  32. cfg.deploy_lib_dir = settings.deploy_lib_dir
  33. cfg.variables.ROCKS_TREE = settings.rocks_dir
  34. cfg.variables.SCRIPTS_DIR = settings.deploy_bin_dir
  35. table.remove(cfg.rocks_servers, 1)
  36. end
  37. local function prepare_sandbox(file)
  38. local root_dir = fs.make_temp_dir(file):gsub("/+$", "")
  39. cfg.root_dir = root_dir
  40. cfg.rocks_dir = path.rocks_dir(root_dir)
  41. cfg.deploy_bin_dir = path.deploy_bin_dir(root_dir)
  42. cfg.variables.ROCKS_TREE = cfg.rocks_dir
  43. cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir
  44. return root_dir
  45. end
  46. local function validate_rockspec(file)
  47. local ok, err, errcode = build.build_rockspec(file, true, "one")
  48. if not ok then
  49. util.printerr(err)
  50. end
  51. return ok, err, errcode
  52. end
  53. local function validate_src_rock(file)
  54. local ok, err, errcode = build.build_rock(file, false, "one")
  55. if not ok then
  56. util.printerr(err)
  57. end
  58. return ok, err, errcode
  59. end
  60. local function validate_rock(file)
  61. local ok, err, errcode = install.install_binary_rock(file, "one")
  62. if not ok then
  63. util.printerr(err)
  64. end
  65. return ok, err, errcode
  66. end
  67. function validate.command(flags, repo)
  68. repo = repo or cfg.rocks_dir
  69. util.printout("Verifying contents of "..repo)
  70. local results = {
  71. ok = {}
  72. }
  73. local settings = save_settings(repo)
  74. local sandbox
  75. if flags["quick"] then
  76. sandbox = prepare_sandbox("luarocks_validate")
  77. end
  78. if not fs.exists(repo) then
  79. return nil, repo.." is not a local repository."
  80. end
  81. for file in fs.dir(repo) do for _=1,1 do
  82. if file == "manifest" or file == "index.html" then
  83. break -- continue for
  84. end
  85. local pathname = fs.absolute_name(dir.path(repo, file))
  86. if not flags["quick"] then
  87. sandbox = prepare_sandbox(file)
  88. end
  89. local ok, err, errcode
  90. util.printout()
  91. util.printout("Verifying "..pathname)
  92. if file:match("%.rockspec$") then
  93. ok, err, errcode = validate_rockspec(pathname, "one")
  94. elseif file:match("%.src%.rock$") then
  95. ok, err, errcode = validate_src_rock(pathname)
  96. elseif file:match("%.rock$") then
  97. ok, err, errcode = validate_rock(pathname)
  98. end
  99. if ok then
  100. table.insert(results.ok, {file=file} )
  101. else
  102. if not errcode then
  103. errcode = "misc"
  104. end
  105. if not results[errcode] then
  106. results[errcode] = {}
  107. end
  108. table.insert(results[errcode], {file=file, err=err} )
  109. end
  110. util.run_scheduled_functions()
  111. if not flags["quick"] then
  112. fs.delete(sandbox)
  113. end
  114. repeat until not fs.pop_dir()
  115. end end
  116. if flags["quick"] then
  117. fs.delete(sandbox)
  118. end
  119. restore_settings(settings)
  120. util.title("Results:")
  121. util.printout("OK: "..tostring(#results.ok))
  122. for _, entry in ipairs(results.ok) do
  123. util.printout(entry.file)
  124. end
  125. for errcode, errors in pairs(results) do
  126. if errcode ~= "ok" then
  127. util.printout()
  128. util.printout(errcode.." errors: "..tostring(#errors))
  129. for _, entry in ipairs(errors) do
  130. util.printout(entry.file, entry.err)
  131. end
  132. end
  133. end
  134. util.title("Summary:")
  135. local total = 0
  136. for errcode, errors in pairs(results) do
  137. util.printout(errcode..": "..tostring(#errors))
  138. total = total + #errors
  139. end
  140. util.printout("Total: "..total)
  141. return true
  142. end
  143. return validate