PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/src/luarocks/add.lua

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