diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2014-05-26 19:41:17 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2014-05-26 19:41:17 -0300 |
| commit | dc9d2daf9deaf867b6caaf88ab1bb088e2b4622d (patch) | |
| tree | fadb309229d6a0403aa38d19917e45c164486e7f /src | |
| parent | 1788d0bb8c08e1ff7e671f20a023b3dcc4f80206 (diff) | |
| download | luarocks-dc9d2daf9deaf867b6caaf88ab1bb088e2b4622d.tar.gz luarocks-dc9d2daf9deaf867b6caaf88ab1bb088e2b4622d.tar.bz2 luarocks-dc9d2daf9deaf867b6caaf88ab1bb088e2b4622d.zip | |
Transition to MoonRocks. Add `luarocks upload` command for uploading into MoonRocks.
Diffstat (limited to 'src')
| -rwxr-xr-x | src/bin/luarocks | 1 | ||||
| -rw-r--r-- | src/luarocks/cfg.lua | 15 | ||||
| -rw-r--r-- | src/luarocks/pack.lua | 4 | ||||
| -rw-r--r-- | src/luarocks/upload.lua | 88 | ||||
| -rw-r--r-- | src/luarocks/upload/multipart.lua | 107 |
5 files changed, 207 insertions, 8 deletions
diff --git a/src/bin/luarocks b/src/bin/luarocks index b85fbc7c..3c9e1b74 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks | |||
| @@ -23,6 +23,7 @@ commands = { | |||
| 23 | write_rockspec = "luarocks.write_rockspec", | 23 | write_rockspec = "luarocks.write_rockspec", |
| 24 | purge = "luarocks.purge", | 24 | purge = "luarocks.purge", |
| 25 | doc = "luarocks.doc", | 25 | doc = "luarocks.doc", |
| 26 | upload = "luarocks.upload", | ||
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | command_line.run_command(...) | 29 | command_line.run_command(...) |
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 59a17754..a96d610f 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
| @@ -32,7 +32,7 @@ end | |||
| 32 | 32 | ||
| 33 | cfg.site_config = site_config | 33 | cfg.site_config = site_config |
| 34 | 34 | ||
| 35 | cfg.program_version = "2.1.2" | 35 | cfg.program_version = "2.2.0beta1" |
| 36 | cfg.major_version = cfg.program_version:match("([^.]%.[^.])") | 36 | cfg.major_version = cfg.program_version:match("([^.]%.[^.])") |
| 37 | 37 | ||
| 38 | local persist = require("luarocks.persist") | 38 | local persist = require("luarocks.persist") |
| @@ -214,14 +214,17 @@ local defaults = { | |||
| 214 | 214 | ||
| 215 | rocks_servers = { | 215 | rocks_servers = { |
| 216 | { | 216 | { |
| 217 | "http://www.luarocks.org/repositories/rocks", | 217 | "http://rocks.moonscript.org", |
| 218 | "http://luarocks.giga.puc-rio.br/", | 218 | "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/", |
| 219 | "http://luafr.org/luarocks/rocks", | ||
| 220 | "http://liblua.so/luarocks/repositories/rocks", | ||
| 221 | "http://luarocks.logiceditor.com/rocks", | ||
| 222 | } | 219 | } |
| 223 | }, | 220 | }, |
| 224 | disabled_servers = {}, | 221 | disabled_servers = {}, |
| 222 | |||
| 223 | upload = { | ||
| 224 | server = "rocks.moonscript.org", | ||
| 225 | tool_version = "0.0.1", | ||
| 226 | api_version = "1", | ||
| 227 | }, | ||
| 225 | 228 | ||
| 226 | lua_extension = "lua", | 229 | lua_extension = "lua", |
| 227 | lua_interpreter = site_config.LUA_INTERPRETER or "lua", | 230 | lua_interpreter = site_config.LUA_INTERPRETER or "lua", |
diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index d8b3ad9b..c73d66ab 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua | |||
| @@ -33,7 +33,7 @@ argument. | |||
| 33 | -- @param rockspec_file string: An URL or pathname for a rockspec file. | 33 | -- @param rockspec_file string: An URL or pathname for a rockspec file. |
| 34 | -- @return string or (nil, string): The filename of the resulting | 34 | -- @return string or (nil, string): The filename of the resulting |
| 35 | -- .src.rock file; or nil and an error message. | 35 | -- .src.rock file; or nil and an error message. |
| 36 | local function pack_source_rock(rockspec_file) | 36 | function pack.pack_source_rock(rockspec_file) |
| 37 | assert(type(rockspec_file) == "string") | 37 | assert(type(rockspec_file) == "string") |
| 38 | 38 | ||
| 39 | local rockspec, err = fetch.load_rockspec(rockspec_file) | 39 | local rockspec, err = fetch.load_rockspec(rockspec_file) |
| @@ -201,7 +201,7 @@ function pack.run(...) | |||
| 201 | 201 | ||
| 202 | local file, err | 202 | local file, err |
| 203 | if arg:match(".*%.rockspec") then | 203 | if arg:match(".*%.rockspec") then |
| 204 | file, err = pack_source_rock(arg) | 204 | file, err = pack.pack_source_rock(arg) |
| 205 | else | 205 | else |
| 206 | file, err = do_pack_binary_rock(arg, version) | 206 | file, err = do_pack_binary_rock(arg, version) |
| 207 | end | 207 | end |
diff --git a/src/luarocks/upload.lua b/src/luarocks/upload.lua new file mode 100644 index 00000000..018e702d --- /dev/null +++ b/src/luarocks/upload.lua | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | |||
| 2 | local upload = {} | ||
| 3 | |||
| 4 | local util = require("luarocks.util") | ||
| 5 | local fetch = require("luarocks.fetch") | ||
| 6 | local pack = require("luarocks.pack") | ||
| 7 | local Api = require("luarocks.upload.api") | ||
| 8 | |||
| 9 | upload.help_summary = "Upload a rockspec to the public rocks repository." | ||
| 10 | upload.help_arguments = "[--skip-pack] [--api-key=<key>] [--force] <rockspec>" | ||
| 11 | upload.help = [[ | ||
| 12 | <rockspec> Pack a source rock file (.src.rock extension), | ||
| 13 | upload rockspec and source rock to server. | ||
| 14 | --skip-pack Do not pack and send source rock. | ||
| 15 | --api-key=<key> Give it an API key. It will be stored for subsequent uses. | ||
| 16 | --force Replace existing rockspec if the same revision of | ||
| 17 | a module already exists. This should be used only | ||
| 18 | in case of upload mistakes: when updating a rockspec, | ||
| 19 | increment the revision number instead. | ||
| 20 | ]] | ||
| 21 | |||
| 22 | function upload.run(...) | ||
| 23 | local flags, fname = util.parse_flags(...) | ||
| 24 | if not fname then | ||
| 25 | return nil, "Missing rockspec. "..util.see_help("upload") | ||
| 26 | end | ||
| 27 | |||
| 28 | local api, err = Api.new(flags) | ||
| 29 | if not api then | ||
| 30 | return nil, err | ||
| 31 | end | ||
| 32 | |||
| 33 | local rockspec, err, errcode = fetch.load_rockspec(fname) | ||
| 34 | if err then | ||
| 35 | return nil, err, errcode | ||
| 36 | end | ||
| 37 | |||
| 38 | util.printout("Sending " .. tostring(fname) .. " ...") | ||
| 39 | local res, err = api:method("check_rockspec", { | ||
| 40 | package = rockspec.package, | ||
| 41 | version = rockspec.version | ||
| 42 | }) | ||
| 43 | if not res then return nil, err end | ||
| 44 | |||
| 45 | if not res.module then | ||
| 46 | util.printout("Will create new module (" .. tostring(rockspec.package) .. ")") | ||
| 47 | end | ||
| 48 | if res.version and not flags["force"] then | ||
| 49 | return nil, "Revision "..rockspec.version.." already exists on the server. "..util.see_help("upload") | ||
| 50 | end | ||
| 51 | |||
| 52 | local rock_fname | ||
| 53 | if not flags["skip-pack"] then | ||
| 54 | util.printout("Packing " .. tostring(rockspec.package)) | ||
| 55 | rock_fname, err = pack.pack_source_rock(fname) | ||
| 56 | if not rock_fname then | ||
| 57 | return nil, err | ||
| 58 | end | ||
| 59 | end | ||
| 60 | |||
| 61 | local multipart = require("luarocks.upload.multipart") | ||
| 62 | |||
| 63 | res, err = api:method("upload", nil, { | ||
| 64 | rockspec_file = multipart.new_file(fname) | ||
| 65 | }) | ||
| 66 | if not res then return nil, err end | ||
| 67 | |||
| 68 | if res.is_new and #res.manifests == 0 then | ||
| 69 | util.printerr("Warning: module not added to root manifest due to name taken.") | ||
| 70 | end | ||
| 71 | |||
| 72 | local module_url = res.module_url | ||
| 73 | |||
| 74 | if rock_fname then | ||
| 75 | util.printout(("Sending " .. tostring(rock_fname) .. " ...")) | ||
| 76 | res, err = api:method("upload_rock/" .. tostring(res.version.id), nil, { | ||
| 77 | rock_file = multipart.new_file(rock_fname) | ||
| 78 | }) | ||
| 79 | if not res then return nil, err end | ||
| 80 | end | ||
| 81 | |||
| 82 | util.printout() | ||
| 83 | util.printout("Done: " .. tostring(module_url)) | ||
| 84 | util.printout() | ||
| 85 | return true | ||
| 86 | end | ||
| 87 | |||
| 88 | return upload | ||
diff --git a/src/luarocks/upload/multipart.lua b/src/luarocks/upload/multipart.lua new file mode 100644 index 00000000..95afe1b3 --- /dev/null +++ b/src/luarocks/upload/multipart.lua | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | |||
| 2 | local multipart = {} | ||
| 3 | |||
| 4 | local url = require("socket.url") | ||
| 5 | |||
| 6 | local File = {} | ||
| 7 | |||
| 8 | local unpack = unpack or table.unpack | ||
| 9 | |||
| 10 | math.randomseed(os.time()) | ||
| 11 | |||
| 12 | function File:mime() | ||
| 13 | if not self.mimetype then | ||
| 14 | local mimetypes_ok, mimetypes = pcall(require, "mimetypes") | ||
| 15 | if mimetypes_ok then | ||
| 16 | self.mimetype = mimetypes.guess(self.fname) | ||
| 17 | else | ||
| 18 | self.mimetype = "application/octet-stream" | ||
| 19 | end | ||
| 20 | end | ||
| 21 | return self.mimetype | ||
| 22 | end | ||
| 23 | |||
| 24 | function File:content() | ||
| 25 | local fd = io.open(self.fname) | ||
| 26 | if not fd then | ||
| 27 | return nil, "Failed to open file: "..self.fname | ||
| 28 | end | ||
| 29 | local data = fd:read("*a") | ||
| 30 | fd:close() | ||
| 31 | return data | ||
| 32 | end | ||
| 33 | |||
| 34 | local function rand_string(len) | ||
| 35 | local shuffled = {} | ||
| 36 | for i = 1, len do | ||
| 37 | local r = math.random(97, 122) | ||
| 38 | if math.random() >= 0.5 then | ||
| 39 | r = r - 32 | ||
| 40 | end | ||
| 41 | shuffled[i] = r | ||
| 42 | end | ||
| 43 | return string.char(unpack(shuffled)) | ||
| 44 | end | ||
| 45 | |||
| 46 | -- multipart encodes params | ||
| 47 | -- returns encoded string,boundary | ||
| 48 | -- params is an a table of tuple tables: | ||
| 49 | -- params = { | ||
| 50 | -- {key1, value2}, | ||
| 51 | -- {key2, value2}, | ||
| 52 | -- key3: value3 | ||
| 53 | -- } | ||
| 54 | function multipart.encode(params) | ||
| 55 | local tuples = { } | ||
| 56 | for i = 1, #params do | ||
| 57 | tuples[i] = params[i] | ||
| 58 | end | ||
| 59 | for k,v in pairs(params) do | ||
| 60 | if type(k) == "string" then | ||
| 61 | table.insert(tuples, {k, v}) | ||
| 62 | end | ||
| 63 | end | ||
| 64 | local chunks = {} | ||
| 65 | for _, tuple in ipairs(tuples) do | ||
| 66 | local k,v = unpack(tuple) | ||
| 67 | k = url.escape(k) | ||
| 68 | local buffer = { 'Content-Disposition: form-data; name="' .. k .. '"' } | ||
| 69 | local content | ||
| 70 | if type(v) == "table" and v.__class == File then | ||
| 71 | buffer[1] = buffer[1] .. ('; filename="' .. v.fname:gsub(".*/", "") .. '"') | ||
| 72 | table.insert(buffer, "Content-type: " .. v:mime()) | ||
| 73 | content = v:content() | ||
| 74 | else | ||
| 75 | content = v | ||
| 76 | end | ||
| 77 | table.insert(buffer, "") | ||
| 78 | table.insert(buffer, content) | ||
| 79 | table.insert(chunks, table.concat(buffer, "\r\n")) | ||
| 80 | end | ||
| 81 | local boundary | ||
| 82 | while not boundary do | ||
| 83 | boundary = "Boundary" .. rand_string(16) | ||
| 84 | for _, chunk in ipairs(chunks) do | ||
| 85 | if chunk:find(boundary) then | ||
| 86 | boundary = nil | ||
| 87 | break | ||
| 88 | end | ||
| 89 | end | ||
| 90 | end | ||
| 91 | local inner = "\r\n--" .. boundary .. "\r\n" | ||
| 92 | return table.concat({ "--", boundary, "\r\n", | ||
| 93 | table.concat(chunks, inner), | ||
| 94 | "\r\n", "--", boundary, "--", "\r\n" }), boundary | ||
| 95 | end | ||
| 96 | |||
| 97 | function multipart.new_file(fname, mime) | ||
| 98 | local self = {} | ||
| 99 | setmetatable(self, { __index = File }) | ||
| 100 | self.__class = File | ||
| 101 | self.fname = fname | ||
| 102 | self.mimetype = mime | ||
| 103 | return self | ||
| 104 | end | ||
| 105 | |||
| 106 | return multipart | ||
| 107 | |||
