Skip to content

Commit

Permalink
Merge pull request #27 from Autodesk/environment-variables
Browse files Browse the repository at this point in the history
Set job environment variables
  • Loading branch information
avirshup authored Mar 9, 2018
2 parents 25ac40d + 724610e commit 614c0a0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pyccc/engines/dockerengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def _generate_container_args(self, job):
working_dir=job.workingdir,
environment={'PYTHONIOENCODING':'utf-8'})

if job.env:
container_args['environment'].update(job.env)

if job.engine_options:
volumes = []
binds = []
Expand Down
2 changes: 2 additions & 0 deletions pyccc/engines/subproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def submit(self, job):

subenv = os.environ.copy()
subenv['PYTHONIOENCODING'] = 'utf-8'
if job.env:
subenv.update(job.env)
job.subproc = subprocess.Popen(job.command,
shell=True,
cwd=job.workingdir,
Expand Down
5 changes: 4 additions & 1 deletion pyccc/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Job(object):
engine_options (dict): additional engine-specific options
workingdir (str): working directory in the execution environment (i.e., on the local
system for a subprocess, or inside the container for a docker engine)
env (Dict[str,str]): custom environment variables for the Job
"""
def __init__(self, engine=None,
image=None,
Expand All @@ -84,14 +85,16 @@ def __init__(self, engine=None,
on_status_update=None,
when_finished=None,
workingdir=None,
engine_options=None):
engine_options=None,
env=None):

self.name = name
self.engine = engine
self.image = image
self.command = if_not_none(command, '')
self.engine_options = engine_options
self.workingdir = workingdir
self.env = env

self.inputs = inputs
if self.inputs is not None: # translate strings into file objects
Expand Down
14 changes: 12 additions & 2 deletions pyccc/tests/test_job_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ def test_abspath_input_files(fixture, request):
def test_directory_input(fixture, request):
engine = request.getfuncargvalue(fixture)

# this is OK with docker but should fail with a subprocess
job = engine.launch(image='alpine', command='cat data/a data/b',
inputs={'data':
pyccc.LocalDirectoryReference(os.path.join(THISDIR, 'data'))})
Expand All @@ -461,7 +460,6 @@ def test_directory_input(fixture, request):
def test_passing_files_between_jobs(fixture, request):
engine = request.getfuncargvalue(fixture)

# this is OK with docker but should fail with a subprocess
job1 = engine.launch(image='alpine', command='echo hello > world')
job1.wait()
assert job1.exitcode == 0
Expand All @@ -471,3 +469,15 @@ def test_passing_files_between_jobs(fixture, request):
job2.wait()
assert job2.exitcode == 0
assert job2.stdout.strip() == 'hello'


@pytest.mark.parametrize('fixture', fixture_types['engine'])
def test_job_env_vars(fixture, request):
engine = request.getfuncargvalue(fixture)

job = engine.launch(image='alpine',
command='echo ${AA} ${BB}',
env={'AA': 'hello', 'BB':'world'})
job.wait()
assert job.exitcode == 0
assert job.stdout.strip() == 'hello world'

0 comments on commit 614c0a0

Please sign in to comment.