comparison share/lua/5.2/luarocks/add.lua @ 1132:d137f631bad5

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