/src/luarocks/add.lua

http://github.com/keplerproject/luarocks · Lua · 122 lines · 117 code · 3 blank · 2 comment · 2 complexity · 3ec9edc39e205aeeaf8852edec4952d5 MD5 · raw file

  1. --- Module implementing the luarocks-admin "add" command.
  2. -- Adds a rock or rockspec to a rocks server.
  3. local add = {}
  4. package.loaded["luarocks.add"] = add
  5. local cfg = require("luarocks.cfg")
  6. local util = require("luarocks.util")
  7. local dir = require("luarocks.dir")
  8. local manif = require("luarocks.manif")
  9. local index = require("luarocks.index")
  10. local fs = require("luarocks.fs")
  11. local cache = require("luarocks.cache")
  12. util.add_run_function(add)
  13. add.help_summary = "Add a rock or rockspec to a rocks server."
  14. add.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}"
  15. add.help = [[
  16. Arguments are local files, which may be rockspecs or rocks.
  17. The flag --server indicates which server to use.
  18. If not given, the default server set in the upload_server variable
  19. from the configuration file is used instead.
  20. The flag --no-refresh indicates the local cache should not be refreshed
  21. prior to generation of the updated manifest.
  22. ]]
  23. local function add_files_to_server(refresh, rockfiles, server, upload_server)
  24. assert(type(refresh) == "boolean" or not refresh)
  25. assert(type(rockfiles) == "table")
  26. assert(type(server) == "string")
  27. assert(type(upload_server) == "table" or not upload_server)
  28. local download_url, login_url = cache.get_server_urls(server, upload_server)
  29. local at = fs.current_dir()
  30. local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url
  31. local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password)
  32. if not local_cache then
  33. return nil, protocol
  34. end
  35. if protocol == "file" then
  36. return nil, "Server "..server.." is not recognized, check your configuration."
  37. end
  38. if not login_url then
  39. login_url = protocol.."://"..server_path
  40. end
  41. local ok, err = fs.change_dir(at)
  42. if not ok then return nil, err end
  43. local files = {}
  44. for _, rockfile in ipairs(rockfiles) do
  45. if fs.exists(rockfile) then
  46. util.printout("Copying file "..rockfile.." to "..local_cache.."...")
  47. local absolute = fs.absolute_name(rockfile)
  48. fs.copy(absolute, local_cache, cfg.perm_read)
  49. table.insert(files, dir.base_name(absolute))
  50. else
  51. util.printerr("File "..rockfile.." not found")
  52. end
  53. end
  54. if #files == 0 then
  55. return nil, "No files found"
  56. end
  57. local ok, err = fs.change_dir(local_cache)
  58. if not ok then return nil, err end
  59. util.printout("Updating manifest...")
  60. manif.make_manifest(local_cache, "one", true)
  61. manif.zip_manifests()
  62. util.printout("Updating index.html...")
  63. index.make_index(local_cache)
  64. local login_info = ""
  65. if user then login_info = " -u "..user end
  66. if password then login_info = login_info..":"..password end
  67. if not login_url:match("/$") then
  68. login_url = login_url .. "/"
  69. end
  70. table.insert(files, "index.html")
  71. table.insert(files, "manifest")
  72. for ver in util.lua_versions() do
  73. table.insert(files, "manifest-"..ver)
  74. table.insert(files, "manifest-"..ver..".zip")
  75. end
  76. -- TODO abstract away explicit 'curl' call
  77. local cmd
  78. if protocol == "rsync" then
  79. local srv, path = server_path:match("([^/]+)(/.+)")
  80. cmd = cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/"
  81. elseif upload_server and upload_server.sftp then
  82. local part1, part2 = upload_server.sftp:match("^([^/]*)/(.*)$")
  83. cmd = cfg.variables.SCP.." "..table.concat(files, " ").." "..user.."@"..part1..":/"..part2
  84. else
  85. cmd = cfg.variables.CURL.." "..login_info.." -T '{"..table.concat(files, ",").."}' "..login_url
  86. end
  87. util.printout(cmd)
  88. fs.execute(cmd)
  89. return true
  90. end
  91. function add.command(flags, ...)
  92. local files = {...}
  93. if #files < 1 then
  94. return nil, "Argument missing. "..util.see_help("add", "luarocks-admin")
  95. end
  96. local server, server_table = cache.get_upload_server(flags["server"])
  97. if not server then return nil, server_table end
  98. return add_files_to_server(not flags["no-refresh"], files, server, server_table)
  99. end
  100. return add