ruby-changes:14284
From: nobu <ko1@a...>
Date: Wed, 16 Dec 2009 15:05:59 +0900 (JST)
Subject: [ruby-changes:14284] Ruby:r26109 (ruby_1_8): * lib/un.rb (wait_writable): wait until target files can be
nobu 2009-12-16 15:05:48 +0900 (Wed, 16 Dec 2009) New Revision: 26109 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26109 Log: * lib/un.rb (wait_writable): wait until target files can be written actually. * lib/un.rb (mkmf): new command to create makefile. * lib/un.rb (rmdir): added -p option. * lib/un.rb (httpd): easy WEBrick HTTP server. Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/lib/un.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 26108) +++ ruby_1_8/ChangeLog (revision 26109) @@ -1,3 +1,14 @@ +Wed Dec 16 15:05:42 2009 Nobuyoshi Nakada <nobu@r...> + + * lib/un.rb (wait_writable): wait until target files can be + written actually. + + * lib/un.rb (mkmf): new command to create makefile. + + * lib/un.rb (rmdir): added -p option. + + * lib/un.rb (httpd): easy WEBrick HTTP server. + Tue Dec 15 11:48:41 2009 NAKAMURA Usaku <usa@r...> * string.c (rb_str_inspect): wrong result of UTF-8 inspect because of Index: ruby_1_8/lib/un.rb =================================================================== --- ruby_1_8/lib/un.rb (revision 26108) +++ ruby_1_8/lib/un.rb (revision 26109) @@ -1,11 +1,11 @@ -# +# # = un.rb -# +# # Copyright (c) 2003 WATANABE Hirofumi <eban@r...> -# +# # This program is free software. # You can distribute/modify this program under the same terms of Ruby. -# +# # == Utilities to replace common UNIX commands in Makefiles etc # # == SYNOPSIS @@ -19,8 +19,10 @@ # ruby -run -e install -- [OPTION] SOURCE DEST # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE # ruby -run -e touch -- [OPTION] FILE +# ruby -run -e wait_writable -- [OPTION] FILE +# ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION] +# ruby -run -e httpd -- [OPTION] DocumentRoot # ruby -run -e help [COMMAND] -# ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION] require "fileutils" require "optparse" @@ -42,7 +44,7 @@ end long_options.each do |s| opt_name = s[/\A(?:--)?([^\s=]+)/, 1].intern - o.on(s.sub(/\A(?!--)/, '--')) do |val| + o.on(s.gsub(/([a-z])([A-Z])/){$1+"-"+$2.downcase}.sub(/\A(?!--)/, '--')) do |val| opt_hash[opt_name] = val end end @@ -157,11 +159,13 @@ # # ruby -run -e rmdir -- [OPTION] DIR # +# -p remove DIRECTORY and its ancestors. # -v verbose # def rmdir - setup do |argv, options| + setup("p") do |argv, options| + options[:parents] = true if options.delete :p FileUtils.rmdir argv, options end end @@ -217,6 +221,37 @@ end ## +# Wait until the file becomes writable. +# +# ruby -run -e wait_writable -- [OPTION] FILE +# +# -n RETRY count to retry +# -w SEC each wait time in seconds +# -v verbose +# + +def wait_writable + setup("n:w:v") do |argv, options| + verbose = options[:verbose] + n = options[:n] and n = Integer(n) + wait = (wait = options[:w]) ? Float(wait) : 0.2 + argv.each do |file| + begin + open(file, "r+b") + rescue Errno::ENOENT + break + rescue Errno::EACCES => e + raise if n and (n -= 1) <= 0 + puts e + STDOUT.flush + sleep wait + retry + end + end + end +end + +## # Create makefile using mkmf. # # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION] @@ -249,6 +284,42 @@ end ## +# Run WEBrick HTTP server. +# +# ruby -run -e httpd -- [OPTION] DocumentRoot +# +# --bind-address=ADDR address to bind +# --port=NUM listening port number +# --max-clients=MAX max number of simultaneous clients +# --temp-dir=DIR temporary directory +# --do-not-reverse-lookup disable reverse lookup +# --request-timeout=SECOND request timeout in seconds +# --http-version=VERSION HTTP version +# -v verbose +# + +def httpd + setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR", + "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do + |argv, options| + require 'webrick' + opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i + unless argv.empty? + options[:DocumentRoot] = argv.shift + end + s = WEBrick::HTTPServer.new(options) + shut = proc {s.shutdown} + Signal.trap("TERM", shut) + Signal.trap("QUIT", shut) + if STDIN.tty? + Signal.trap("HUP", shut) + Signal.trap("INT", shut) + end + s.start + end +end + +## # Display help message. # # ruby -run -e help [COMMAND] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/