From ec074fcb99c44941c38775e51700d60cbd59cfd4 Mon Sep 17 00:00:00 2001
From: hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>
Date: Fri, 14 Aug 2009 20:57:38 +0000
Subject: further cleanup in fs abstractions

git-svn-id: http://luarocks.org/svn/luarocks/trunk@48 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
---
 src/luarocks/cfg.lua           | 13 +++++++++--
 src/luarocks/fs/lua.lua        | 49 ++++++++++++------------------------------
 src/luarocks/fs/unix/tools.lua | 30 ++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 37 deletions(-)

(limited to 'src')

diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua
index ddcfac9d..bd3bdbc3 100644
--- a/src/luarocks/cfg.lua
+++ b/src/luarocks/cfg.lua
@@ -1,6 +1,6 @@
 
-local rawset, next, table, pairs, print, require, io, os, setmetatable, pcall =
-      rawset, next, table, pairs, print, require, io, os, setmetatable, pcall
+local rawset, next, table, pairs, print, require, io, os, setmetatable, pcall, ipairs, package =
+      rawset, next, table, pairs, print, require, io, os, setmetatable, pcall, ipairs, package
 
 --- Configuration for LuaRocks.
 -- Tries to load the user's configuration file and
@@ -109,6 +109,8 @@ local defaults = {
    root_dir = root,
    rocks_dir = root.."/lib/luarocks/rocks",
    scripts_dir = root.."/bin/",
+   lua_modules_path = "/share/lua/5.1/",
+   bin_modules_path = "/lib/lua/5.1/",
    lua_modules_dir = root.."/share/lua/5.1/",
    bin_modules_dir = root.."/lib/lua/5.1/",
 
@@ -269,3 +271,10 @@ local cfg_mt = {
    end
 }
 setmetatable(_M, cfg_mt)
+
+
+for _,tree in ipairs(rocks_trees) do
+   package.path = tree..lua_modules_path.."/?.lua;"..tree..lua_modules_path.."/?/init.lua;"..package.path
+   package.cpath = tree..bin_modules_path.."/?."..lib_extension..";"..package.cpath
+end
+
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua
index 013fad5f..f989b652 100644
--- a/src/luarocks/fs/lua.lua
+++ b/src/luarocks/fs/lua.lua
@@ -10,9 +10,9 @@ local dir = require("luarocks.dir")
 
 local zip_ok, zip = pcall(require, "luarocks.tools.zip")
 local lfs_ok, lfs = pcall(require, "lfs")
-
 local curl_ok, curl = pcall(require, "luacurl")
 local md5_ok, md5 = pcall(require, "md5")
+local posix_ok, posix = pcall(require, "posix")
 
 local tar = require("luarocks.tools.tar")
 local patch = require("luarocks.tools.patch")
@@ -467,6 +467,19 @@ end
 
 end
 
+---------------------------------------------------------------------
+-- POSIX functions
+---------------------------------------------------------------------
+
+if posix_ok then
+
+function chmod(file, mode)
+   local err = posix.chmod(file, mode)
+   return err == 0
+end
+
+end
+
 ---------------------------------------------------------------------
 -- Other functions
 ---------------------------------------------------------------------
@@ -503,37 +516,3 @@ function move(src, dest)
    end
    return true
 end
-
----------------------------------------------------------------------
--- TODO These still reference external binaries
----------------------------------------------------------------------
-
---- Unpack an archive.
--- Extract the contents of an archive, detecting its format by
--- filename extension.
--- @param archive string: Filename of archive.
--- @return boolean or (boolean, string): true on success, false and an error message on failure.
-function unpack_archive(archive)
-   assert(type(archive) == "string")
-
-   local ok
-   if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then
-      -- ok = fs.execute("tar zxvpf ", archive)
-      ok = fs.execute_string("gunzip -c "..archive.."|tar -xf -")
-   elseif archive:match("%.tar%.bz2$") then
-      -- ok = fs.execute("tar jxvpf ", archive)
-      ok = fs.execute_string("bunzip2 -c "..archive.."|tar -xf -")
-   elseif archive:match("%.zip$") then
-      ok = fs.execute("unzip ", archive)
-   elseif archive:match("%.lua$") or archive:match("%.c$") then
-      -- Ignore .lua and .c files; they don't need to be extracted.
-      return true
-   else
-      local ext = archive:match(".*(%..*)")
-      return false, "Unrecognized filename extension "..(ext or "")
-   end
-   if not ok then
-      return false, "Failed extracting "..archive
-   end
-   return true
-end
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua
index 71dfe074..b4cb43a7 100644
--- a/src/luarocks/fs/unix/tools.lua
+++ b/src/luarocks/fs/unix/tools.lua
@@ -321,3 +321,33 @@ function check_md5(file, md5sum)
       return false
    end
 end
+
+--- Unpack an archive.
+-- Extract the contents of an archive, detecting its format by
+-- filename extension.
+-- @param archive string: Filename of archive.
+-- @return boolean or (boolean, string): true on success, false and an error message on failure.
+function unpack_archive(archive)
+   assert(type(archive) == "string")
+
+   local ok
+   if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then
+      -- ok = fs.execute("tar zxvpf ", archive)
+      ok = fs.execute_string("gunzip -c "..archive.."|tar -xf -")
+   elseif archive:match("%.tar%.bz2$") then
+      -- ok = fs.execute("tar jxvpf ", archive)
+      ok = fs.execute_string("bunzip2 -c "..archive.."|tar -xf -")
+   elseif archive:match("%.zip$") then
+      ok = fs.execute("unzip ", archive)
+   elseif archive:match("%.lua$") or archive:match("%.c$") then
+      -- Ignore .lua and .c files; they don't need to be extracted.
+      return true
+   else
+      local ext = archive:match(".*(%..*)")
+      return false, "Unrecognized filename extension "..(ext or "")
+   end
+   if not ok then
+      return false, "Failed extracting "..archive
+   end
+   return true
+end
-- 
cgit v1.2.3-55-g6feb