Class: Bovem::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/bovem/shell.rb

Overview

A utility class for most common shell operation.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Shell) initialize

Initializes a new Shell.



21
22
23
# File 'lib/bovem/shell.rb', line 21

def initialize
  @console = ::Bovem::Console.instance
end

Instance Attribute Details

- (Object) console

A Console instance.



11
12
13
# File 'lib/bovem/shell.rb', line 11

def console
  @console
end

Class Method Details

+ (Shell) instance

Returns a unique instance for Shell.

Returns:

  • (Shell)

    A new instance.



16
17
18
# File 'lib/bovem/shell.rb', line 16

def self.instance
  @instance ||= ::Bovem::Shell.new
end

Instance Method Details

- (Object) check(path, tests)

Tests a path against a list of test.

Valid tests are every method available in http://www.ruby-doc.org/core-1.9.3/FileTest.html (plus read, write, execute, exec, dir). Trailing question mark can be omitted.

Parameters:

  • path (String)

    The path to test.

  • tests (Array)

    The list of tests to perform.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bovem/shell.rb', line 73

def check(path, tests)
  path = path.ensure_string

  tests.ensure_array.all? {|test|
    # Adjust test name
    test = test.ensure_string.strip

    test = case test
      when "read" then "readable"
      when "write" then "writable"
      when "execute", "exec" then "executable"
      when "dir" then "directory"
      else test
    end

    # Execute test
    test += "?" if test !~ /\?$/
    FileTest.respond_to?(test) ? FileTest.send(test, path) : nil
  }
end

- (Boolean) copy(src, dst, run = true, show_errors = false, fatal = true)

Copies a set of files or directory to another location.

Parameters:

  • src (String|Array)

    The entries to copy. If is an Array, dst is assumed to be a directory.

  • dst (String)

    The destination. Any existing entries will be overwritten. Any required directory will be created.

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of message that would be copied or moved.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



238
239
240
# File 'lib/bovem/shell.rb', line 238

def copy(src, dst, run = true, show_errors = false, fatal = true)
  self.copy_or_move(src, dst, :copy, run, show_errors, fatal)
end

- (Boolean) copy_or_move(src, dst, operation, run = true, show_errors = false, fatal = true)

Copies or moves a set of files or directory to another location.

Parameters:

  • src (String|Array)

    The entries to copy or move. If is an Array, dst is assumed to be a directory.

  • dst (String)

    The destination. Any existing entries will be overwritten. Any required directory will be created.

  • operation (Symbol)

    The operation to perform. Valid values are :copy or :move.

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of message that would be copied or moved.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/bovem/shell.rb', line 146

def copy_or_move(src, dst, operation, run = true, show_errors = false, fatal = true)
  rv = true
  operation = :copy if operation != :move
  single = !src.is_a?(Array)

  if single then
    src = File.expand_path(src)
  else
    src = src.collect {|s| File.expand_path(s) }
  end

  dst = File.expand_path(dst.ensure_string)

  if !run then
    if single then
      self.console.warn("Will #{operation} a file:")
      self.console.write("From: {mark=bright}#{File.expand_path(src.ensure_string)}{/mark}", "\n", 11)
      self.console.write("To: {mark=bright}#{dst}{/mark}", "\n", 11)
    else
      self.console.warn("Will #{operation} following entries:")
      self.console.with_indentation(11) do
        src.each do |s| self.console.write(s) end
      end
      self.console.write("to directory: {mark=bright}#{dst}{/mark}", "\n", 5)
    end
  else
    rv = catch(:rv) do
      dst_dir = single ? File.dirname(dst) : dst
      has_dir = self.check(dst_dir, :dir)

      # Create directory
      has_dir = self.create_directories(dst_dir, 0755, true, show_errors, fatal) if !has_dir
      throw(:rv, false) if !has_dir

      if single && self.check(dst, :dir) then
        @console.send(fatal ? :fatal : :error, "Cannot #{operation} file {mark=bright}#{src}{/mark} to {mark=bright}#{dst}{/mark} because it is currently a directory.")
        throw(:rv, false)
      end

      # Check that every file is existing
      src.ensure_array.each do |s|
        if !self.check(s, :exists) then
          @console.send(fatal ? :fatal : :error, "Cannot #{operation} non existent file {mark=bright}#{s}{/mark}.")
          throw(:rv, false)
        end
      end

      # Do operation
      begin
        FileUtils.send(operation == :move ? :mv : :cp_r, src, dst, {:noop => false, :verbose => false})
      rescue Errno::EACCES => e
        if single then
          @console.send(fatal ? :fatal : :error, "Cannot #{operation} file {mark=bright}#{src}{/mark} to non writable directory {mark=bright}#{dst_dir}{/mark}.")
        else
          self.console.error("Cannot #{operation} following file(s) to non writable directory {mark=bright}#{dst}{/mark}:")
          self.console.with_indentation(11) do
            src.each do |s| self.console.write(s) end
          end
          Kernel.exit(-1) if fatal
        end

        throw(:rv, false)
      rescue Exception => e
        if single then
          @console.send(fatal ? :fatal : :error, "Cannot #{operation} file {mark=bright}#{src}{/mark} to directory {mark=bright}#{dst_dir}{/mark} due to this error: [#{e.class.to_s}] #{e}.", "\n", 5)
        else
          self.console.error("Cannot #{operation} following entries to {mark=bright}#{dst}{/mark}:")
          self.console.with_indentation(11) do
            src.each do |s| self.console.write(s) end
          end
          self.console.write("due to this error: [#{e.class.to_s}] #{e}.", "\n", 5)
          Kernel.exit(-1) if fatal
        end

        throw(:rv, false)
      end

      true
    end
  end

  rv
end

- (Boolean) create_directories(directories, mode = 0755, run = true, show_errors = false, fatal = true)

Creates a list of directories, included missing parent directories.

Parameters:

  • directories (Array)

    The list of directories to create.

  • mode (Fixnum) (defaults to: 0755)

    Initial permissions for the new directories.

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of directories that would be created.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/bovem/shell.rb', line 296

def create_directories(directories, mode = 0755, run = true, show_errors = false, fatal = true)
  rv = true

  # Adjust directory
  directories = directories.ensure_array.compact {|d| File.expand_path(d.ensure_string) }

  if !run then # Just print
    self.console.warn("Will create directories:")
    self.console.with_indentation(11) do
      directories.each do |directory| self.console.write(directory) end
    end
  else
    directories.each do |directory|
      rv = catch(:rv) do
        # Perform tests
        if self.check(directory, :directory) then
          self.console.send(fatal ? :fatal : :error, "The directory {mark=bright}#{directory}{/mark} already exists.")
        elsif self.check(directory, :exist) then
          self.console.send(fatal ? :fatal : :error, "Path {mark=bright}#{directory}{/mark} is currently a file.")
        else
          begin # Create directory
            FileUtils.mkdir_p(directory, {:mode => mode, :noop => false, :verbose => false})
            throw(:rv, true)
          rescue Errno::EACCES => e
            self.console.send(fatal ? :fatal : :error, "Cannot create following directory due to permission denied: {mark=bright}#{e.message.gsub(/.+ - (.+)/, "\\1")}{/mark}.")
          rescue Exception => e
            if show_errors then
              self.console.error("Cannot create following directories:")
              self.console.with_indentation(11) do
                directories.each do |directory| self.console.write(directory) end
              end
              self.console.write("due to this error: [#{e.class.to_s}] #{e}.", "\n", 5)
              Kernel.exit(-1) if fatal
            end
          end
        end

        false
      end

      break if !rv
    end
  end

  rv
end

- (Boolean) delete(files, run = true, show_errors = false, fatal = true)

Deletes a list of files.

Parameters:

  • files (Array)

    The list of files to remove

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of message that would be deleted.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bovem/shell.rb', line 101

def delete(files, run = true, show_errors = false, fatal = true)
  rv = true
  files = files.ensure_array.compact.collect {|f| File.expand_path(f.ensure_string) }

  if !run then
    self.console.warn("Will remove file(s):")
    self.console.with_indentation(11) do
      files.each do |file| self.console.write(file) end
    end
  else
    rv = catch(:rv) do
      begin
        FileUtils.rm_r(files, {:noop => false, :verbose => false, :secure => true})
        throw(:rv, true)
      rescue Errno::EACCES => e
        self.console.send(fatal ? :fatal : :error, "Cannot remove following non writable file: {mark=bright}#{e.message.gsub(/.+ - (.+)/, "\\1")}{/mark}")
      rescue Errno::ENOENT => e
        self.console.send(fatal ? :fatal : :error, "Cannot remove following non existent file: {mark=bright}#{e.message.gsub(/.+ - (.+)/, "\\1")}{/mark}")
      rescue Exception => e
        if show_errors then
          self.console.error("Cannot remove following file(s):")
          self.console.with_indentation(11) do
            files.each do |file| self.console.write(file) end
          end
          self.console.write("due to this error: [#{e.class.to_s}] #{e}.", "\n", 5)
          Kernel.exit(-1) if fatal
        end
      end

      false
    end
  end

  rv
end

- (Object) find(directories, patterns = [], by_extension = false, case_sensitive = false)

Find a list of files in directories matching given regexps or patterns.

You can also pass a block to perform matching. The block will receive a single argument and the path will be considered matched if the blocks not evaluates to nil or false.

Inside the block, you can call Find.prune to stop searching in the current directory.

Parameters:

  • directories (String)

    A list of directories where to search files.

  • patterns (Array) (defaults to: [])

    A list of regexps or patterns to match files. If empty, every file is returned. Ignored if a block is provided.

  • by_extension (Boolean) (defaults to: false)

    If to only search in extensions. Ignored if a block is provided.

  • case_sensitive (Boolean) (defaults to: false)

    If the search is case sensitive. Only meaningful for string patterns.



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/bovem/shell.rb', line 353

def find(directories, patterns = [], by_extension = false, case_sensitive = false)
  rv = []

  # Adjust directory
  directories = directories.ensure_array.compact {|d| File.expand_path(d.ensure_string) }

  # Adjust patterns
  patterns = patterns.ensure_array.compact.collect {|p| p.is_a?(::Regexp) ? p : Regexp.new(Regexp.quote(p.ensure_string)) }
  patterns = patterns.collect {|p| /(#{p.source})$/ } if by_extension
  patterns = patterns.collect {|p| /#{p.source}/i } if !case_sensitive

  directories.each do |directory|
    if self.check(directory, [:directory, :readable, :executable]) then
      Find.find(directory) do |entry|
        found = patterns.blank? ? true : catch(:found) do
          if block_given? then
            throw(:found, true) if yield(entry)
          else
            patterns.each do |pattern|
              throw(:found, true) if pattern.match(entry) && (!by_extension || !File.directory?(entry))
            end
          end

          false
        end

        rv << entry if found
      end
    end
  end

  rv
end

- (Boolean) move(src, dst, run = true, show_errors = false, fatal = true)

Moves a set of files or directory to another location.

Parameters:

  • src (String|Array)

    The entries to move. If is an Array, dst is assumed to be a directory.

  • dst (String)

    The destination. Any existing entries will be overwritten. Any required directory will be created.

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of message that would be deleted.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



250
251
252
# File 'lib/bovem/shell.rb', line 250

def move(src, dst, run = true, show_errors = false, fatal = true)
  self.copy_or_move(src, dst, :move, run, show_errors, fatal)
end

- (Hash) run(command, message = nil, run = true, show_exit = true, show_output = false, show_command = false, fatal = true)

Runs a command into the shell.

Parameters:

  • command (String)

    The string to run.

  • message (String) (defaults to: nil)

    A message to show before running.

  • run (Boolean) (defaults to: true)

    If false, it will just print a message with the full command that will be run.

  • show_exit (Boolean) (defaults to: true)

    If show the exit status.

  • show_output (Boolean) (defaults to: false)

    If show command output.

  • show_command (Boolean) (defaults to: false)

    If show the command that will be run.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Hash)

    An hash with status and output keys.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bovem/shell.rb', line 35

def run(command, message = nil, run = true, show_exit = true, show_output = false, show_command = false, fatal = true)
  rv = {:status => 0, :output => ""}
  command = command.ensure_string

  # Show the command
  self.console.begin(message) if message.present?


  if !run then # Print a message
    self.console.warn("Will run command: {mark=bright}\"#{command}\"{/mark}...")
    self.console.status(:ok) if show_exit
  else # Run
    output = ""

    self.console.info("Running command: {mark=bright}\"#{command}\"{/mark}...") if show_command
    rv[:status] = ::Open4::popen4(command + " 2>&1") { |pid, stdin, stdout, stderr|
      stdout.each_line do |line|
        output << line
        Kernel.print line if show_output
      end
    }.exitstatus

    rv[:output] = output
  end

  # Return
  self.console.status(rv[:status] == 0 ? :ok : :fail) if show_exit
  exit(rv[:status]) if fatal && rv[:status] != 0
  rv
end

- (Boolean) within_directory(directory, restore = true, show_messages = false)

Executes a block of code in another directory.

Parameters:

  • directory (String)

    The new working directory.

  • restore (Boolean) (defaults to: true)

    If to restore the original working directory.

  • show_messages (Boolean) (defaults to: false)

    Show informative messages about working directory changes.

Returns:

  • (Boolean)

    true if the directory was valid and the code executed, false otherwise.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/bovem/shell.rb', line 260

def within_directory(directory, restore = true, show_messages = false)
  rv = false
  original = Dir.pwd
  directory = File.expand_path(directory.ensure_string)

  if self.check(directory, [:directory, :executable]) then
    begin
      self.console.info("Moving into directory {mark=bright}#{directory}{/mark}") if show_messages
      Dir.chdir(directory)
      rv = true
    rescue Exception => e
    end
  end

  yield if rv && block_given?

  if rv && original then
    begin
      self.console.info("Moving back into directory {mark=bright}#{original}{/mark}") if show_messages
      Dir.chdir(original) if restore
    rescue Exception => e
      rv = false
    end
  end

  rv
end