Juicy lil' bytes

A Simple Approach to Execute Ruby Scripts With Python

Here are a couple of examples that demonstrate how to execute Ruby scripts from a Python script with standard output and standard error stream handling on either the Ruby side or the Python side.

Ruby Dependency

Execution of local Ruby script files requires you to have Ruby installed on your system. Refer to the Ruby documentation for details.

The Naked Shell Library

The functions that we will use are available in the Naked Framework shell library. To install Naked with pip, use the following command:

Install Naked with Pip

1
pip install Naked

or download the source from GitHub, decompress the source archive locally, navigate to the top level source directory and run the following command:

Install Naked from Source

1
python setup.py install

Import Naked Shell Library Functions

1
from Naked.toolshed.shell import execute_rb, muterun_rb

Execute a Ruby Script

To execute a Ruby file and allow the Ruby side to control the standard output and standard error streams for the user, use the execute_rb() function. The path to the Ruby file is included as a parameter to the function like this:

Execute the Ruby File

1
2
3
from Naked.toolshed.shell import execute_rb

success = execute_rb('testscript.rb')

The execute_rb() function returns a boolean value for the success (or lack thereof) of the Ruby script. This is based upon the zero (success) or non-zero (failure) exit code returned from the Ruby code to the Python calling code.

You can test for the success of the Ruby script like this:

Test for Success of Ruby Execution

1
2
3
4
if success:
    # handle success of the Ruby code
else:
    # handle failure of the Ruby code

because the success variable is defined with the returned boolean value from the execute_rb() function.

This function executes the Ruby file with the following system command:

1
ruby <Ruby file path> [arguments]

To pass additional arguments to the ruby executable, include them as a second string parameter in the execute_rb() function. See the Naked documentation for additional details.

Execute a Ruby Script and Control the Standard Ouput and Standard Error from the Python Side

To gain control over the standard output and standard error streams returned from the Ruby script, use the muterun_rb() function rather than the execute_rb() function. This function suppresses output of the standard output and error streams from the Ruby script and, instead, returns these streams along with the exit code to the Python script as attributes of a generic object. You can access these strings (actually bytes strings) using standard Python dot syntax.

As with the execute_rb() function, use the Ruby file path as the argument to the function:

Execute the Ruby File

1
2
3
4
import sys
from Naked.toolshed.shell import muterun_rb

response = muterun_rb('testscript.rb')

The object is assigned to the response variable. To confirm that the executed code was successful, you can test the exitcode attribute returned in the object:

Test for Success of the Executed Ruby File

1
2
3
4
if response.exitcode == 0:
    # the command was successful (returned 0 exit code)
else:
    # the command was not successful (returned non-0 exit code)

You can then access the standard output and standard error stream strings (acutally bytes strings) with the stdout and stderr attributes, respectively:

1
2
3
4
if response.exitcode == 0:
  print(response.stdout)
else:
  sys.stderr.write(response.stderr)

Here, we simply push it back out to the standard output or error stream from the Python script. You can manipulate the output however you would like in your Python code before pushing it to the user.

You have the option to pass additional arguments to the ruby system command that this function wraps by including them in a second parameter string to the function. Details are available in the Naked documentation.

Distribute Naked With Your Code

To make the Naked Framework available for other users of the Python script that you developed, include the following line in your setup.py file:

1
install_requires=['Naked'],

This includes Naked as a dependency in your project and automatically installs the Naked Framework on the user’s system when they install your application or library using commands based on distutils, including the commonly used pip install and python setup.py install install commands.

The Naked Shell Library

If you are interested in learning more about the Naked shell library module, you can find the documentation here.

Naked Does Node Too…

Naked includes a method for Node.js scripts as well. Details are available in this post.