Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 343 lines (313 sloc) 8.037 kb
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
1 #
511dc44 initial import
Laurent Sansonetti authored
2 # = un.rb
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
3 #
511dc44 initial import
Laurent Sansonetti authored
4 # Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
5 #
511dc44 initial import
Laurent Sansonetti authored
6 # This program is free software.
7 # You can distribute/modify this program under the same terms of Ruby.
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
8 #
511dc44 initial import
Laurent Sansonetti authored
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
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
22 # ruby -run -e wait_writable -- [OPTION] FILE
23 # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
24 # ruby -run -e httpd -- [OPTION] DocumentRoot
511dc44 initial import
Laurent Sansonetti authored
25 # ruby -run -e help [COMMAND]
26
27 require "fileutils"
28 require "optparse"
29
30 module FileUtils
31 # @fileutils_label = ""
32 @fileutils_output = $stdout
33 end
34
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
35 def setup(options = "", *long_options)
511dc44 initial import
Laurent Sansonetti authored
36 opt_hash = {}
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
37 argv = []
511dc44 initial import
Laurent Sansonetti authored
38 OptionParser.new do |o|
39 options.scan(/.:?/) do |s|
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
40 opt_name = s.delete(":").intern
511dc44 initial import
Laurent Sansonetti authored
41 o.on("-" + s.tr(":", " ")) do |val|
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
42 opt_hash[opt_name] = val
43 end
44 end
45 long_options.each do |s|
46 opt_name = s[/\A(?:--)?([^\s=]+)/, 1].intern
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
47 o.on(s.gsub(/([a-z])([A-Z])/){$1+"-"+$2.downcase}.sub(/\A(?!--)/, '--')) do |val|
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
48 opt_hash[opt_name] = val
511dc44 initial import
Laurent Sansonetti authored
49 end
50 end
51 o.on("-v") do opt_hash[:verbose] = true end
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
52 o.order!(ARGV) do |x|
53 if /[*?\[{]/ =~ x
54 argv.concat(Dir[x])
55 else
56 argv << x
57 end
58 end
511dc44 initial import
Laurent Sansonetti authored
59 end
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
60 yield argv, opt_hash
511dc44 initial import
Laurent Sansonetti authored
61 end
62
63 ##
64 # Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
65 #
66 # ruby -run -e cp -- [OPTION] SOURCE DEST
67 #
68 # -p preserve file attributes if possible
69 # -r copy recursively
70 # -v verbose
71 #
72
73 def cp
74 setup("pr") do |argv, options|
75 cmd = "cp"
76 cmd += "_r" if options.delete :r
77 options[:preserve] = true if options.delete :p
78 dest = argv.pop
79 argv = argv[0] if argv.size == 1
80 FileUtils.send cmd, argv, dest, options
81 end
82 end
83
84 ##
85 # Create a link to the specified TARGET with LINK_NAME.
86 #
87 # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
88 #
89 # -s make symbolic links instead of hard links
90 # -f remove existing destination files
91 # -v verbose
92 #
93
94 def ln
95 setup("sf") do |argv, options|
96 cmd = "ln"
97 cmd += "_s" if options.delete :s
98 options[:force] = true if options.delete :f
99 dest = argv.pop
100 argv = argv[0] if argv.size == 1
101 FileUtils.send cmd, argv, dest, options
102 end
103 end
104
105 ##
106 # Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
107 #
108 # ruby -run -e mv -- [OPTION] SOURCE DEST
109 #
110 # -v verbose
111 #
112
113 def mv
114 setup do |argv, options|
115 dest = argv.pop
116 argv = argv[0] if argv.size == 1
117 FileUtils.mv argv, dest, options
118 end
119 end
120
121 ##
122 # Remove the FILE
123 #
124 # ruby -run -e rm -- [OPTION] FILE
125 #
126 # -f ignore nonexistent files
127 # -r remove the contents of directories recursively
128 # -v verbose
129 #
130
131 def rm
132 setup("fr") do |argv, options|
133 cmd = "rm"
134 cmd += "_r" if options.delete :r
135 options[:force] = true if options.delete :f
136 FileUtils.send cmd, argv, options
137 end
138 end
139
140 ##
141 # Create the DIR, if they do not already exist.
142 #
143 # ruby -run -e mkdir -- [OPTION] DIR
144 #
145 # -p no error if existing, make parent directories as needed
146 # -v verbose
147 #
148
149 def mkdir
150 setup("p") do |argv, options|
151 cmd = "mkdir"
152 cmd += "_p" if options.delete :p
153 FileUtils.send cmd, argv, options
154 end
155 end
156
157 ##
158 # Remove the DIR.
159 #
160 # ruby -run -e rmdir -- [OPTION] DIR
161 #
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
162 # -p remove DIRECTORY and its ancestors.
511dc44 initial import
Laurent Sansonetti authored
163 # -v verbose
164 #
165
166 def rmdir
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
167 setup("p") do |argv, options|
168 options[:parents] = true if options.delete :p
511dc44 initial import
Laurent Sansonetti authored
169 FileUtils.rmdir argv, options
170 end
171 end
172
173 ##
174 # Copy SOURCE to DEST.
175 #
176 # ruby -run -e install -- [OPTION] SOURCE DEST
177 #
178 # -p apply access/modification times of SOURCE files to
179 # corresponding destination files
180 # -m set permission mode (as in chmod), instead of 0755
181 # -v verbose
182 #
183
184 def install
185 setup("pm:") do |argv, options|
186 options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
187 options[:preserve] = true if options.delete :p
188 dest = argv.pop
189 argv = argv[0] if argv.size == 1
190 FileUtils.install argv, dest, options
191 end
192 end
193
194 ##
195 # Change the mode of each FILE to OCTAL-MODE.
196 #
197 # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
198 #
199 # -v verbose
200 #
201
202 def chmod
203 setup do |argv, options|
204 mode = argv.shift.oct
205 FileUtils.chmod mode, argv, options
206 end
207 end
208
209 ##
210 # Update the access and modification times of each FILE to the current time.
211 #
212 # ruby -run -e touch -- [OPTION] FILE
213 #
214 # -v verbose
215 #
216
217 def touch
218 setup do |argv, options|
219 FileUtils.touch argv, options
220 end
221 end
222
223 ##
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
224 # Wait until the file becomes writable.
225 #
226 # ruby -run -e wait_writable -- [OPTION] FILE
227 #
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
228 # -n RETRY count to retry
229 # -w SEC each wait time in seconds
230 # -v verbose
231 #
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
232
233 def wait_writable
234 setup("n:w:v") do |argv, options|
235 verbose = options[:verbose]
236 n = options[:n] and n = Integer(n)
237 wait = (wait = options[:w]) ? Float(wait) : 0.2
238 argv.each do |file|
239 begin
240 open(file, "r+b")
241 rescue Errno::ENOENT
242 break
243 rescue Errno::EACCES => e
244 raise if n and (n -= 1) <= 0
245 puts e
246 STDOUT.flush
247 sleep wait
248 retry
249 end
250 end
251 end
252 end
253
254 ##
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
255 # Create makefile using mkmf.
256 #
257 # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
258 #
259 # -d ARGS run dir_config
260 # -h ARGS run have_header
261 # -l ARGS run have_library
262 # -f ARGS run have_func
263 # -v ARGS run have_var
264 # -t ARGS run have_type
265 # -m ARGS run have_macro
266 # -c ARGS run have_const
267 # --vendor install to vendor_ruby
268 #
269
270 def mkmf
271 setup("d:h:l:f:v:t:m:c:", "vendor") do |argv, options|
272 require 'mkmf'
273 opt = options[:d] and opt.split(/:/).each {|n| dir_config(*n.split(/,/))}
274 opt = options[:h] and opt.split(/:/).each {|n| have_header(*n.split(/,/))}
275 opt = options[:l] and opt.split(/:/).each {|n| have_library(*n.split(/,/))}
276 opt = options[:f] and opt.split(/:/).each {|n| have_func(*n.split(/,/))}
277 opt = options[:v] and opt.split(/:/).each {|n| have_var(*n.split(/,/))}
278 opt = options[:t] and opt.split(/:/).each {|n| have_type(*n.split(/,/))}
279 opt = options[:m] and opt.split(/:/).each {|n| have_macro(*n.split(/,/))}
280 opt = options[:c] and opt.split(/:/).each {|n| have_const(*n.split(/,/))}
281 $configure_args["--vendor"] = true if options[:vendor]
282 create_makefile(*argv)
283 end
284 end
285
286 ##
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
287 # Run WEBrick HTTP server.
288 #
289 # ruby -run -e httpd -- [OPTION] DocumentRoot
290 #
291 # --bind-address=ADDR address to bind
292 # --port=NUM listening port number
293 # --max-clients=MAX max number of simultaneous clients
294 # --temp-dir=DIR temporary directory
295 # --do-not-reverse-lookup disable reverse lookup
296 # --request-timeout=SECOND request timeout in seconds
297 # --http-version=VERSION HTTP version
298 # -v verbose
299 #
300
301 def httpd
302 setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR",
303 "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do
304 |argv, options|
305 require 'webrick'
306 opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i
307 opt = options[:Port] and (options[:Port] = Integer(opt)) rescue nil
308 unless argv.empty?
309 options[:DocumentRoot] = argv.shift
310 end
311 s = WEBrick::HTTPServer.new(options)
312 shut = proc {s.shutdown}
313 Signal.trap("TERM", shut)
314 Signal.trap("QUIT", shut)
315 if STDIN.tty?
316 Signal.trap("HUP", shut)
317 Signal.trap("INT", shut)
318 end
319 s.start
320 end
321 end
322
323 ##
511dc44 initial import
Laurent Sansonetti authored
324 # Display help message.
325 #
326 # ruby -run -e help [COMMAND]
327 #
328
329 def help
330 setup do |argv,|
331 all = argv.empty?
332 open(__FILE__) do |me|
333 while me.gets("##\n")
334 if help = me.gets("\n\n")
335 if all or argv.delete help[/-e \w+/].sub(/-e /, "")
336 print help.gsub(/^# ?/, "")
337 end
338 end
339 end
340 end
341 end
342 end
Something went wrong with that request. Please try again.