Skip to content

Commit

Permalink
Merge pull request #53 from vsoch/fix/pull
Browse files Browse the repository at this point in the history
Adding ability to pull based on commit / hash
  • Loading branch information
vsoch committed Sep 9, 2018
2 parents 68dda47 + 6d0d25d commit 20f820b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ The client here will eventually be released as "spython" (and eventually to
singularity on pypi), and the versions here will coincide with these releases.

## [master](https://github.com/singularityhub/singularity-cli/tree/master)
- adding name_by_commit and name_by_hash to pull (0.0.42)
- adding nvidia flag as nv argument (with default False) to run/exec (0.0.41)
- fixing bug in shell.py, cli should be client (0.0.40)
- remove uri function should only right strip to support relative paths (0.0.39)
Expand Down
5 changes: 3 additions & 2 deletions spython/main/build.py
Expand Up @@ -76,16 +76,17 @@ def build(self, recipe=None,
if re.search('(docker|shub)://', recipe) and robot_name is False:
image = self._get_filename(recipe, ext)
else:
image = "%s.%s" %(self.RobotNamer.generate(),ext)
image = "%s.%s" %(self.RobotNamer.generate(), ext)

# Does the user want a custom build folder?
if build_folder is not None:
if not os.path.exists(build_folder):
bot.exit('%s does not exist!' %build_folder)
bot.exit('%s does not exist!' % build_folder)

image = "%s/%s" %(build_folder, image)


# The user wants to run an isolated build
if isolated is True:
cmd.append('--isolated')

Expand Down
47 changes: 43 additions & 4 deletions spython/main/pull.py
Expand Up @@ -21,7 +21,9 @@
from spython.utils import stream_command
import os
import re
import shutil
import sys
import tempfile

def pull(self,
image=None,
Expand All @@ -30,6 +32,8 @@ def pull(self,
ext="simg",
force=False,
capture=False,
name_by_commit=False,
name_by_hash=False,
stream=False):

'''pull will pull a singularity hub or Docker image
Expand Down Expand Up @@ -68,20 +72,55 @@ def pull(self,
self.setenv('SINGULARITY_PULLFOLDER', pull_folder)

# If we still don't have a custom name, base off of image uri.
if name is None:
name = self._get_filename(image, ext)
# Determine how to tell client to name the image, preference to hash

if name_by_hash is True:
cmd.append('--hash')

elif name_by_commit is True:
cmd.append('--commit')

cmd = cmd + ["--name", name]
elif name is None:
name = self._get_filename(image, ext)

# Only add name if we aren't naming by hash or commit
if not name_by_commit and not name_by_hash:
cmd = cmd + ["--name", name]

if force is True:
cmd = cmd + ["--force"]

cmd.append(image)
bot.info(' '.join(cmd))

# If name is still None, make empty string
if name is None:
name = ''

final_image = os.path.join(pull_folder, name)
if stream is False:

# Option 1: For hash or commit, need return value to get final_image
if name_by_commit or name_by_hash:

# Set pull to temporary location
tmp_folder = tempfile.mkdtemp()
self.setenv('SINGULARITY_PULLFOLDER', tmp_folder)
self._run_command(cmd, capture=capture)

try:
tmp_image = os.path.join(tmp_folder, os.listdir(tmp_folder)[0])
final_image = os.path.join(pull_folder, os.path.basename(tmp_image))
shutil.move(tmp_image, final_image)
shutil.rmtree(tmp_folder)

except:
bot.error('Issue pulling image with commit or hash, try without?')

# Option 2: Streaming we just run to show user
elif stream is False:
self._run_command(cmd, capture=capture)

# Option 3: A custom name we can predict (not commit/hash) and can also show
else:
return final_image, stream_command(cmd, sudo=False)

Expand Down
4 changes: 2 additions & 2 deletions spython/version.py
Expand Up @@ -18,7 +18,7 @@



__version__ = "0.0.41"
__version__ = "0.0.42"
AUTHOR = 'Vanessa Sochat'
AUTHOR_EMAIL = 'vsochat@stanford.edu'
NAME = 'spython'
Expand All @@ -28,4 +28,4 @@
LICENSE = "LICENSE"

INSTALL_REQUIRES = (
)
)

0 comments on commit 20f820b

Please sign in to comment.