lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I start every script I write in this manner with the following code
since I usually have a modules path in the same dir as the script:

---------------------------------------------------------------
-- Bootstrapping functions required to coalesce paths
local
function exec(cmd, echo)
	if(type(echo) == "nil") then
		echo = true
	end

	if(echo) then
		print(cmd)
		print("")
	end
	local res = io.popen(cmd):read("*a")
	return res:sub(1, res:len()-1)
end

local
function stripfilename(filename)
	return string.match(filename, "(.+)/[^/]*%.%w+$")
end

local
function getextension(filename)
	return filename:match(".+%.(%w+)$")
end

local
function addmodulepath(path)
	-- add to package paths (if not already present)
	if not string.find(package.path, path, 0, true) then
		package.path = string.format("%s/?.lua;%s", path, package.path)
		package.path = string.format("%s/?/init.lua;%s", path, package.path)
		package.cpath = string.format("%s/?.so;%s", path, package.cpath)
	end
end


local
function init()
	local pwd = exec("pwd", false)
	local subpath = stripfilename(arg[0])

	local path
	if(subpath) then
		path = pwd.."/"..subpath
	else
		path = pwd
	end

	return {
		path = path,
	}
end

local script = init()
addmodulepath(script.path.."/modules")


The other module I use a lot in this instance is LuaFileSystem.

On top of that, I usually have these functions available through a
utilities module:

function stripfilename(filename)
	return string.match(filename, "(.+)/[^/]*%.%w+$")
end

function strippath(filename)
	return string.match(filename, ".+/([^/]*%.%w+)$")
end

function stripextension(filename)
	local idx = filename:match(".+()%.%w+$")
	if(idx) then
		return filename:sub(1, idx-1)
	else
		return filename
	end
end

function getextension(filename)
	return filename:match(".+%.(%w+)$")
end

function pathchunks(name)
	local chunks = {}
	for w in string.gmatch(name, "[^/\\]+") do
	   table.insert(chunks, 1, w)
	end
	return chunks
end

function ensurepath(path)
	local chunks = pathchunks(path)
	local originalpath = lfs.currentdir()
	lfs.chdir("/")
	for i=#chunks, 1, -1 do
		local c = chunks[i]
		local exists = lfs.attributes(c) ~= nil
		if(not exists) then
			lfs.mkdir(c)
		end
		lfs.chdir(c)
	end
	lfs.chdir(originalpath)
	return path
end

function writefile(filename, str)
	local file, err = io.open(filename, "w")
	if(not file) then
		error("%s  writing to file %s", err, filename)
		return	
	end
	
	file:write(str)
	file:close()
end

function findfileinpath(path, filename, recursive)
	local result
	local originalpath = lfs.currentdir()
	if lfs.chdir(path) then
		for f in lfs.dir(path) do
			local subpath = path .. "/" .. f
			if f == filename then
				return subpath
			else
				if recursive then
					local attr = lfs.attributes(subpath)
					if attr.mode == "directory" and f:find('.', 1, true) ~= 1 then
						result = findfileinpath(subpath, filename, true)
						if(result) then
							return result
						end
					end
				end
			end
		end
	end
	lfs.chdir(originalpath)
	return result
end