Skip to content

Commit

Permalink
Merge pull request #19 from ericdill/remote-script
Browse files Browse the repository at this point in the history
ENH: Prototype remote script download/execute
  • Loading branch information
pelson committed May 4, 2016
2 parents 6b6cf6a + 3035f88 commit 3274784
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
33 changes: 24 additions & 9 deletions conda_execute/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil
import stat
import subprocess
import requests

import conda.api
import conda.lock
Expand Down Expand Up @@ -119,6 +120,17 @@ def execute_within_env(env_prefix, cmd):
return code


def _write_code_to_disk(code):
with tempfile.NamedTemporaryFile(prefix='conda-execute_',
delete=False, mode='w') as fh:
fh.writelines(code)
path = fh.name
log.info('Writing temporary code to {}'.format(path))
# Make the file executable.
os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
return path


def main():
parser = argparse.ArgumentParser(description='Execute a script in a temporary conda environment.')
parser.add_argument('path', nargs='?',
Expand Down Expand Up @@ -159,17 +171,20 @@ def __call__(self, parser, namespace, values, option_string=None):

try:
if args.code:
with tempfile.NamedTemporaryFile(prefix='conda-execute_',
delete=False, mode='w') as fh:
fh.writelines(args.code)
path = fh.name
log.info('Writing temporary code to {}'.format(path))
path = _write_code_to_disk(args.code)
# Queue the temporary file up for cleaning.
exit_actions.append(lambda: os.remove(path))
elif args.path:
# check to see if `args.path` is a remote path, download whatever
# is at that remote location and stash it as args.code.
if args.path.startswith('http'):
# download code from the remote path and write it to disk
code = requests.get(args.path).content.decode()
path = _write_code_to_disk(code)
# Queue the temporary file up for cleaning.
exit_actions.append(lambda: os.remove(path))
# Make the file executable.
os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
elif args.path:
path = os.path.abspath(args.path)
else:
path = os.path.abspath(args.path)
else:
raise ValueError('Either pass the filename to execute, or pipe with -c.')

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ setuptools
pyyaml
conda
psutil
requests
4 changes: 2 additions & 2 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ conda execute $example_script wibble --arg1=123 foobar


cat <<EOF > $tmp_script
#!/usr/bin/env conda-execute
#!/usr/bin/env conda-execute
"""
A script that uses numpy's random normal distribution to print 10 numbers.
Expand All @@ -78,4 +78,4 @@ chmod u+x $tmp_script
$tmp_script



conda execute https://raw.githubusercontent.com/pelson/conda-execute/master/example.sh

0 comments on commit 3274784

Please sign in to comment.