diff --git a/CHANGELOG.md b/CHANGELOG.md index cd6b6eb0..d4f83a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/spython/main/build.py b/spython/main/build.py index 33391560..13c8cc65 100644 --- a/spython/main/build.py +++ b/spython/main/build.py @@ -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') diff --git a/spython/main/pull.py b/spython/main/pull.py index af56b3e1..fea9e632 100644 --- a/spython/main/pull.py +++ b/spython/main/pull.py @@ -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, @@ -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 @@ -68,10 +72,20 @@ 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"] @@ -79,9 +93,34 @@ def pull(self, 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) diff --git a/spython/version.py b/spython/version.py index da8c86fd..5e2cc6d1 100644 --- a/spython/version.py +++ b/spython/version.py @@ -18,7 +18,7 @@ -__version__ = "0.0.41" +__version__ = "0.0.42" AUTHOR = 'Vanessa Sochat' AUTHOR_EMAIL = 'vsochat@stanford.edu' NAME = 'spython' @@ -28,4 +28,4 @@ LICENSE = "LICENSE" INSTALL_REQUIRES = ( -) +) \ No newline at end of file