Hi all,
for the TeX Live uninstaller I am trying to write a rmdir function for
win32 in texlua which should work like rm -rf on unix.
My first try was:
function rmdir(dirn)
if os.type == 'windows' then
-- we have to replace all / with \
foo = string.gsub(dirn, '/', '\\')
ret = os.spawn({"rmdir", "/s", "/q", foo})
else
ret = os.spawn({"rm", "-rf", dirn})
end
if ret then
return ret
else
io.stderr:write(filename..': removing '..dirn.." didn't work\n")
end
return ret
end
(No checks by now, will come later ...)
But that didn't work and did spit out always the io.stderr message.
Any suggestion how to do that? Or is there some code already somewhere
around in the ConTeXt project?
Thanks and all the best
Norbert
-------------------------------------------------------------------------------
Dr. Norbert Preining
Norbert Preining wrote:
Hi all,
for the TeX Live uninstaller I am trying to write a rmdir function for win32 in texlua which should work like rm -rf on unix.
My first try was: function rmdir(dirn) if os.type == 'windows' then -- we have to replace all / with \ foo = string.gsub(dirn, '/', '\\') ret = os.spawn({"rmdir", "/s", "/q", foo}) else ret = os.spawn({"rm", "-rf", dirn}) end if ret then return ret else io.stderr:write(filename..': removing '..dirn.." didn't work\n") end return ret end
(No checks by now, will come later ...)
But that didn't work and did spit out always the io.stderr message.
rmdir is a system command (1) use os.execute for system commands (2) in the case of gnutools (you can never be sure it they are installed, use the funny cygwin syntax or so) anyhow, this works: function rmdir(name) if os.type == 'windows' then os.execute("rmdir /s /q " .. name:gsub('/', '\\')) else os.spawn({"rm", "-rf", dirn}) end local ok = lfs.attributes(name) if ok then io.stderr:write(string.format("removing '%s' didn't work our well\n",name)) end return ok end concerning your code:
if os.type == 'windows' then foo = string.gsub(dirn, '/', '\\') ret = os.spawn({"rmdir", "/s", "/q", foo})
local ret if os.type == 'windows' then local foo = string.gsub(dirn, '/', '\\') ret = os.spawn({"rmdir", "/s", "/q", foo}) in order not to polute the global namespace ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On Do, 03 Apr 2008, Hans Hagen wrote:
anyhow, this works:
Thanks, also big thnaks for the lua crash course ;-) I will try to
remember using local everywhere ;-)
Later on I will come back with more questions, after testing on my
virtual machine exhibits more problems.
Best wishes
Norbert
-------------------------------------------------------------------------------
Dr. Norbert Preining
Hello,
Thanks, also big thnaks for the lua crash course ;-) I will try to remember using local everywhere ;-)
Once you start, it is impossible to stop; you want to make every variable local ;-)
That is actually possible. You simply have a module add its definitions not to the global table, but to a local one which is then returned: local M = { } M.foo = function(...) ... end ... return M The caller then does: local mymodule = require("mymodule") mymodule.foo(...) In TeX, you could do the same thing: \directlua0{require("mymodule").foo(...)} (of course this only works because require has a global variable storing the return value of all loaded modules. So some globals are still required.)
Arthur
Jonathan
participants (4)
-
Arthur Reutenauer
-
Hans Hagen
-
Jonathan Sauer
-
Norbert Preining