Top Level Namespace
Defined Under Namespace
Modules: Ult
Instance Method Summary collapse
- #assert(condition, msg, error_type = RuntimeError) ⇒ Object
- #copy(src, dst) ⇒ Object
- #dir(path, &block) ⇒ Object
- #dir?(path) ⇒ Boolean
- #direach(path) ⇒ Object
- #dirname(path) ⇒ Object
- #execute(command, input = "", &block) ⇒ Object
- #extname(path) ⇒ Object
- #file?(path) ⇒ Boolean
- #filename(path) ⇒ Object
- #find(pattern) ⇒ Object
- #fullpath(name, dir = ".") ⇒ Object
- #linux? ⇒ Boolean
- #mac? ⇒ Boolean
- #mkdir(path, &block) ⇒ Object
- #mkfile(path, mode, &block) ⇒ Object
- #option(*args, &block) ⇒ Object
- #platform ⇒ Object
- #pwd ⇒ Object
- #remove(path) ⇒ Object
- #replica(count, prefix, suffix = "") ⇒ Object
- #rmdir(path) ⇒ Object
- #rmfile(path) ⇒ Object
- #rmkdir(path) ⇒ Object
- #safety(&block) ⇒ Object
- #shell(command, &block) ⇒ Object
- #watchdog(command, input = "", &block) ⇒ Object
- #windows? ⇒ Boolean
Instance Method Details
#assert(condition, msg, error_type = RuntimeError) ⇒ Object
1 2 3 |
# File 'lib/ult/assert.rb', line 1 def assert( condition, msg, error_type = RuntimeError ) raise( error_type, msg, caller( 1 ) ) if ! condition end |
#copy(src, dst) ⇒ Object
4 5 6 |
# File 'lib/ult/copy.rb', line 4 def copy( src, dst ) dir?( src ) ? FileUtils.cp_r( src, dst ) : FileUtils.copy( src, dst ) end |
#dir(path, &block) ⇒ Object
22 23 24 |
# File 'lib/ult/dir.rb', line 22 def dir( path, &block ) Dir.chdir( path, &block ) end |
#dir?(path) ⇒ Boolean
4 5 6 |
# File 'lib/ult/dir.rb', line 4 def dir?( path ) Dir.exist?( path ) end |
#direach(path) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/ult/dir.rb', line 12 def direach( path ) Dir::foreach( path ){|name| case name when ".", ".." else yield( name, fullpath( name, path ) ) end } end |
#dirname(path) ⇒ Object
8 9 10 |
# File 'lib/ult/dir.rb', line 8 def dirname( path ) File.dirname( path ) end |
#execute(command, input = "", &block) ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ult/execute.rb', line 1 def execute( command, input = "", &block ) status = -1 block = lambda{|type, io, msg| io.print msg} if ! block_given? begin i_r, i_w = IO.pipe o_r, o_w = IO.pipe e_r, e_w = IO.pipe pid = Process.fork{ i_w.close STDIN.reopen( i_r ) STDOUT.reopen( o_w ) STDERR.reopen( e_w ) exec( command ) } o_w.close e_w.close i_r.close i_w.write input i_w.close # IOブロックによるプロセス停止を防ぐため、スレッドで定期的に読み取る block.call( :cmd, $stdout, "#{command}\n" ) out_thread = Thread.start{ chars = [] o_r.each_char{|c| chars.push c case c when /[\r\n]/ block.call( :out, $stdout, chars.join ) chars = [] end } block.call( :out, $stdout, chars.join ) if ! chars.empty? } err_thread = Thread.start{ chars = [] e_r.each_char{|c| chars.push c case c when /[\r\n]/ block.call( :err, $stderr, chars.join ) chars = [] end } block.call( :err, $stderr, chars.join ) if ! chars.empty? } Process.waitpid( pid ) status = $?.exitstatus out_thread.join err_thread.join end status end |
#extname(path) ⇒ Object
9 10 11 |
# File 'lib/ult/file.rb', line 9 def extname( path ) /^\.?.+?\.(.+)$/ =~ path ? $1 : "" end |
#file?(path) ⇒ Boolean
1 2 3 |
# File 'lib/ult/file.rb', line 1 def file?( path ) File.exist?( path ) end |
#filename(path) ⇒ Object
5 6 7 |
# File 'lib/ult/file.rb', line 5 def filename( path ) File.basename( path ) end |
#find(pattern) ⇒ Object
1 2 3 |
# File 'lib/ult/find.rb', line 1 def find( pattern ) Dir.glob( pattern ) end |
#fullpath(name, dir = ".") ⇒ Object
1 2 3 |
# File 'lib/ult/path.rb', line 1 def fullpath( name, dir = "." ) File.( name, dir ) end |
#linux? ⇒ Boolean
9 10 11 |
# File 'lib/ult/platform.rb', line 9 def linux? ! platform.match( /linux/ ).nil? end |
#mac? ⇒ Boolean
5 6 7 |
# File 'lib/ult/platform.rb', line 5 def mac? ! platform.match( /darwin/ ).nil? end |
#mkdir(path, &block) ⇒ Object
30 31 32 33 |
# File 'lib/ult/dir.rb', line 30 def mkdir( path, &block ) FileUtils.mkdir_p( path ) if ! dir?( path ) dir( path, &block ) end |
#mkfile(path, mode, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/ult/file.rb', line 13 def mkfile( path, mode, &block ) open( path, mode ){|file| block.call( file ) if block_given? } end |
#option(*args, &block) ⇒ Object
3 4 5 |
# File 'lib/ult/option.rb', line 3 def option( *args, &block ) OptionParser.new( *args ).instance_eval( &block ) end |
#platform ⇒ Object
1 2 3 |
# File 'lib/ult/platform.rb', line 1 def platform RUBY_PLATFORM end |
#pwd ⇒ Object
26 27 28 |
# File 'lib/ult/dir.rb', line 26 def pwd Dir.pwd end |
#remove(path) ⇒ Object
4 5 6 |
# File 'lib/ult/remove.rb', line 4 def remove( path ) dir?( path ) ? rmdir( path ) : rmfile( path ) end |
#replica(count, prefix, suffix = "") ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ult/replica.rb', line 6 def replica( count, prefix, suffix = "" ) src = "#{prefix}#{suffix}" if dir?( src ) count.times{|index| dst = "#{prefix}#{index + 1}#{suffix}" remove( dst ) copy( src, dst ) } elsif file?( src ) count.times{|index| dst = "#{prefix}#{index + 1}#{suffix}" remove( dst ) copy( src, dst ) } end end |
#rmdir(path) ⇒ Object
35 36 37 |
# File 'lib/ult/dir.rb', line 35 def rmdir( path ) FileUtils.rm_rf( path ) if dir?( path ) end |
#rmfile(path) ⇒ Object
19 20 21 |
# File 'lib/ult/file.rb', line 19 def rmfile( path ) File.delete( path ) if file?( path ) end |
#rmkdir(path) ⇒ Object
39 40 41 42 |
# File 'lib/ult/dir.rb', line 39 def rmkdir( path ) rmdir( path ) mkdir( path ) end |
#safety(&block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/ult/safety.rb', line 3 def safety( &block ) root_dir = pwd begin block.call rescue Exception => e dir( root_dir ) raise e rescue StandardError => e dir( root_dir ) raise e end dir( root_dir ) end |
#shell(command, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/ult/execute.rb', line 57 def shell( command, &block ) status = execute( command ) if block_given? status = block.call( status ) else raise "#{command} => #{status}" if 0 != status end status end |
#watchdog(command, input = "", &block) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/ult/watchdog.rb', line 3 def watchdog( command, input = "", &block ) loop{ status, outputs, errors = execute( command, input ) break if ! block.call( status, outputs, errors ) } end |