diff options
author | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-11 04:48:30 +0000 |
---|---|---|
committer | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-11 04:48:30 +0000 |
commit | 6faa5af7e8711a0cd3d963d47e59e1b078501411 (patch) | |
tree | 3b42d26142670368fab0c350b6ee08f067f2f26c /src | |
parent | 5348d5b36763979d8e22fa37a47249c128e30aa6 (diff) | |
download | luarocks-6faa5af7e8711a0cd3d963d47e59e1b078501411.tar.gz luarocks-6faa5af7e8711a0cd3d963d47e59e1b078501411.tar.bz2 luarocks-6faa5af7e8711a0cd3d963d47e59e1b078501411.zip |
make separate cache module, expose refresh-cache command to luarocks-admin
git-svn-id: http://luarocks.org/svn/luarocks/trunk@78 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/add.lua | 49 | ||||
-rw-r--r-- | src/luarocks/cache.lua | 56 | ||||
-rw-r--r-- | src/luarocks/refresh_cache.lua | 25 |
3 files changed, 84 insertions, 46 deletions
diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua index ed995d98..11e4048b 100644 --- a/src/luarocks/add.lua +++ b/src/luarocks/add.lua | |||
@@ -10,6 +10,7 @@ local dir = require("luarocks.dir") | |||
10 | local manif = require("luarocks.manif") | 10 | local manif = require("luarocks.manif") |
11 | local index = require("luarocks.index") | 11 | local index = require("luarocks.index") |
12 | local fs = require("luarocks.fs") | 12 | local fs = require("luarocks.fs") |
13 | local cache = require("luarocks.cache") | ||
13 | 14 | ||
14 | help_summary = "Add a rock or rockpec to a rocks server." | 15 | help_summary = "Add a rock or rockpec to a rocks server." |
15 | help_arguments = "[--to=<server>] {<rockspec>|<rock>]}" | 16 | help_arguments = "[--to=<server>] {<rockspec>|<rock>]}" |
@@ -20,50 +21,6 @@ If not given, the default server set in the upload_server variable | |||
20 | from the configuration file is used instead. | 21 | from the configuration file is used instead. |
21 | ]] | 22 | ]] |
22 | 23 | ||
23 | local function split_server_url(server, user, password) | ||
24 | local protocol, server_path = dir.split_url(server) | ||
25 | if server_path:match("@") then | ||
26 | local credentials | ||
27 | credentials, server_path = server_path:match("([^@]*)@(.*)") | ||
28 | if credentials:match(":") then | ||
29 | user, password = credentials:match("([^:]*):(.*)") | ||
30 | else | ||
31 | user = credentials | ||
32 | end | ||
33 | end | ||
34 | local local_cache | ||
35 | if cfg.local_cache then | ||
36 | local_cache = cfg.local_cache .. "/" .. server_path | ||
37 | end | ||
38 | return local_cache, protocol, server_path, user, password | ||
39 | end | ||
40 | |||
41 | local function refresh_local_cache(server, user, password) | ||
42 | local local_cache, protocol, server_path, user, password = split_server_url(server, user, password) | ||
43 | |||
44 | fs.make_dir(cfg.local_cache) | ||
45 | |||
46 | local tmp_cache = false | ||
47 | if not local_cache then | ||
48 | local_cache = fs.make_temp_dir("local_cache") | ||
49 | tmp_cache = true | ||
50 | end | ||
51 | local ok = fs.make_dir(local_cache) | ||
52 | if not ok then | ||
53 | return nil, "Failed creating local cache dir." | ||
54 | end | ||
55 | fs.change_dir(local_cache) | ||
56 | print("Refreshing cache "..local_cache.."...") | ||
57 | |||
58 | local login_info = "" | ||
59 | if user then login_info = " --user="..user end | ||
60 | if password then login_info = login_info .. " --password="..password end | ||
61 | |||
62 | -- TODO abstract away explicit 'wget' call | ||
63 | fs.execute("wget -q -m -nd "..protocol.."://"..server_path..login_info) | ||
64 | return local_cache, protocol, server_path, user, password | ||
65 | end | ||
66 | |||
67 | local function add_file_to_server(refresh, rockfile, server) | 24 | local function add_file_to_server(refresh, rockfile, server) |
68 | if not fs.exists(rockfile) then | 25 | if not fs.exists(rockfile) then |
69 | return nil, "Could not find "..rockfile | 26 | return nil, "Could not find "..rockfile |
@@ -73,9 +30,9 @@ local function add_file_to_server(refresh, rockfile, server) | |||
73 | 30 | ||
74 | local local_cache, protocol, server_path, user, password | 31 | local local_cache, protocol, server_path, user, password |
75 | if refresh then | 32 | if refresh then |
76 | local_cache, protocol, server_path, user, password = refresh_local_cache(server, cfg.upload_user, cfg.upload_password) | 33 | local_cache, protocol, server_path, user, password = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password) |
77 | else | 34 | else |
78 | local_cache, protocol, server_path, user, password = split_server_url(server, cfg.upload_user, cfg.upload_password) | 35 | local_cache, protocol, server_path, user, password = cache.split_server_url(server, cfg.upload_user, cfg.upload_password) |
79 | end | 36 | end |
80 | fs.change_dir(local_cache) | 37 | fs.change_dir(local_cache) |
81 | print("Copying file "..rockfile.." to "..local_cache.."...") | 38 | print("Copying file "..rockfile.." to "..local_cache.."...") |
diff --git a/src/luarocks/cache.lua b/src/luarocks/cache.lua new file mode 100644 index 00000000..abf0a0fe --- /dev/null +++ b/src/luarocks/cache.lua | |||
@@ -0,0 +1,56 @@ | |||
1 | |||
2 | --- Module handling the LuaRocks local cache. | ||
3 | -- Adds a rock or rockspec to a rocks server. | ||
4 | module("luarocks.cache", package.seeall) | ||
5 | |||
6 | local fs = require("luarocks.fs") | ||
7 | local cfg = require("luarocks.cfg") | ||
8 | local dir = require("luarocks.dir") | ||
9 | |||
10 | function split_server_url(server, user, password) | ||
11 | local protocol, server_path = dir.split_url(server) | ||
12 | if server_path:match("@") then | ||
13 | local credentials | ||
14 | credentials, server_path = server_path:match("([^@]*)@(.*)") | ||
15 | if credentials:match(":") then | ||
16 | user, password = credentials:match("([^:]*):(.*)") | ||
17 | else | ||
18 | user = credentials | ||
19 | end | ||
20 | end | ||
21 | local local_cache | ||
22 | if cfg.local_cache then | ||
23 | local_cache = cfg.local_cache .. "/" .. server_path | ||
24 | end | ||
25 | return local_cache, protocol, server_path, user, password | ||
26 | end | ||
27 | |||
28 | function refresh_local_cache(server, user, password) | ||
29 | local local_cache, protocol, server_path, user, password = split_server_url(server, user, password) | ||
30 | |||
31 | fs.make_dir(cfg.local_cache) | ||
32 | |||
33 | local tmp_cache = false | ||
34 | if not local_cache then | ||
35 | local_cache = fs.make_temp_dir("local_cache") | ||
36 | tmp_cache = true | ||
37 | end | ||
38 | local ok = fs.make_dir(local_cache) | ||
39 | if not ok then | ||
40 | return nil, "Failed creating local cache dir." | ||
41 | end | ||
42 | fs.change_dir(local_cache) | ||
43 | print("Refreshing cache "..local_cache.."...") | ||
44 | |||
45 | local login_info = "" | ||
46 | if user then login_info = " --user="..user end | ||
47 | if password then login_info = login_info .. " --password="..password end | ||
48 | |||
49 | -- TODO abstract away explicit 'wget' call | ||
50 | local ok = fs.execute("wget -q -m -nd "..protocol.."://"..server_path..login_info) | ||
51 | if not ok then | ||
52 | return nil, "Failed downloading cache." | ||
53 | end | ||
54 | return local_cache, protocol, server_path, user, password | ||
55 | end | ||
56 | |||
diff --git a/src/luarocks/refresh_cache.lua b/src/luarocks/refresh_cache.lua new file mode 100644 index 00000000..91a78a7f --- /dev/null +++ b/src/luarocks/refresh_cache.lua | |||
@@ -0,0 +1,25 @@ | |||
1 | |||
2 | module("luarocks.refresh_cache", package.seeall) | ||
3 | |||
4 | local util = require("luarocks.util") | ||
5 | local cfg = require("luarocks.cfg") | ||
6 | local cache = require("luarocks.cache") | ||
7 | |||
8 | function run(...) | ||
9 | local flags = util.parse_flags(...) | ||
10 | local server = flags["to"] | ||
11 | if not server then server = cfg.upload_server end | ||
12 | if not server then | ||
13 | return nil, "No server specified with --to and no default configured with upload_server." | ||
14 | end | ||
15 | if cfg.upload_aliases then | ||
16 | server = cfg.upload_aliases[server] or server | ||
17 | end | ||
18 | local ok, err = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password) | ||
19 | if not ok then | ||
20 | return nil, err | ||
21 | else | ||
22 | return true | ||
23 | end | ||
24 | end | ||
25 | |||