/tools/Ruby/lib/ruby/1.8/un.rb

http://github.com/agross/netopenspace · Ruby · 235 lines · 111 code · 23 blank · 101 comment · 20 complexity · 2116598676ebfad9beec71e2178eff12 MD5 · raw file

  1. #
  2. # = un.rb
  3. #
  4. # Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
  5. #
  6. # This program is free software.
  7. # You can distribute/modify this program under the same terms of Ruby.
  8. #
  9. # == Utilities to replace common UNIX commands in Makefiles etc
  10. #
  11. # == SYNOPSIS
  12. #
  13. # ruby -run -e cp -- [OPTION] SOURCE DEST
  14. # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
  15. # ruby -run -e mv -- [OPTION] SOURCE DEST
  16. # ruby -run -e rm -- [OPTION] FILE
  17. # ruby -run -e mkdir -- [OPTION] DIRS
  18. # ruby -run -e rmdir -- [OPTION] DIRS
  19. # ruby -run -e install -- [OPTION] SOURCE DEST
  20. # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
  21. # ruby -run -e touch -- [OPTION] FILE
  22. # ruby -run -e help [COMMAND]
  23. require "fileutils"
  24. require "optparse"
  25. module FileUtils
  26. # @fileutils_label = ""
  27. @fileutils_output = $stdout
  28. end
  29. def setup(options = "")
  30. ARGV.map! do |x|
  31. case x
  32. when /^-/
  33. x.delete "^-#{options}v"
  34. when /[*?\[{]/
  35. Dir[x]
  36. else
  37. x
  38. end
  39. end
  40. ARGV.flatten!
  41. ARGV.delete_if{|x| x == "-"}
  42. opt_hash = {}
  43. OptionParser.new do |o|
  44. options.scan(/.:?/) do |s|
  45. o.on("-" + s.tr(":", " ")) do |val|
  46. opt_hash[s.delete(":").intern] = val
  47. end
  48. end
  49. o.on("-v") do opt_hash[:verbose] = true end
  50. o.parse!
  51. end
  52. yield ARGV, opt_hash
  53. end
  54. ##
  55. # Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
  56. #
  57. # ruby -run -e cp -- [OPTION] SOURCE DEST
  58. #
  59. # -p preserve file attributes if possible
  60. # -r copy recursively
  61. # -v verbose
  62. #
  63. def cp
  64. setup("pr") do |argv, options|
  65. cmd = "cp"
  66. cmd += "_r" if options.delete :r
  67. options[:preserve] = true if options.delete :p
  68. dest = argv.pop
  69. argv = argv[0] if argv.size == 1
  70. FileUtils.send cmd, argv, dest, options
  71. end
  72. end
  73. ##
  74. # Create a link to the specified TARGET with LINK_NAME.
  75. #
  76. # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
  77. #
  78. # -s make symbolic links instead of hard links
  79. # -f remove existing destination files
  80. # -v verbose
  81. #
  82. def ln
  83. setup("sf") do |argv, options|
  84. cmd = "ln"
  85. cmd += "_s" if options.delete :s
  86. options[:force] = true if options.delete :f
  87. dest = argv.pop
  88. argv = argv[0] if argv.size == 1
  89. FileUtils.send cmd, argv, dest, options
  90. end
  91. end
  92. ##
  93. # Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
  94. #
  95. # ruby -run -e mv -- [OPTION] SOURCE DEST
  96. #
  97. # -v verbose
  98. #
  99. def mv
  100. setup do |argv, options|
  101. dest = argv.pop
  102. argv = argv[0] if argv.size == 1
  103. FileUtils.mv argv, dest, options
  104. end
  105. end
  106. ##
  107. # Remove the FILE
  108. #
  109. # ruby -run -e rm -- [OPTION] FILE
  110. #
  111. # -f ignore nonexistent files
  112. # -r remove the contents of directories recursively
  113. # -v verbose
  114. #
  115. def rm
  116. setup("fr") do |argv, options|
  117. cmd = "rm"
  118. cmd += "_r" if options.delete :r
  119. options[:force] = true if options.delete :f
  120. FileUtils.send cmd, argv, options
  121. end
  122. end
  123. ##
  124. # Create the DIR, if they do not already exist.
  125. #
  126. # ruby -run -e mkdir -- [OPTION] DIR
  127. #
  128. # -p no error if existing, make parent directories as needed
  129. # -v verbose
  130. #
  131. def mkdir
  132. setup("p") do |argv, options|
  133. cmd = "mkdir"
  134. cmd += "_p" if options.delete :p
  135. FileUtils.send cmd, argv, options
  136. end
  137. end
  138. ##
  139. # Remove the DIR.
  140. #
  141. # ruby -run -e rmdir -- [OPTION] DIR
  142. #
  143. # -v verbose
  144. #
  145. def rmdir
  146. setup do |argv, options|
  147. FileUtils.rmdir argv, options
  148. end
  149. end
  150. ##
  151. # Copy SOURCE to DEST.
  152. #
  153. # ruby -run -e install -- [OPTION] SOURCE DEST
  154. #
  155. # -p apply access/modification times of SOURCE files to
  156. # corresponding destination files
  157. # -m set permission mode (as in chmod), instead of 0755
  158. # -v verbose
  159. #
  160. def install
  161. setup("pm:") do |argv, options|
  162. options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
  163. options[:preserve] = true if options.delete :p
  164. dest = argv.pop
  165. argv = argv[0] if argv.size == 1
  166. FileUtils.install argv, dest, options
  167. end
  168. end
  169. ##
  170. # Change the mode of each FILE to OCTAL-MODE.
  171. #
  172. # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
  173. #
  174. # -v verbose
  175. #
  176. def chmod
  177. setup do |argv, options|
  178. mode = argv.shift.oct
  179. FileUtils.chmod mode, argv, options
  180. end
  181. end
  182. ##
  183. # Update the access and modification times of each FILE to the current time.
  184. #
  185. # ruby -run -e touch -- [OPTION] FILE
  186. #
  187. # -v verbose
  188. #
  189. def touch
  190. setup do |argv, options|
  191. FileUtils.touch argv, options
  192. end
  193. end
  194. ##
  195. # Display help message.
  196. #
  197. # ruby -run -e help [COMMAND]
  198. #
  199. def help
  200. setup do |argv,|
  201. all = argv.empty?
  202. open(__FILE__) do |me|
  203. while me.gets("##\n")
  204. if help = me.gets("\n\n")
  205. if all or argv.delete help[/-e \w+/].sub(/-e /, "")
  206. print help.gsub(/^# ?/, "")
  207. end
  208. end
  209. end
  210. end
  211. end
  212. end