Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: shell.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# shell.rb -
[2] Fix | Delete
# $Release Version: 0.7 $
[3] Fix | Delete
# $Revision: 1.9 $
[4] Fix | Delete
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
[5] Fix | Delete
#
[6] Fix | Delete
# --
[7] Fix | Delete
#
[8] Fix | Delete
#
[9] Fix | Delete
#
[10] Fix | Delete
[11] Fix | Delete
require "e2mmap"
[12] Fix | Delete
[13] Fix | Delete
require "forwardable"
[14] Fix | Delete
[15] Fix | Delete
require "shell/error"
[16] Fix | Delete
require "shell/command-processor"
[17] Fix | Delete
require "shell/process-controller"
[18] Fix | Delete
[19] Fix | Delete
# Shell implements an idiomatic Ruby interface for common UNIX shell commands.
[20] Fix | Delete
#
[21] Fix | Delete
# It provides users the ability to execute commands with filters and pipes,
[22] Fix | Delete
# like +sh+/+csh+ by using native facilities of Ruby.
[23] Fix | Delete
#
[24] Fix | Delete
# == Examples
[25] Fix | Delete
#
[26] Fix | Delete
# === Temp file creation
[27] Fix | Delete
#
[28] Fix | Delete
# In this example we will create three +tmpFile+'s in three different folders
[29] Fix | Delete
# under the +/tmp+ directory.
[30] Fix | Delete
#
[31] Fix | Delete
# sh = Shell.cd("/tmp") # Change to the /tmp directory
[32] Fix | Delete
# sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
[33] Fix | Delete
# # make the 'shell-test-1' directory if it doesn't already exist
[34] Fix | Delete
# sh.cd("shell-test-1") # Change to the /tmp/shell-test-1 directory
[35] Fix | Delete
# for dir in ["dir1", "dir3", "dir5"]
[36] Fix | Delete
# if !sh.exists?(dir)
[37] Fix | Delete
# sh.mkdir dir # make dir if it doesn't already exist
[38] Fix | Delete
# sh.cd(dir) do
[39] Fix | Delete
# # change to the `dir` directory
[40] Fix | Delete
# f = sh.open("tmpFile", "w") # open a new file in write mode
[41] Fix | Delete
# f.print "TEST\n" # write to the file
[42] Fix | Delete
# f.close # close the file handler
[43] Fix | Delete
# end
[44] Fix | Delete
# print sh.pwd # output the process working directory
[45] Fix | Delete
# end
[46] Fix | Delete
# end
[47] Fix | Delete
#
[48] Fix | Delete
# === Temp file creation with self
[49] Fix | Delete
#
[50] Fix | Delete
# This example is identical to the first, except we're using
[51] Fix | Delete
# CommandProcessor#transact.
[52] Fix | Delete
#
[53] Fix | Delete
# CommandProcessor#transact executes the given block against self, in this case
[54] Fix | Delete
# +sh+; our Shell object. Within the block we can substitute +sh.cd+ to +cd+,
[55] Fix | Delete
# because the scope within the block uses +sh+ already.
[56] Fix | Delete
#
[57] Fix | Delete
# sh = Shell.cd("/tmp")
[58] Fix | Delete
# sh.transact do
[59] Fix | Delete
# mkdir "shell-test-1" unless exists?("shell-test-1")
[60] Fix | Delete
# cd("shell-test-1")
[61] Fix | Delete
# for dir in ["dir1", "dir3", "dir5"]
[62] Fix | Delete
# if !exists?(dir)
[63] Fix | Delete
# mkdir dir
[64] Fix | Delete
# cd(dir) do
[65] Fix | Delete
# f = open("tmpFile", "w")
[66] Fix | Delete
# f.print "TEST\n"
[67] Fix | Delete
# f.close
[68] Fix | Delete
# end
[69] Fix | Delete
# print pwd
[70] Fix | Delete
# end
[71] Fix | Delete
# end
[72] Fix | Delete
# end
[73] Fix | Delete
#
[74] Fix | Delete
# === Pipe /etc/printcap into a file
[75] Fix | Delete
#
[76] Fix | Delete
# In this example we will read the operating system file +/etc/printcap+,
[77] Fix | Delete
# generated by +cupsd+, and then output it to a new file relative to the +pwd+
[78] Fix | Delete
# of +sh+.
[79] Fix | Delete
#
[80] Fix | Delete
# sh = Shell.new
[81] Fix | Delete
# sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
[82] Fix | Delete
# (sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
[83] Fix | Delete
# sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
[84] Fix | Delete
# (sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
[85] Fix | Delete
#
[86] Fix | Delete
class Shell
[87] Fix | Delete
[88] Fix | Delete
include Error
[89] Fix | Delete
extend Exception2MessageMapper
[90] Fix | Delete
[91] Fix | Delete
# debug: true -> normal debug
[92] Fix | Delete
# debug: 1 -> eval definition debug
[93] Fix | Delete
# debug: 2 -> detail inspect debug
[94] Fix | Delete
@debug = false
[95] Fix | Delete
@verbose = true
[96] Fix | Delete
[97] Fix | Delete
@debug_display_process_id = false
[98] Fix | Delete
@debug_display_thread_id = true
[99] Fix | Delete
@debug_output_mutex = Thread::Mutex.new
[100] Fix | Delete
@default_system_path = nil
[101] Fix | Delete
@default_record_separator = nil
[102] Fix | Delete
[103] Fix | Delete
class << Shell
[104] Fix | Delete
extend Forwardable
[105] Fix | Delete
[106] Fix | Delete
attr_accessor :cascade, :verbose
[107] Fix | Delete
attr_reader :debug
[108] Fix | Delete
[109] Fix | Delete
alias debug? debug
[110] Fix | Delete
alias verbose? verbose
[111] Fix | Delete
@verbose = true
[112] Fix | Delete
[113] Fix | Delete
def debug=(val)
[114] Fix | Delete
@debug = val
[115] Fix | Delete
@verbose = val if val
[116] Fix | Delete
end
[117] Fix | Delete
[118] Fix | Delete
[119] Fix | Delete
# call-seq:
[120] Fix | Delete
# Shell.cd(path)
[121] Fix | Delete
#
[122] Fix | Delete
# Creates a new Shell instance with the current working directory
[123] Fix | Delete
# set to +path+.
[124] Fix | Delete
def cd(path)
[125] Fix | Delete
new(path)
[126] Fix | Delete
end
[127] Fix | Delete
[128] Fix | Delete
# Returns the directories in the current shell's PATH environment variable
[129] Fix | Delete
# as an array of directory names. This sets the system_path for all
[130] Fix | Delete
# instances of Shell.
[131] Fix | Delete
#
[132] Fix | Delete
# Example: If in your current shell, you did:
[133] Fix | Delete
#
[134] Fix | Delete
# $ echo $PATH
[135] Fix | Delete
# /usr/bin:/bin:/usr/local/bin
[136] Fix | Delete
#
[137] Fix | Delete
# Running this method in the above shell would then return:
[138] Fix | Delete
#
[139] Fix | Delete
# ["/usr/bin", "/bin", "/usr/local/bin"]
[140] Fix | Delete
#
[141] Fix | Delete
def default_system_path
[142] Fix | Delete
if @default_system_path
[143] Fix | Delete
@default_system_path
[144] Fix | Delete
else
[145] Fix | Delete
ENV["PATH"].split(":")
[146] Fix | Delete
end
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
# Sets the system_path that new instances of Shell should have as their
[150] Fix | Delete
# initial system_path.
[151] Fix | Delete
#
[152] Fix | Delete
# +path+ should be an array of directory name strings.
[153] Fix | Delete
def default_system_path=(path)
[154] Fix | Delete
@default_system_path = path
[155] Fix | Delete
end
[156] Fix | Delete
[157] Fix | Delete
def default_record_separator
[158] Fix | Delete
if @default_record_separator
[159] Fix | Delete
@default_record_separator
[160] Fix | Delete
else
[161] Fix | Delete
$/
[162] Fix | Delete
end
[163] Fix | Delete
end
[164] Fix | Delete
[165] Fix | Delete
def default_record_separator=(rs)
[166] Fix | Delete
@default_record_separator = rs
[167] Fix | Delete
end
[168] Fix | Delete
[169] Fix | Delete
# os resource mutex
[170] Fix | Delete
mutex_methods = ["unlock", "lock", "locked?", "synchronize", "try_lock"]
[171] Fix | Delete
for m in mutex_methods
[172] Fix | Delete
def_delegator("@debug_output_mutex", m, "debug_output_"+m.to_s)
[173] Fix | Delete
end
[174] Fix | Delete
[175] Fix | Delete
end
[176] Fix | Delete
[177] Fix | Delete
# call-seq:
[178] Fix | Delete
# Shell.new(pwd, umask) -> obj
[179] Fix | Delete
#
[180] Fix | Delete
# Creates a Shell object which current directory is set to the process
[181] Fix | Delete
# current directory, unless otherwise specified by the +pwd+ argument.
[182] Fix | Delete
def initialize(pwd = Dir.pwd, umask = nil)
[183] Fix | Delete
@cwd = File.expand_path(pwd)
[184] Fix | Delete
@dir_stack = []
[185] Fix | Delete
@umask = umask
[186] Fix | Delete
[187] Fix | Delete
@system_path = Shell.default_system_path
[188] Fix | Delete
@record_separator = Shell.default_record_separator
[189] Fix | Delete
[190] Fix | Delete
@command_processor = CommandProcessor.new(self)
[191] Fix | Delete
@process_controller = ProcessController.new(self)
[192] Fix | Delete
[193] Fix | Delete
@verbose = Shell.verbose
[194] Fix | Delete
@debug = Shell.debug
[195] Fix | Delete
end
[196] Fix | Delete
[197] Fix | Delete
# Returns the command search path in an array
[198] Fix | Delete
attr_reader :system_path
[199] Fix | Delete
[200] Fix | Delete
# Sets the system path (the Shell instance's PATH environment variable).
[201] Fix | Delete
#
[202] Fix | Delete
# +path+ should be an array of directory name strings.
[203] Fix | Delete
def system_path=(path)
[204] Fix | Delete
@system_path = path
[205] Fix | Delete
rehash
[206] Fix | Delete
end
[207] Fix | Delete
[208] Fix | Delete
[209] Fix | Delete
# Returns the umask
[210] Fix | Delete
attr_accessor :umask
[211] Fix | Delete
attr_accessor :record_separator
[212] Fix | Delete
attr_accessor :verbose
[213] Fix | Delete
attr_reader :debug
[214] Fix | Delete
[215] Fix | Delete
def debug=(val)
[216] Fix | Delete
@debug = val
[217] Fix | Delete
@verbose = val if val
[218] Fix | Delete
end
[219] Fix | Delete
[220] Fix | Delete
alias verbose? verbose
[221] Fix | Delete
alias debug? debug
[222] Fix | Delete
[223] Fix | Delete
attr_reader :command_processor
[224] Fix | Delete
attr_reader :process_controller
[225] Fix | Delete
[226] Fix | Delete
def expand_path(path)
[227] Fix | Delete
File.expand_path(path, @cwd)
[228] Fix | Delete
end
[229] Fix | Delete
[230] Fix | Delete
# Most Shell commands are defined via CommandProcessor
[231] Fix | Delete
[232] Fix | Delete
#
[233] Fix | Delete
# Dir related methods
[234] Fix | Delete
#
[235] Fix | Delete
# Shell#cwd/dir/getwd/pwd
[236] Fix | Delete
# Shell#chdir/cd
[237] Fix | Delete
# Shell#pushdir/pushd
[238] Fix | Delete
# Shell#popdir/popd
[239] Fix | Delete
# Shell#mkdir
[240] Fix | Delete
# Shell#rmdir
[241] Fix | Delete
[242] Fix | Delete
# Returns the current working directory.
[243] Fix | Delete
attr_reader :cwd
[244] Fix | Delete
alias dir cwd
[245] Fix | Delete
alias getwd cwd
[246] Fix | Delete
alias pwd cwd
[247] Fix | Delete
[248] Fix | Delete
attr_reader :dir_stack
[249] Fix | Delete
alias dirs dir_stack
[250] Fix | Delete
[251] Fix | Delete
# call-seq:
[252] Fix | Delete
# Shell.chdir(path)
[253] Fix | Delete
#
[254] Fix | Delete
# Creates a Shell object which current directory is set to +path+.
[255] Fix | Delete
#
[256] Fix | Delete
# If a block is given, it restores the current directory when the block ends.
[257] Fix | Delete
#
[258] Fix | Delete
# If called as iterator, it restores the current directory when the
[259] Fix | Delete
# block ends.
[260] Fix | Delete
def chdir(path = nil, verbose = @verbose)
[261] Fix | Delete
check_point
[262] Fix | Delete
[263] Fix | Delete
if iterator?
[264] Fix | Delete
notify("chdir(with block) #{path}") if verbose
[265] Fix | Delete
cwd_old = @cwd
[266] Fix | Delete
begin
[267] Fix | Delete
chdir(path, nil)
[268] Fix | Delete
yield
[269] Fix | Delete
ensure
[270] Fix | Delete
chdir(cwd_old, nil)
[271] Fix | Delete
end
[272] Fix | Delete
else
[273] Fix | Delete
notify("chdir #{path}") if verbose
[274] Fix | Delete
path = "~" unless path
[275] Fix | Delete
@cwd = expand_path(path)
[276] Fix | Delete
notify "current dir: #{@cwd}"
[277] Fix | Delete
rehash
[278] Fix | Delete
Void.new(self)
[279] Fix | Delete
end
[280] Fix | Delete
end
[281] Fix | Delete
alias cd chdir
[282] Fix | Delete
[283] Fix | Delete
# call-seq:
[284] Fix | Delete
# pushdir(path)
[285] Fix | Delete
# pushdir(path) { &block }
[286] Fix | Delete
#
[287] Fix | Delete
# Pushes the current directory to the directory stack, changing the current
[288] Fix | Delete
# directory to +path+.
[289] Fix | Delete
#
[290] Fix | Delete
# If +path+ is omitted, it exchanges its current directory and the top of its
[291] Fix | Delete
# directory stack.
[292] Fix | Delete
#
[293] Fix | Delete
# If a block is given, it restores the current directory when the block ends.
[294] Fix | Delete
def pushdir(path = nil, verbose = @verbose)
[295] Fix | Delete
check_point
[296] Fix | Delete
[297] Fix | Delete
if iterator?
[298] Fix | Delete
notify("pushdir(with block) #{path}") if verbose
[299] Fix | Delete
pushdir(path, nil)
[300] Fix | Delete
begin
[301] Fix | Delete
yield
[302] Fix | Delete
ensure
[303] Fix | Delete
popdir
[304] Fix | Delete
end
[305] Fix | Delete
elsif path
[306] Fix | Delete
notify("pushdir #{path}") if verbose
[307] Fix | Delete
@dir_stack.push @cwd
[308] Fix | Delete
chdir(path, nil)
[309] Fix | Delete
notify "dir stack: [#{@dir_stack.join ', '}]"
[310] Fix | Delete
self
[311] Fix | Delete
else
[312] Fix | Delete
notify("pushdir") if verbose
[313] Fix | Delete
if pop = @dir_stack.pop
[314] Fix | Delete
@dir_stack.push @cwd
[315] Fix | Delete
chdir pop
[316] Fix | Delete
notify "dir stack: [#{@dir_stack.join ', '}]"
[317] Fix | Delete
self
[318] Fix | Delete
else
[319] Fix | Delete
Shell.Fail DirStackEmpty
[320] Fix | Delete
end
[321] Fix | Delete
end
[322] Fix | Delete
Void.new(self)
[323] Fix | Delete
end
[324] Fix | Delete
alias pushd pushdir
[325] Fix | Delete
[326] Fix | Delete
# Pops a directory from the directory stack, and sets the current directory
[327] Fix | Delete
# to it.
[328] Fix | Delete
def popdir
[329] Fix | Delete
check_point
[330] Fix | Delete
[331] Fix | Delete
notify("popdir")
[332] Fix | Delete
if pop = @dir_stack.pop
[333] Fix | Delete
chdir pop
[334] Fix | Delete
notify "dir stack: [#{@dir_stack.join ', '}]"
[335] Fix | Delete
self
[336] Fix | Delete
else
[337] Fix | Delete
Shell.Fail DirStackEmpty
[338] Fix | Delete
end
[339] Fix | Delete
Void.new(self)
[340] Fix | Delete
end
[341] Fix | Delete
alias popd popdir
[342] Fix | Delete
[343] Fix | Delete
# Returns a list of scheduled jobs.
[344] Fix | Delete
def jobs
[345] Fix | Delete
@process_controller.jobs
[346] Fix | Delete
end
[347] Fix | Delete
[348] Fix | Delete
# call-seq:
[349] Fix | Delete
# kill(signal, job)
[350] Fix | Delete
#
[351] Fix | Delete
# Sends the given +signal+ to the given +job+
[352] Fix | Delete
def kill(sig, command)
[353] Fix | Delete
@process_controller.kill_job(sig, command)
[354] Fix | Delete
end
[355] Fix | Delete
[356] Fix | Delete
# call-seq:
[357] Fix | Delete
# def_system_command(command, path = command)
[358] Fix | Delete
#
[359] Fix | Delete
# Convenience method for Shell::CommandProcessor.def_system_command.
[360] Fix | Delete
# Defines an instance method which will execute the given shell command.
[361] Fix | Delete
# If the executable is not in Shell.default_system_path, you must
[362] Fix | Delete
# supply the path to it.
[363] Fix | Delete
#
[364] Fix | Delete
# Shell.def_system_command('hostname')
[365] Fix | Delete
# Shell.new.hostname # => localhost
[366] Fix | Delete
#
[367] Fix | Delete
# # How to use an executable that's not in the default path
[368] Fix | Delete
#
[369] Fix | Delete
# Shell.def_system_command('run_my_program', "~/hello")
[370] Fix | Delete
# Shell.new.run_my_program # prints "Hello from a C program!"
[371] Fix | Delete
#
[372] Fix | Delete
def Shell.def_system_command(command, path = command)
[373] Fix | Delete
CommandProcessor.def_system_command(command, path)
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
# Convenience method for Shell::CommandProcessor.undef_system_command
[377] Fix | Delete
def Shell.undef_system_command(command)
[378] Fix | Delete
CommandProcessor.undef_system_command(command)
[379] Fix | Delete
end
[380] Fix | Delete
[381] Fix | Delete
# call-seq:
[382] Fix | Delete
# alias_command(alias, command, *opts, &block)
[383] Fix | Delete
#
[384] Fix | Delete
# Convenience method for Shell::CommandProcessor.alias_command.
[385] Fix | Delete
# Defines an instance method which will execute a command under
[386] Fix | Delete
# an alternative name.
[387] Fix | Delete
#
[388] Fix | Delete
# Shell.def_system_command('date')
[389] Fix | Delete
# Shell.alias_command('date_in_utc', 'date', '-u')
[390] Fix | Delete
# Shell.new.date_in_utc # => Sat Jan 25 16:59:57 UTC 2014
[391] Fix | Delete
#
[392] Fix | Delete
def Shell.alias_command(ali, command, *opts, &block)
[393] Fix | Delete
CommandProcessor.alias_command(ali, command, *opts, &block)
[394] Fix | Delete
end
[395] Fix | Delete
[396] Fix | Delete
# Convenience method for Shell::CommandProcessor.unalias_command
[397] Fix | Delete
def Shell.unalias_command(ali)
[398] Fix | Delete
CommandProcessor.unalias_command(ali)
[399] Fix | Delete
end
[400] Fix | Delete
[401] Fix | Delete
# call-seq:
[402] Fix | Delete
# install_system_commands(pre = "sys_")
[403] Fix | Delete
#
[404] Fix | Delete
# Convenience method for Shell::CommandProcessor.install_system_commands.
[405] Fix | Delete
# Defines instance methods representing all the executable files found in
[406] Fix | Delete
# Shell.default_system_path, with the given prefix prepended to their
[407] Fix | Delete
# names.
[408] Fix | Delete
#
[409] Fix | Delete
# Shell.install_system_commands
[410] Fix | Delete
# Shell.new.sys_echo("hello") # => hello
[411] Fix | Delete
#
[412] Fix | Delete
def Shell.install_system_commands(pre = "sys_")
[413] Fix | Delete
CommandProcessor.install_system_commands(pre)
[414] Fix | Delete
end
[415] Fix | Delete
[416] Fix | Delete
#
[417] Fix | Delete
def inspect
[418] Fix | Delete
if debug.kind_of?(Integer) && debug > 2
[419] Fix | Delete
super
[420] Fix | Delete
else
[421] Fix | Delete
to_s
[422] Fix | Delete
end
[423] Fix | Delete
end
[424] Fix | Delete
[425] Fix | Delete
def self.notify(*opts)
[426] Fix | Delete
Shell::debug_output_synchronize do
[427] Fix | Delete
if opts[-1].kind_of?(String)
[428] Fix | Delete
yorn = verbose?
[429] Fix | Delete
else
[430] Fix | Delete
yorn = opts.pop
[431] Fix | Delete
end
[432] Fix | Delete
return unless yorn
[433] Fix | Delete
[434] Fix | Delete
if @debug_display_thread_id
[435] Fix | Delete
if @debug_display_process_id
[436] Fix | Delete
prefix = "shell(##{Process.pid}:#{Thread.current.to_s.sub("Thread", "Th")}): "
[437] Fix | Delete
else
[438] Fix | Delete
prefix = "shell(#{Thread.current.to_s.sub("Thread", "Th")}): "
[439] Fix | Delete
end
[440] Fix | Delete
else
[441] Fix | Delete
prefix = "shell: "
[442] Fix | Delete
end
[443] Fix | Delete
_head = true
[444] Fix | Delete
STDERR.print opts.collect{|mes|
[445] Fix | Delete
mes = mes.dup
[446] Fix | Delete
yield mes if iterator?
[447] Fix | Delete
if _head
[448] Fix | Delete
_head = false
[449] Fix | Delete
prefix + mes
[450] Fix | Delete
else
[451] Fix | Delete
" "* prefix.size + mes
[452] Fix | Delete
end
[453] Fix | Delete
}.join("\n")+"\n"
[454] Fix | Delete
end
[455] Fix | Delete
end
[456] Fix | Delete
[457] Fix | Delete
CommandProcessor.initialize
[458] Fix | Delete
CommandProcessor.run_config
[459] Fix | Delete
end
[460] Fix | Delete
[461] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function