Skip to content

Commit 20f820b

Browse files
authored
Merge pull request #53 from vsoch/fix/pull
Adding ability to pull based on commit / hash
2 parents 68dda47 + 6d0d25d commit 20f820b

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The client here will eventually be released as "spython" (and eventually to
1717
singularity on pypi), and the versions here will coincide with these releases.
1818

1919
## [master](https://github.com/singularityhub/singularity-cli/tree/master)
20+
- adding name_by_commit and name_by_hash to pull (0.0.42)
2021
- adding nvidia flag as nv argument (with default False) to run/exec (0.0.41)
2122
- fixing bug in shell.py, cli should be client (0.0.40)
2223
- remove uri function should only right strip to support relative paths (0.0.39)

spython/main/build.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,17 @@ def build(self, recipe=None,
7676
if re.search('(docker|shub)://', recipe) and robot_name is False:
7777
image = self._get_filename(recipe, ext)
7878
else:
79-
image = "%s.%s" %(self.RobotNamer.generate(),ext)
79+
image = "%s.%s" %(self.RobotNamer.generate(), ext)
8080

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

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

8888

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

spython/main/pull.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from spython.utils import stream_command
2222
import os
2323
import re
24+
import shutil
2425
import sys
26+
import tempfile
2527

2628
def pull(self,
2729
image=None,
@@ -30,6 +32,8 @@ def pull(self,
3032
ext="simg",
3133
force=False,
3234
capture=False,
35+
name_by_commit=False,
36+
name_by_hash=False,
3337
stream=False):
3438

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

7074
# If we still don't have a custom name, base off of image uri.
71-
if name is None:
72-
name = self._get_filename(image, ext)
75+
# Determine how to tell client to name the image, preference to hash
76+
77+
if name_by_hash is True:
78+
cmd.append('--hash')
79+
80+
elif name_by_commit is True:
81+
cmd.append('--commit')
7382

74-
cmd = cmd + ["--name", name]
83+
elif name is None:
84+
name = self._get_filename(image, ext)
85+
86+
# Only add name if we aren't naming by hash or commit
87+
if not name_by_commit and not name_by_hash:
88+
cmd = cmd + ["--name", name]
7589

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

7993
cmd.append(image)
8094
bot.info(' '.join(cmd))
8195

96+
# If name is still None, make empty string
97+
if name is None:
98+
name = ''
99+
82100
final_image = os.path.join(pull_folder, name)
83-
if stream is False:
101+
102+
# Option 1: For hash or commit, need return value to get final_image
103+
if name_by_commit or name_by_hash:
104+
105+
# Set pull to temporary location
106+
tmp_folder = tempfile.mkdtemp()
107+
self.setenv('SINGULARITY_PULLFOLDER', tmp_folder)
84108
self._run_command(cmd, capture=capture)
109+
110+
try:
111+
tmp_image = os.path.join(tmp_folder, os.listdir(tmp_folder)[0])
112+
final_image = os.path.join(pull_folder, os.path.basename(tmp_image))
113+
shutil.move(tmp_image, final_image)
114+
shutil.rmtree(tmp_folder)
115+
116+
except:
117+
bot.error('Issue pulling image with commit or hash, try without?')
118+
119+
# Option 2: Streaming we just run to show user
120+
elif stream is False:
121+
self._run_command(cmd, capture=capture)
122+
123+
# Option 3: A custom name we can predict (not commit/hash) and can also show
85124
else:
86125
return final_image, stream_command(cmd, sudo=False)
87126

spython/version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020

21-
__version__ = "0.0.41"
21+
__version__ = "0.0.42"
2222
AUTHOR = 'Vanessa Sochat'
2323
AUTHOR_EMAIL = '[email protected]'
2424
NAME = 'spython'
@@ -28,4 +28,4 @@
2828
LICENSE = "LICENSE"
2929

3030
INSTALL_REQUIRES = (
31-
)
31+
)

0 commit comments

Comments
 (0)