~ubuntu-branches/ubuntu/vivid/luarocks/vivid-proposed

« back to all changes in this revision

Viewing changes to src/luarocks/download.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2014-10-11 15:26:47 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20141011152647-bkfciayfdz6elvv3
Tags: 2.2.0+dfsg-1
* New upstream release
* patch 0001-Fixed-detection-of-Debian-paths removed (applied upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
--- Module implementing the luarocks "download" command.
3
3
-- Download a rock from the repository.
4
 
module("luarocks.download", package.seeall)
 
4
--module("luarocks.download", package.seeall)
 
5
local download = {}
 
6
package.loaded["luarocks.download"] = download
5
7
 
6
8
local util = require("luarocks.util")
7
9
local path = require("luarocks.path")
8
10
local fetch = require("luarocks.fetch")
9
11
local search = require("luarocks.search")
10
 
 
11
 
help_summary = "Download a specific rock file from a rocks server."
12
 
help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]"
13
 
 
14
 
help = [[
 
12
local fs = require("luarocks.fs")
 
13
local dir = require("luarocks.dir")
 
14
 
 
15
download.help_summary = "Download a specific rock file from a rocks server."
 
16
download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]"
 
17
 
 
18
download.help = [[
15
19
--all          Download all files if there are multiple matches.
16
20
--source       Download .src.rock if available.
17
21
--rockspec     Download .rockspec if available.
18
22
--arch=<arch>  Download rock for a specific architecture.
19
23
]]
20
24
 
21
 
function download(arch, name, version, all)
 
25
local function get_file(filename)
 
26
   local protocol, pathname = dir.split_url(filename)
 
27
   if protocol == "file" then
 
28
      local ok, err = fs.copy(pathname, fs.current_dir())
 
29
      if ok then
 
30
         return pathname
 
31
      else
 
32
         return nil, err
 
33
      end
 
34
   else
 
35
      return fetch.fetch_url(filename)
 
36
   end
 
37
end
 
38
 
 
39
function download.download(arch, name, version, all)
22
40
   local results, err
23
41
   local query = search.make_query(name, version)
24
42
   if arch then query.arch = arch end
25
43
   if all then
26
44
      if name == "" then query.exact_name = false end
27
 
      results, err = search.search_repos(query)
 
45
      results = search.search_repos(query)
28
46
   else
29
47
      results, err = search.find_suitable_rock(query)
30
48
   end
31
49
   if type(results) == "string" then
32
 
      local file = fetch.fetch_url(results)
33
 
      return file
 
50
      return get_file(results)
34
51
   elseif type(results) == "table" and next(results) then
35
52
      if all then
36
53
         local all_ok = true
39
56
            for version, versions in pairs(result) do
40
57
               for _,items in pairs(versions) do
41
58
                  local filename = path.make_url(items.repo, name, version, items.arch)
42
 
                  local ok, err = fetch.fetch_url(filename)
 
59
                  local ok, err = get_file(filename)
43
60
                  if not ok then
44
61
                     all_ok = false
45
62
                     any_err = any_err .. "\n" .. err
50
67
         return all_ok, any_err
51
68
      else
52
69
         util.printerr("Multiple search results were returned.")
53
 
         util.printout()
54
 
         util.printout("Search results:")
55
 
         util.printout("---------------")
 
70
         util.title("Search results:")
56
71
         search.print_results(results)
57
72
         return nil, "Please narrow your query or use --all."
58
73
      end
66
81
-- version may also be passed.
67
82
-- @return boolean or (nil, string): true if successful or nil followed
68
83
-- by an error message.
69
 
function run(...)
 
84
function download.run(...)
70
85
   local flags, name, version = util.parse_flags(...)
71
86
   
72
87
   assert(type(version) == "string" or not version)
85
100
      arch = flags["arch"]
86
101
   end
87
102
   
88
 
   local dl, err = download(arch, name, version, flags["all"])
 
103
   local dl, err = download.download(arch, name, version, flags["all"])
89
104
   return dl and true, err
90
105
end
 
106
 
 
107
return download