Added optional passing of one or multiple environment variables via --env flag to the bonobo cli.

This commit is contained in:
cwandrews
2017-09-18 15:24:27 -04:00
parent 5a6e65ee77
commit 8b9dac50ec
3 changed files with 60 additions and 1 deletions

View File

@ -40,7 +40,10 @@ def _install_requirements(requirements):
importlib.reload(site)
def execute(filename, module, install=False, quiet=False, verbose=False):
def execute(filename, module, install=False, quiet=False, verbose=False,
env=None):
import re
import runpy
from bonobo import Graph, run, settings
@ -50,6 +53,12 @@ def execute(filename, module, install=False, quiet=False, verbose=False):
if verbose:
settings.DEBUG.set(True)
if env:
quote_killer = re.compile('["\']')
for e in env:
var_name, var_value = e.split('=')
os.environ[var_name] = quote_killer.sub('', var_value)
if filename:
if os.path.isdir(filename):
if install:
@ -106,4 +115,5 @@ def register(parser):
verbosity_group.add_argument('--quiet', '-q', action='store_true')
verbosity_group.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--install', '-I', action='store_true')
parser.add_argument('--env', '-e', action='append')
return execute

View File

@ -3,6 +3,7 @@ import runpy
import sys
from unittest.mock import patch
import pathlib
import pkg_resources
import pytest
@ -96,3 +97,29 @@ def test_version(runner, capsys):
out = out.strip()
assert out.startswith('bonobo ')
assert __version__ in out
@all_runners
def test_run_with_env(runner, capsys):
runner('run', '--quiet',
str(pathlib.Path(os.path.dirname(__file__),
'util', 'get_passed_env.py')),
'--env', 'ENV_TEST_NUMBER=123', '--env', 'ENV_TEST_USER=cwandrews',
'--env', "ENV_TEST_STRING='my_test_string'")
out, err = capsys.readouterr()
out = out.split('\n')
assert out[0] == 'cwandrews'
assert out[1] == '123'
assert out[2] == 'my_test_string'
@all_runners
def test_run_module_with_env(runner, capsys):
runner('run', '--quiet', '-m', 'tests.util.get_passed_env',
'--env', 'ENV_TEST_NUMBER=123', '--env', 'ENV_TEST_USER=cwandrews',
'--env', "ENV_TEST_STRING='my_test_string'")
out, err = capsys.readouterr()
out = out.split('\n')
assert out[0] == 'cwandrews'
assert out[1] == '123'
assert out[2] == 'my_test_string'

View File

@ -0,0 +1,22 @@
import os
from bonobo import Graph
def extract():
env_test_user = os.getenv('ENV_TEST_USER')
env_test_number = os.getenv('ENV_TEST_NUMBER')
env_test_string = os.getenv('ENV_TEST_STRING')
return env_test_user, env_test_number, env_test_string
def load(s: str):
print(s)
graph = Graph(extract, load)
if __name__ == '__main__':
from bonobo import run
run(graph)