[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile
From: |
rghetta |
Subject: |
[Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory |
Date: |
Sat, 11 Jun 2005 20:37:39 +0200 |
User-agent: |
Mozilla Thunderbird 1.0.2 (X11/20050322) |
Monotone has two levels of configuration files, a per-workdir
MT/monotonerc and a per-user ~/.monotone/monotonerc.
Often is necessary to have also a per-site customization or simply share
part of the configuration beetween developers (for example, a library of
lua "snippets").
Loading all scripts contained in a specific directory is also a common.
To support these use case, this patch adds two new lua functions:
include(<filename>) and includedir(<path>)
The first function works like --rcfile, while the latter receives a path
and loads all scripts contained there, in alphabetical order.
The --rcfile option now also accept a directory as argument, with the
same effect.
Note:
Include functions have the same precedence of the calling rcfile, i.e.
if you include() something from your home monotonerc, it can be
superceded by MT/monotonerc or --rcfile.
Riccardo
#
# add_file "tests/t_lua_includedir.at"
#
# add_file "tests/t_rcfile_dir.at"
#
# patch "commands.cc"
# from [5d992c6d2826ea22eba4bfc5366f35e21c433a86]
# to [aef25ffe8205ea72975eb43570d421afd9ad2d69]
#
# patch "lua.cc"
# from [c5a01c71aef633d6ad5ad78650e4e0601295b349]
# to [e4dd123e0c07b59361555b521f3b2cc17aa445b5]
#
# patch "tests/t_lua_includedir.at"
# from []
# to [75b5d1cc4e2af564e0beb2acd48243122891e0a5]
#
# patch "tests/t_rcfile_dir.at"
# from []
# to [06128ab21f137c3a69e4fce2db25b0ea3fa424de]
#
# patch "testsuite.at"
# from [353e351595bd4a6dadf8360e19ee41a4f4d8e68a]
# to [01d6670abecce04db3444e4874e2257a2c391c8c]
#
--- commands.cc
+++ commands.cc
@@ -2948,8 +2948,8 @@
// we have the following
//
- // old --- working
- // \ \
+ // old --- working
+ // / /
// chosen --- merged
//
// - old is the revision specified in MT/revision
--- lua.cc
+++ lua.cc
@@ -30,6 +30,7 @@
#include "sanity.hh"
#include "vocab.hh"
#include "platform.hh"
+#include "transforms.hh"
// defined in {std,test}_hooks.lua, converted
#include "test_hooks.h"
@@ -45,164 +46,6 @@
}
*/
-extern "C"
-{
- static int
- monotone_mkstemp_for_lua(lua_State *L)
- {
- int fd = -1;
- FILE **pf = NULL;
- char const *filename = lua_tostring (L, -1);
- std::string dup(filename);
-
- fd = monotone_mkstemp(dup);
-
- if (fd == -1)
- return 0;
-
- // this magic constructs a lua object which the lua io library
- // will enjoy working with
- pf = static_cast<FILE **>(lua_newuserdata(L, sizeof(FILE *)));
- *pf = fdopen(fd, "r+");
- lua_pushstring(L, "FILE*");
- lua_rawget(L, LUA_REGISTRYINDEX);
- lua_setmetatable(L, -2);
-
- lua_pushstring(L, dup.c_str());
-
- if (*pf == NULL)
- {
- lua_pushnil(L);
- lua_pushfstring(L, "%s", strerror(errno));
- lua_pushnumber(L, errno);
- return 3;
- }
- else
- return 2;
- }
-
- static int
- monotone_existsonpath_for_lua(lua_State *L)
- {
- const char *exe = lua_tostring(L, -1);
- lua_pushnumber(L, existsonpath(exe));
- return 1;
- }
-
- static int
- monotone_is_executable_for_lua(lua_State *L)
- {
- const char *path = lua_tostring(L, -1);
- lua_pushboolean(L, is_executable(path));
- return 1;
- }
-
- static int
- monotone_make_executable_for_lua(lua_State *L)
- {
- const char *path = lua_tostring(L, -1);
- lua_pushnumber(L, make_executable(path));
- return 1;
- }
-
- static int
- monotone_spawn_for_lua(lua_State *L)
- {
- int n = lua_gettop(L);
- const char *path = lua_tostring(L, -n);
- char **argv = (char**)malloc((n+1)*sizeof(char*));
- int i;
- pid_t ret;
- if (argv==NULL)
- return 0;
- argv[0] = (char*)path;
- for (i=1; i<n; i++) argv[i] = (char*)lua_tostring(L, -(n - i));
- argv[i] = NULL;
- ret = process_spawn(argv);
- free(argv);
- lua_pushnumber(L, ret);
- return 1;
- }
-
- static int
- monotone_wait_for_lua(lua_State *L)
- {
- pid_t pid = (pid_t)lua_tonumber(L, -1);
- int res;
- int ret;
- ret = process_wait(pid, &res);
- lua_pushnumber(L, res);
- lua_pushnumber(L, ret);
- return 2;
- }
-
- static int
- monotone_kill_for_lua(lua_State *L)
- {
- int n = lua_gettop(L);
- pid_t pid = (pid_t)lua_tonumber(L, -2);
- int sig;
- if (n>1)
- sig = (int)lua_tonumber(L, -1);
- else
- sig = SIGTERM;
- lua_pushnumber(L, process_kill(pid, sig));
- return 1;
- }
-
- static int
- monotone_sleep_for_lua(lua_State *L)
- {
- int seconds = (int)lua_tonumber(L, -1);
- lua_pushnumber(L, process_sleep(seconds));
- return 1;
- }
-
- static int
- monotone_guess_binary_for_lua(lua_State *L)
- {
- const char *path = lua_tostring(L, -1);
- N(path, F("guess_binary called with an invalid parameter"));
- lua_pushboolean(L, guess_binary(std::string(path, lua_strlen(L, -1))));
- return 1;
- }
-}
-
-
-lua_hooks::lua_hooks()
-{
- st = lua_open ();
- I(st);
-
- // no atpanic support in 4.x
- // lua_atpanic (st, &panic_thrower);
-
- luaopen_base(st);
- luaopen_io(st);
- luaopen_string(st);
- luaopen_math(st);
- luaopen_table(st);
- luaopen_debug(st);
-
- // add monotone-specific functions
- lua_register(st, "mkstemp", monotone_mkstemp_for_lua);
- lua_register(st, "existsonpath", monotone_existsonpath_for_lua);
- lua_register(st, "is_executable", monotone_is_executable_for_lua);
- lua_register(st, "make_executable", monotone_make_executable_for_lua);
- lua_register(st, "spawn", monotone_spawn_for_lua);
- lua_register(st, "wait", monotone_wait_for_lua);
- lua_register(st, "kill", monotone_kill_for_lua);
- lua_register(st, "sleep", monotone_sleep_for_lua);
- lua_register(st, "guess_binary", monotone_guess_binary_for_lua);
-}
-
-lua_hooks::~lua_hooks()
-{
- if (st)
- lua_close (st);
-}
-
-
// This Lua object represents a single imperative transaction with the lua
// interpreter. if it fails at any point, all further commands in the
// transaction are ignored. it cleans the lua stack up when it is
@@ -535,6 +378,217 @@
std::set<string> Lua::missing_functions;
+
+
+
+extern "C"
+{
+ static int
+ monotone_mkstemp_for_lua(lua_State *L)
+ {
+ int fd = -1;
+ FILE **pf = NULL;
+ char const *filename = lua_tostring (L, -1);
+ std::string dup(filename);
+
+ fd = monotone_mkstemp(dup);
+
+ if (fd == -1)
+ return 0;
+
+ // this magic constructs a lua object which the lua io library
+ // will enjoy working with
+ pf = static_cast<FILE **>(lua_newuserdata(L, sizeof(FILE *)));
+ *pf = fdopen(fd, "r+");
+ lua_pushstring(L, "FILE*");
+ lua_rawget(L, LUA_REGISTRYINDEX);
+ lua_setmetatable(L, -2);
+
+ lua_pushstring(L, dup.c_str());
+
+ if (*pf == NULL)
+ {
+ lua_pushnil(L);
+ lua_pushfstring(L, "%s", strerror(errno));
+ lua_pushnumber(L, errno);
+ return 3;
+ }
+ else
+ return 2;
+ }
+
+ static int
+ monotone_existsonpath_for_lua(lua_State *L)
+ {
+ const char *exe = lua_tostring(L, -1);
+ lua_pushnumber(L, existsonpath(exe));
+ return 1;
+ }
+
+ static int
+ monotone_is_executable_for_lua(lua_State *L)
+ {
+ const char *path = lua_tostring(L, -1);
+ lua_pushboolean(L, is_executable(path));
+ return 1;
+ }
+
+ static int
+ monotone_make_executable_for_lua(lua_State *L)
+ {
+ const char *path = lua_tostring(L, -1);
+ lua_pushnumber(L, make_executable(path));
+ return 1;
+ }
+
+ static int
+ monotone_spawn_for_lua(lua_State *L)
+ {
+ int n = lua_gettop(L);
+ const char *path = lua_tostring(L, -n);
+ char **argv = (char**)malloc((n+1)*sizeof(char*));
+ int i;
+ pid_t ret;
+ if (argv==NULL)
+ return 0;
+ argv[0] = (char*)path;
+ for (i=1; i<n; i++) argv[i] = (char*)lua_tostring(L, -(n - i));
+ argv[i] = NULL;
+ ret = process_spawn(argv);
+ free(argv);
+ lua_pushnumber(L, ret);
+ return 1;
+ }
+
+ static int
+ monotone_wait_for_lua(lua_State *L)
+ {
+ pid_t pid = (pid_t)lua_tonumber(L, -1);
+ int res;
+ int ret;
+ ret = process_wait(pid, &res);
+ lua_pushnumber(L, res);
+ lua_pushnumber(L, ret);
+ return 2;
+ }
+
+ static int
+ monotone_kill_for_lua(lua_State *L)
+ {
+ int n = lua_gettop(L);
+ pid_t pid = (pid_t)lua_tonumber(L, -2);
+ int sig;
+ if (n>1)
+ sig = (int)lua_tonumber(L, -1);
+ else
+ sig = SIGTERM;
+ lua_pushnumber(L, process_kill(pid, sig));
+ return 1;
+ }
+
+ static int
+ monotone_sleep_for_lua(lua_State *L)
+ {
+ int seconds = (int)lua_tonumber(L, -1);
+ lua_pushnumber(L, process_sleep(seconds));
+ return 1;
+ }
+
+ static int
+ monotone_guess_binary_for_lua(lua_State *L)
+ {
+ const char *path = lua_tostring(L, -1);
+ N(path, F("guess_binary called with an invalid parameter"));
+ lua_pushboolean(L, guess_binary(std::string(path, lua_strlen(L, -1))));
+ return 1;
+ }
+
+ static int
+ monotone_include_for_lua(lua_State *L)
+ {
+ const char *path = lua_tostring(L, -1);
+ N(path, F("Include called with an invalid parameter"));
+
+ bool res =Lua(L)
+ .loadfile(std::string(path, lua_strlen(L, -1)))
+ .call(0,1)
+ .ok();
+
+ lua_pushboolean(L, res);
+ return 1;
+ }
+
+ static int
+ monotone_includedir_for_lua(lua_State *L)
+ {
+ const char *pathstr = lua_tostring(L, -1);
+ N(pathstr, F("IncludeDir called with an invalid parameter"));
+
+ fs::path locpath(pathstr);
+ N(fs::exists(locpath), F("Directory '%s' does not exists") % pathstr);
+ N(fs::is_directory(locpath), F("'%s' is not a directory") % pathstr);
+
+ // directory, iterate over it, skipping subdirs, taking every filename,
+ // sorting them and loading in sorted order
+ fs::directory_iterator it(locpath);
+ std::vector<fs::path> arr;
+ while (it != fs::directory_iterator())
+ {
+ if (!fs::is_directory(*it))
+ arr.push_back(*it);
+ ++it;
+ }
+ std::sort(arr.begin(), arr.end());
+ for (std::vector<fs::path>::iterator i= arr.begin(); i != arr.end(); ++i)
+ {
+ bool res =Lua(L)
+ .loadfile(i->string())
+ .call(0,1)
+ .ok();
+ N(res, F("lua error while loading rcfile '%s'") % i->string());
+ }
+
+ lua_pushboolean(L, true);
+ return 1;
+ }
+}
+
+
+lua_hooks::lua_hooks()
+{
+ st = lua_open ();
+ I(st);
+
+ // no atpanic support in 4.x
+ // lua_atpanic (st, &panic_thrower);
+
+ luaopen_base(st);
+ luaopen_io(st);
+ luaopen_string(st);
+ luaopen_math(st);
+ luaopen_table(st);
+ luaopen_debug(st);
+
+ // add monotone-specific functions
+ lua_register(st, "mkstemp", monotone_mkstemp_for_lua);
+ lua_register(st, "existsonpath", monotone_existsonpath_for_lua);
+ lua_register(st, "is_executable", monotone_is_executable_for_lua);
+ lua_register(st, "make_executable", monotone_make_executable_for_lua);
+ lua_register(st, "spawn", monotone_spawn_for_lua);
+ lua_register(st, "wait", monotone_wait_for_lua);
+ lua_register(st, "kill", monotone_kill_for_lua);
+ lua_register(st, "sleep", monotone_sleep_for_lua);
+ lua_register(st, "guess_binary", monotone_guess_binary_for_lua);
+ lua_register(st, "include", monotone_include_for_lua);
+ lua_register(st, "includedir", monotone_includedir_for_lua);
+}
+
+lua_hooks::~lua_hooks()
+{
+ if (st)
+ lua_close (st);
+}
+
static bool
run_string(lua_State * st, string const &str, string const & identity)
{
@@ -591,6 +645,29 @@
lua_hooks::load_rcfile(utf8 const & rc)
{
I(st);
+ if (rc() != "-")
+ {
+ fs::path locpath(localized(rc));
+ if (fs::exists(locpath) && fs::is_directory(locpath))
+ {
+ // directory, iterate over it, skipping subdirs, taking every
filename,
+ // sorting them and loading in sorted order
+ fs::directory_iterator it(locpath);
+ std::vector<fs::path> arr;
+ while (it != fs::directory_iterator())
+ {
+ if (!fs::is_directory(*it))
+ arr.push_back(*it);
+ ++it;
+ }
+ std::sort(arr.begin(), arr.end());
+ for (std::vector<fs::path>::iterator i= arr.begin(); i != arr.end();
++i)
+ {
+ load_rcfile(*i, true);
+ }
+ return; // directory read, skip the rest ...
+ }
+ }
data dat;
L(F("opening rcfile '%s' ...\n") % rc);
read_data_for_command_line(rc, dat);
--- tests/t_lua_includedir.at
+++ tests/t_lua_includedir.at
@@ -0,0 +1,46 @@
+AT_SETUP([include() and includedir() lua functions])
+MONOTONE_SETUP
+
+AT_CHECK(mkdir gongolo)
+
+AT_DATA(include.lua, [include("../gongolo/aaa.rc")
+])
+
+AT_DATA(includedir.lua, [includedir("../gongolo")
+])
+
+# write two files and check that they will be invoked in alphabetic order
+AT_DATA(gongolo/aaa.rc, [function paraponzi()
+ io.write("BOOGA BOOGA")
+end
+paraponzi()
+])
+AT_DATA(gongolo/bbb.zz, [function labellagigogin()
+ io.write("CICCA CICCA")
+end
+labellagigogin()
+])
+
+# setup a wrk dir
+AT_CHECK(MONOTONE setup alt_wrk, [], [ignore], [ignore])
+
+# include directly a single file
+AT_CHECK(cd alt_wrk && MONOTONE --root=. --rcfile=../include.lua status, [],
[stdout], [ignore])
+AT_CHECK(grep -q "BOOGA BOOGA" stdout)
+
+# include dirs
+AT_CHECK(cd alt_wrk && MONOTONE --root=. --rcfile=../includedir.lua status,
[], [stdout], [ignore])
+AT_CHECK(grep -q "BOOGA BOOGACICCA CICCA" stdout)
+
+# write a third file: should be read beetween the two previous ones
+AT_DATA(gongolo/aba.rc, [function notwowithoutthree()
+ io.write("hu hu")
+end
+notwowithoutthree()
+])
+AT_CHECK(cd alt_wrk && MONOTONE --root=. --rcfile=../includedir.lua status,
[], [stdout], [ignore])
+AT_CHECK(grep -q "BOOGA BOOGAhu huCICCA CICCA" stdout)
+
+
+
+AT_CLEANUP
--- tests/t_rcfile_dir.at
+++ tests/t_rcfile_dir.at
@@ -0,0 +1,32 @@
+AT_SETUP([--rcfile=directory])
+MONOTONE_SETUP
+
+AT_CHECK(mkdir gongolo)
+
+# write two files and check that they will be invoked in alphabetic order
+AT_DATA(gongolo/aaa.rc, [function paraponzi()
+ io.write("BOOGA BOOGA")
+end
+paraponzi()
+])
+AT_DATA(gongolo/bbb.rc, [function labellagigogin()
+ io.write("CICCA CICCA")
+end
+labellagigogin()
+])
+
+# note: rcfile is placed outside workdir
+AT_CHECK(MONOTONE setup alt_wrk, [], [ignore], [ignore])
+AT_CHECK(cd alt_wrk && MONOTONE --root=. --rcfile=../gongolo status, [],
[stdout], [ignore])
+AT_CHECK(grep -q "BOOGA BOOGACICCA CICCA" stdout)
+
+# write a third file: should be read beetween the two previous ones
+AT_DATA(gongolo/aba.rc, [function notwowithoutthree()
+ io.write("hu hu")
+end
+notwowithoutthree()
+])
+AT_CHECK(cd alt_wrk && MONOTONE --root=. --rcfile=../gongolo status, [],
[stdout], [ignore])
+AT_CHECK(grep -q "BOOGA BOOGAhu huCICCA CICCA" stdout)
+
+AT_CLEANUP
--- testsuite.at
+++ testsuite.at
@@ -658,3 +658,5 @@
m4_include(tests/t_restricted_commit_with_inodeprints.at)
m4_include(tests/t_merge_manual.at)
m4_include(tests/t_revert_restrict.at)
+m4_include(tests/t_rcfile_dir.at)
+m4_include(tests/t_lua_includedir.at)
- [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory,
rghetta <=
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, Derek Scherger, 2005/06/11
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, rghetta, 2005/06/11
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, Richard Levitte - VMS Whacker, 2005/06/12
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, rghetta, 2005/06/13
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, Nuno Lucas, 2005/06/13
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, rghetta, 2005/06/13
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, Nuno Lucas, 2005/06/14
- Re: [Monotone-devel] [PATCH] Include()/Includedir() lua functions, --rcfile=directory, Richard Levitte - VMS Whacker, 2005/06/13