From 6faa5af7e8711a0cd3d963d47e59e1b078501411 Mon Sep 17 00:00:00 2001 From: hisham Date: Sun, 11 Oct 2009 04:48:30 +0000 Subject: 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 --- src/luarocks/add.lua | 49 +++--------------------------------- src/luarocks/cache.lua | 56 ++++++++++++++++++++++++++++++++++++++++++ src/luarocks/refresh_cache.lua | 25 +++++++++++++++++++ 3 files changed, 84 insertions(+), 46 deletions(-) create mode 100644 src/luarocks/cache.lua create mode 100644 src/luarocks/refresh_cache.lua (limited to 'src') 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") local manif = require("luarocks.manif") local index = require("luarocks.index") local fs = require("luarocks.fs") +local cache = require("luarocks.cache") help_summary = "Add a rock or rockpec to a rocks server." help_arguments = "[--to=] {|]}" @@ -20,50 +21,6 @@ If not given, the default server set in the upload_server variable from the configuration file is used instead. ]] -local function split_server_url(server, user, password) - local protocol, server_path = dir.split_url(server) - if server_path:match("@") then - local credentials - credentials, server_path = server_path:match("([^@]*)@(.*)") - if credentials:match(":") then - user, password = credentials:match("([^:]*):(.*)") - else - user = credentials - end - end - local local_cache - if cfg.local_cache then - local_cache = cfg.local_cache .. "/" .. server_path - end - return local_cache, protocol, server_path, user, password -end - -local function refresh_local_cache(server, user, password) - local local_cache, protocol, server_path, user, password = split_server_url(server, user, password) - - fs.make_dir(cfg.local_cache) - - local tmp_cache = false - if not local_cache then - local_cache = fs.make_temp_dir("local_cache") - tmp_cache = true - end - local ok = fs.make_dir(local_cache) - if not ok then - return nil, "Failed creating local cache dir." - end - fs.change_dir(local_cache) - print("Refreshing cache "..local_cache.."...") - - local login_info = "" - if user then login_info = " --user="..user end - if password then login_info = login_info .. " --password="..password end - - -- TODO abstract away explicit 'wget' call - fs.execute("wget -q -m -nd "..protocol.."://"..server_path..login_info) - return local_cache, protocol, server_path, user, password -end - local function add_file_to_server(refresh, rockfile, server) if not fs.exists(rockfile) then return nil, "Could not find "..rockfile @@ -73,9 +30,9 @@ local function add_file_to_server(refresh, rockfile, server) local local_cache, protocol, server_path, user, password if refresh then - local_cache, protocol, server_path, user, password = refresh_local_cache(server, cfg.upload_user, cfg.upload_password) + local_cache, protocol, server_path, user, password = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password) else - local_cache, protocol, server_path, user, password = split_server_url(server, cfg.upload_user, cfg.upload_password) + local_cache, protocol, server_path, user, password = cache.split_server_url(server, cfg.upload_user, cfg.upload_password) end fs.change_dir(local_cache) 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 @@ + +--- Module handling the LuaRocks local cache. +-- Adds a rock or rockspec to a rocks server. +module("luarocks.cache", package.seeall) + +local fs = require("luarocks.fs") +local cfg = require("luarocks.cfg") +local dir = require("luarocks.dir") + +function split_server_url(server, user, password) + local protocol, server_path = dir.split_url(server) + if server_path:match("@") then + local credentials + credentials, server_path = server_path:match("([^@]*)@(.*)") + if credentials:match(":") then + user, password = credentials:match("([^:]*):(.*)") + else + user = credentials + end + end + local local_cache + if cfg.local_cache then + local_cache = cfg.local_cache .. "/" .. server_path + end + return local_cache, protocol, server_path, user, password +end + +function refresh_local_cache(server, user, password) + local local_cache, protocol, server_path, user, password = split_server_url(server, user, password) + + fs.make_dir(cfg.local_cache) + + local tmp_cache = false + if not local_cache then + local_cache = fs.make_temp_dir("local_cache") + tmp_cache = true + end + local ok = fs.make_dir(local_cache) + if not ok then + return nil, "Failed creating local cache dir." + end + fs.change_dir(local_cache) + print("Refreshing cache "..local_cache.."...") + + local login_info = "" + if user then login_info = " --user="..user end + if password then login_info = login_info .. " --password="..password end + + -- TODO abstract away explicit 'wget' call + local ok = fs.execute("wget -q -m -nd "..protocol.."://"..server_path..login_info) + if not ok then + return nil, "Failed downloading cache." + end + return local_cache, protocol, server_path, user, password +end + 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 @@ + +module("luarocks.refresh_cache", package.seeall) + +local util = require("luarocks.util") +local cfg = require("luarocks.cfg") +local cache = require("luarocks.cache") + +function run(...) + local flags = util.parse_flags(...) + local server = flags["to"] + if not server then server = cfg.upload_server end + if not server then + return nil, "No server specified with --to and no default configured with upload_server." + end + if cfg.upload_aliases then + server = cfg.upload_aliases[server] or server + end + local ok, err = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password) + if not ok then + return nil, err + else + return true + end +end + -- cgit v1.2.3-55-g6feb