module RubymentInvocationModule

# begin_documentation

This module offers functions for invocation of Rubyment (and Ruby) code in general.

# end_documentation

Public Instance Methods

bled_call(*args, &block) click to toggle source

Just a convenience for typing less. Calls the block inside a bled, which is a function which captures and returns exceptions and runtime errors in a functional way, instead of Ruby's way.

Examples:

bled_call {:return_value }
# => [:return_value, nil, nil]

bled_call { UndefinedStuff }
# => [nil,
# ...
#    nil],
#  NameError],
#   #<NameError: uninitialized constant #<Class:#<Rubyment:0x000000035adcc0>>::UndefinedStuff>]

bled_call [:return_value_on_error] { UndefinedStuff }
# => [:return_value_on_error,
#  [nil,
#   "message{\nuninitialized constant #<Class:#<Rubyment:0x0000000308e078>>::UndefinedStuff\
# File lib/rubyment.rb, line 1225
def bled_call *args, &block
  b = bled *args, &block
  b.first.call
end
invoke__basic_sender_array(args, &block) click to toggle source

Takes an array as parameter and invokes a method (second element of that array) of an object (first element of that array) giving the remaining array as argument list, and optionally giving a block as extra parameter.

Closed for extensions

examples:

invoke__basic_sender_array [ 9, “next” ] # => 10

invoke__basic_sender_array [ self, “p”, “args”, “to”, “p” ] # => [“args”, “to”, “p”]

invoke__basic_sender_array [ [[“S”, “2”], [“<”, “3” ]], :map ], &:join # => [“S2”, “<3”]

# File lib/rubyment.rb, line 1193
def invoke__basic_sender_array args, &block
  object, method_name, *args_to_method = args
  object.send method_name, *args_to_method, &block
end
ruby_code__from_binary(binary_or_script_blob, requirements=nil) click to toggle source

“Rubyfy” a system's executable binary string/blob/memory chunk. Ruby gems won't distribute binaries other than Ruby executables (no whims, it is just their mechanisms of handling binaries with multiple versions in the system, they solved only for Ruby case).

With this function, a binary string can be given (in a file_string definition/structure; ie as second element), and a ruby code (which normally relies on Rubyment) will be generated to wrap that binary in a ruby code. A temporary file (not for the ruby code, but for storing the binary memory chunk/string is by default generated at /tmp/ dir; however it can set if given as the first element of that file_string.

Examples:

ruby_code__from_binary [nil, File.read(“/bin/ls”) ] # long output # => “#!/usr/bin/env rubyn# Autogenerated on 2018.12.21_16:45:11nrequire 'base64'nblob_base64 = "f0VMRgIBAQAAAAAAAAAAAAIAPgABAAAAoElAAAAAAABAAAAAAAAAADjnAQAA\nAAAAAAAAAEAAOAAJAEAAHQAcAAYAAAA

# this version is good for self development: include this current .rb file, which contains function not yet in the gem package: ruby_code__from_binary [nil, File.read(“/bin/ls”) ], [ nil, :include_this_file ] # long output # => “#!/usr/bin/env rubyn# Autogenerated on 2018.12.21_16:45:11nrequire 'base64'nblob_base64 = "f0VMRgIBAQAAAAAAAAAAAAIAPgABAAAAoElAAAAAAABAAAAAAAAAADjnAQAA\nAAAAAAAAAEAAOAAJAEAAHQAcAAYAAAA

ruby_code__from_binary [“/tmp/temporary_ls”, File.read(“/bin/ls”) ] # long output # => “#!/usr/bin/env rubyn# Autogenerated on 2018.12.21_16:50:37nrequire "rubyment"nrequire 'base64'nblob_base64 =

system_command__exec_via_file [ [ nil, (ruby_code__from_binary [nil, File.read(“/bin/ls”) ]) ] ] # => “/tmp/rubyment.file.1e7f29e90deb6ba33b3e2e21540c77c56fca5995bb88141b572481f322290cad”

# this version is good for self development: include this current .rb file, which contains function not yet in the gem package: system_command__exec_via_file [ [ nil, (ruby_code__from_binary [nil, File.read(“/bin/ls”) ], [ nil, :include_this_file ]) ] ]

# or even: # this version is good for self development: include this current .rb file system_command__exec_via_file [ [ nil, (ruby_code__from_binary [nil, File.read(“/bin/ls”) ], [ nil, :include_this_file ]) ] , “-l” ]

# File lib/rubyment.rb, line 1337
  def ruby_code__from_binary binary_or_script_blob, requirements=nil
    require 'base64'
    temporary_file_path,
      blob_string = containerize(binary_or_script_blob)
    blob_base64 = Base64.encode64 blob_string

    required_gems,
      load_current,
      object_creator,
      memory_exec_function,
      reserved = containerize(requirements)

    memory_exec_function = memory_exec_function.nne(
      "system_command__exec_via_file"
    )

    object_creator = object_creator.nne(
      "#{self.class}.new"
    )

    memory_exec_function_call = string__recursive_join [
      ".",
      object_creator,
      memory_exec_function,
    ]


    required_gem,
      *only_one_currently_supported,
      reserved = containerize(required_gems)

    required_gem = required_gem.nne "rubyment"
    load_current &&= "load #{__FILE__.inspect}"

    code =<<-ENDHEREDOC
#!/usr/bin/env ruby
# Autogenerated on #{time__now_strftime_default}
require #{required_gem.inspect}
#{load_current}
require 'base64'
blob_base64 = #{blob_base64.inspect}
system #{memory_exec_function_call} [
  [
    #{temporary_file_path.inspect},
    Base64.decode64(blob_base64),
  ],
  ARGV,
]
    ENDHEREDOC
  end
system_command__exec_via_file(exec_via_file) click to toggle source

Generates a string that can be given to system (or any other command line executor) to execute a binary in memory (Ie, a file is generated having as contents a given string). If not file path is given a default one in /tmp will be generated (using the digest sha256 of the file (truncated at 96 chars, by now). Arguments can be given in an array (they will be escaped with Shellwords.join) or String (no escape apply)

Examples:

# write the contents of /bin/ls to a temporary file and offers # a command to execute it: system_command__exec_via_file [ [nil, File.read(“/bin/ls”) ] , [“-l”, “-h”] ] # => “/tmp/rubyment.file.a90ba058c747458330ba26b5e2a744f4fc57f92f9d0c9112b1cb2f76c66c4ba0 -l -h”

system_command__exec_via_file [ [“/tmp/my_ls”, File.read(“/bin/ls”) ] , [“-l”, “-h”] ] # => “/tmp/my_ls -l -h”

system_command__exec_via_file [ [“/tmp/my_ls”, File.read(“/bin/ls”) ] , “-l -h” ] # => “/tmp/my_ls -l -h”

# File lib/rubyment.rb, line 1256
def system_command__exec_via_file exec_via_file
 args = exec_via_file

  binary_or_script_blob,
  args_to_binary_or_blob,
    reserved = containerize(args)

  file_path_src,
    string_src,
    reserved = containerize(binary_or_script_blob)

  require 'openssl'
  string_src_sha256 = string_truncate [
    Digest::SHA256.hexdigest(string_src.to_s),
    96,
  ]
  default_path_for_string_src = "/tmp/rubyment.file.#{string_src_sha256}"

  actual_file_path,
    old_contents,
    reserved = file_string__experimental [
      file_path_src.nne(default_path_for_string_src),
      string_src,
    ]

  bled_call {
    require 'fileutils'
    FileUtils.chmod "+x", actual_file_path
  }
  require "shellwords"
  escaped_args_as_str = (
    bled_call [ args_to_binary_or_blob ] {
      Shellwords.join args_to_binary_or_blob
    }
  ).first.to_s
  string__recursive_join [
    " ",
    actual_file_path,
    escaped_args_as_str,
  ]
end