Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec step #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ test:
tests/cli/test_cli_exit_codes.sh &&\
tests/cli/test_cli_logs.sh &&\
tests/cli/test_custom_formatters.sh &&\
tests/cli/test_flow.sh
tests/cli/test_flow.sh &&\
tests/cli/test_exec.sh

version:
@echo $(VERSION)
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ my-flow:

You can run the pipeline using `dpp run my-flow`.

### Executing a subprocess

You can integrate system executables or shell scripts into the pipeline using `exec`.
This can be used for example to set permissions / run external binaries / install system dependencies.
The execution output is logged in real-time to standard logging at `info` logging level.

Exec commands must be placed before the pipeline steps

```
process-data:
pipeline:
- exec: [sh, set_permissions.sh]
- exec: [ls, -lah]
- exec: |
curl http://example.com/data.zip > my-data.zip
unzip my-data.zip
- flow: ..
- run: ..
..
```

## The Standard Processor Library

A few built in processors are provided with the library.
Expand Down
16 changes: 16 additions & 0 deletions datapackage_pipelines/lib/exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from datapackage_pipelines.wrapper import ingest
import subprocess
import logging
import os


with ingest() as ctx:
parameters, datapackage, resources = ctx
assert len(datapackage['resources']) == 0, 'exec processor does not support input data'
cmd = parameters.pop('__exec')
os.environ['__EXEC_PROCESSOR_PATH'] = parameters.pop('__path')
with subprocess.Popen(cmd, shell=isinstance(cmd, str), stdout=subprocess.PIPE) as p:
for line in p.stdout:
logging.info(line.decode())
p.wait()
assert p.returncode == 0, f'exec failed, returncode = {p.returncode}'
4 changes: 4 additions & 0 deletions datapackage_pipelines/specs/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def resolve_executor(step, path, errors):
step['run'] = 'flow'
step.setdefault('parameters', {}).update(__flow=step.pop('flow'),
__path=path)
elif 'exec' in step:
step['run'] = 'exec'
step.setdefault('parameters', {}).update(__exec=step.pop('exec'),
__path=path)

executor = step['run']
back_up, parts = convert_dot_notation(executor)
Expand Down
5 changes: 5 additions & 0 deletions datapackage_pipelines/specs/schemas/pipeline-spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"required": [
"flow"
]
},
{
"required": [
"exec"
]
}
],
"properties": {
Expand Down
16 changes: 16 additions & 0 deletions tests/cli/pipeline-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ dataflows:
- run: dump_to_path
parameters:
out-path: test_flow_data

exec:
pipeline:
- exec: [ls, -lah]
- exec: [bash, -c, 'echo __EXEC_PROCESSOR_PATH = $__EXEC_PROCESSOR_PATH']
- run: load_resource
parameters:
url: ../data/datapackage.json
resource: my-spiffy-resource

exec_shell:
pipeline:
- exec: |
TEMPFILE=`mktemp`
echo test > $TEMPFILE
echo $TEMPFILE > test_exec_shell_tempfile
30 changes: 30 additions & 0 deletions tests/cli/test_exec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

cd tests/cli

TEMPFILE=`mktemp`

set -o pipefail
! dpp run --verbose ./exec >/dev/stdout 2>&1 | tee $TEMPFILE && echo failed to run exec pipeline && exit 1
set +o pipefail

! cat "${TEMPFILE}" | grep "test_exec.sh" && echo exec output is missing && exit 1
! cat "${TEMPFILE}" | grep "__EXEC_PROCESSOR_PATH" && echo exec output is missing && exit 1
rm $TEMPFILE

TEMPFILE=`mktemp`

rm -f test_exec_shell_tempfile

set -o pipefail
! dpp run --verbose ./exec_shell >/dev/stdout 2>&1 | tee $TEMPFILE && echo failed to run exec_shell pipeline && exit 1
set +o pipefail

! [ "$(cat $(cat test_exec_shell_tempfile))" == "test" ] \
&& echo unexecpted data && exit 1

rm $(cat test_exec_shell_tempfile)
rm test_exec_shell_tempfile

echo Great Success
exit 0