Skip to content

Commit

Permalink
Merge pull request #75 from fsteggink/execwithenvvars
Browse files Browse the repository at this point in the history
Introduced env_args and env_separator config settings to outputs.execoutput.ExecOutput
  • Loading branch information
justb4 authored Jul 3, 2018
2 parents 93188f4 + dbf3e28 commit c53f21a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
36 changes: 28 additions & 8 deletions stetl/filters/execfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
import subprocess
import os
from stetl.component import Config
from stetl.filter import Filter
from stetl.util import Util
from stetl.packet import FORMAT
Expand All @@ -19,21 +20,40 @@ class ExecFilter(Filter):
Executes any command (abstract base class).
"""

@Config(ptype=str, default='', required=False)
def env_args(self):
"""
Provides of list of environment variables which will be used when executing the given command.
Example: env_args = pgpassword=postgres othersetting=value~with~spaces
"""
pass

@Config(ptype=str, default='=', required=False)
def env_separator(self):
"""
Provides the separator to split the environment variable names from their values.
"""
pass

def __init__(self, configdict, section, consumes, produces):
Filter.__init__(self, configdict, section, consumes, produces)

def invoke(self, packet):
return packet

def execute_cmd(self, cmd):
use_shell = True
if os.name == 'nt':
use_shell = False

log.info("executing cmd=%s" % cmd)
result = subprocess.check_output(cmd, shell=use_shell)
log.info("execute done")
return result
env_vars = Util.string_to_dict(self.env_args, self.env_separator)
old_environ = os.environ.copy()

try:
os.environ.update(env_vars)
log.info("executing cmd=%s" % cmd)
result = subprocess.check_output(cmd, shell=True)
log.info("execute done")
return result
finally:
os.environ = old_environ


class CommandExecFilter(ExecFilter):
Expand Down
33 changes: 26 additions & 7 deletions stetl/outputs/execoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,39 @@ class ExecOutput(Output):
Executes any command (abstract base class).
"""

@Config(ptype=str, default='', required=False)
def env_args(self):
"""
Provides of list of environment variables which will be used when executing the given command.
Example: env_args = pgpassword=postgres othersetting=value~with~spaces
"""
pass

@Config(ptype=str, default='=', required=False)
def env_separator(self):
"""
Provides the separator to split the environment variable names from their values.
"""
pass

def __init__(self, configdict, section, consumes):
Output.__init__(self, configdict, section, consumes)

def write(self, packet):
return packet

def execute_cmd(self, cmd):
use_shell = True
if os.name == 'nt':
use_shell = False

log.info("executing cmd=%s" % cmd)
subprocess.call(cmd, shell=use_shell)
log.info("execute done")
env_vars = Util.string_to_dict(self.env_args, self.env_separator)
old_environ = os.environ.copy()

try:
os.environ.update(env_vars)
log.info("executing cmd=%s" % cmd)
subprocess.call(cmd, shell=True)
log.info("execute done")
finally:
os.environ = old_environ


class CommandExecOutput(ExecOutput):
Expand Down
2 changes: 2 additions & 0 deletions tests/filters/configs/commandexecfilter.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ file_path = tests/data/commandexecfilter.txt

[command_executor]
class = filters.execfilter.CommandExecFilter
env_args = pgpassword!postgres
env_separator = !

[packet_buffer]
class = filters.packetbuffer.PacketBuffer
Expand Down
2 changes: 2 additions & 0 deletions tests/outputs/configs/commandexecoutput.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ file_path = tests/data/command.txt
[output_exec]
class = outputs.execoutput.CommandExecOutput
input_format = string
env_args = pgpassword!postgres
env_separator = !

0 comments on commit c53f21a

Please sign in to comment.