Class: RightScraper::Processes::Shell

Inherits:
Object
  • Object
show all
Includes:
RightGit::Shell::Interface
Defined in:
lib/right_scraper/processes/shell.rb

Overview

provides a shell with configurable properties that satisfies the interface for a shell for right_git but can be used for other scraper actions.

Defined Under Namespace

Classes: LimitError, SizeLimitError, TimeLimitError

Constant Summary collapse

MAX_SAFE_BUFFER_LINE_COUNT =
10
MAX_SAFE_BUFFER_LINE_LENGTH =
128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.

Parameters:

Options Hash (options):

  • :initial_directory (Integer)

    for child process (Default = use current directory)

  • :max_bytes (Integer)

    for interruption (Default = no byte limit)

  • :max_seconds (Integer)

    for interruption (Default = no time limit)

  • :watch_directory (Integer)

    for interruption (Default = no byte limit)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/right_scraper/processes/shell.rb', line 57

def initialize(options = {})
  options = {
    :initial_directory => nil,
    :max_bytes         => nil,
    :max_seconds       => nil,
    :watch_directory   => nil,
  }.merge(options)

  @initial_directory = options[:initial_directory]
  @max_bytes = options[:max_bytes]
  @max_seconds = options[:max_seconds]
  @watch_directory = options[:watch_directory]

  # set stop time once for the lifetime of this shell object.
  @stop_timestamp = (::Time.now + @max_seconds).to_i if @max_seconds
end

Instance Attribute Details

#initial_directoryObject

Returns the value of attribute initial_directory.



48
49
50
# File 'lib/right_scraper/processes/shell.rb', line 48

def initial_directory
  @initial_directory
end

#max_bytesObject

Returns the value of attribute max_bytes.



48
49
50
# File 'lib/right_scraper/processes/shell.rb', line 48

def max_bytes
  @max_bytes
end

#max_secondsObject

Returns the value of attribute max_seconds.



48
49
50
# File 'lib/right_scraper/processes/shell.rb', line 48

def max_seconds
  @max_seconds
end

#stop_timestampObject

Returns the value of attribute stop_timestamp.



49
50
51
# File 'lib/right_scraper/processes/shell.rb', line 49

def stop_timestamp
  @stop_timestamp
end

#watch_directoryObject

Returns the value of attribute watch_directory.



49
50
51
# File 'lib/right_scraper/processes/shell.rb', line 49

def watch_directory
  @watch_directory
end

Instance Method Details

#execute(cmd, options = {}) ⇒ Integer

Implements execute interface.

Parameters:

  • cmd (String)

    the shell command to run

  • options (Hash) (defaults to: {})

    for execution

Returns:

  • (Integer)

    exitstatus of the command

Raises:

  • (ShellError)

    on failure only if :raise_on_failure is true



82
83
84
85
86
# File 'lib/right_scraper/processes/shell.rb', line 82

def execute(cmd, options = {})
  inner_execute(cmd, :safe_output_handler, options)
ensure
  @output = nil
end

#exit_handler(status) ⇒ TrueClass

Handles exit status.

Parameters:

  • status (Status)

    after execution

Returns:

  • (TrueClass)

    always true

Raises:

  • (ShellError)

    on execution failure



148
149
150
151
152
153
154
155
# File 'lib/right_scraper/processes/shell.rb', line 148

def exit_handler(status)
  @exit_code = status.exitstatus
  if @raise_on_failure && !status.success?
    @output.buffer << "Exit code = #{@exit_code}"
    raise ::RightScraper::Error, "Execution failed: #{@output.display_text}"
  end
  true
end

#output_for(cmd, options = {}) ⇒ String

Implements output_for interface.

Parameters:

  • cmd (String)

    command to execute

  • options (Hash) (defaults to: {})

    for execution

Returns:

  • (String)

    entire output (stdout) of the command

Raises:

  • (ShellError)

    on failure only if :raise_on_failure is true



96
97
98
99
100
101
# File 'lib/right_scraper/processes/shell.rb', line 96

def output_for(cmd, options = {})
  inner_execute(cmd, :unsafe_output_handler, options)
  @output.display_text
ensure
  @output = nil
end

#safe_output_handler(data) ⇒ TrueClass

Buffers output safely.

Parameters:

  • data (String)

Returns:

  • (TrueClass)

    always true



108
109
110
111
# File 'lib/right_scraper/processes/shell.rb', line 108

def safe_output_handler(data)
  @output.safe_buffer_data(data)
  true
end

#size_limit_handlerObject

Raises size limit error.

Raises:



126
127
128
129
130
131
132
# File 'lib/right_scraper/processes/shell.rb', line 126

def size_limit_handler
  message =
    "Exceeded size limit of #{@max_bytes / (1024 * 1024)} MB on " +
    "repository directory. Hidden file and directory sizes are not " +
    "included in the total."
  raise SizeLimitError, message
end

#timeout_handlerObject

Raises timeout error.

Raises:



137
138
139
# File 'lib/right_scraper/processes/shell.rb', line 137

def timeout_handler
  raise TimeLimitError, "Timed-out after #{@max_seconds} seconds"
end

#unsafe_output_handler(data) ⇒ TrueClass

Buffers output unsafely but completely.

Parameters:

  • data (String)

Returns:

  • (TrueClass)

    always true



118
119
120
121
# File 'lib/right_scraper/processes/shell.rb', line 118

def unsafe_output_handler(data)
  @output.buffer << data.chomp
  true
end