5
5
import subprocess # nosec
6
6
from abc import ABC , abstractmethod
7
7
from pathlib import Path
8
- from typing import List , Union
8
+ from typing import List , Optional , Union
9
9
10
10
from .singularity import get_version as get_singularity_version
11
11
from .singularity import is_version_2_6 as is_singularity_version_2_6
17
17
18
18
class ImagePuller (ABC ):
19
19
def __init__ (
20
- self , req : str , save_directory : Union [str , Path ], cmd : str , force_pull : bool
20
+ self ,
21
+ req : str ,
22
+ save_directory : Optional [Union [str , Path ]],
23
+ cmd : str ,
24
+ force_pull : bool ,
21
25
) -> None :
22
26
"""Create an ImagePuller."""
23
27
self .req = req
@@ -62,22 +66,24 @@ def generate_udocker_loading_command(self) -> str:
62
66
63
67
def save_docker_image (self ) -> None :
64
68
"""Download and save the software container image to disk as a docker tarball."""
65
- _LOGGER .info (f"Pulling { self .req } with Docker ..." )
69
+ _LOGGER .info (f"Pulling { self .req } with { self . cmd } ..." )
66
70
cmd_pull = [self .cmd , "pull" , self .req ]
67
71
ImagePuller ._run_command_pull (cmd_pull )
68
- dest = os .path .join (self .save_directory , self .get_image_name ())
69
- if self .force_pull :
70
- os .remove (dest )
71
- cmd_save = [
72
- self .cmd ,
73
- "save" ,
74
- "-o" ,
75
- dest ,
76
- self .req ,
77
- ]
78
- subprocess .run (cmd_save , check = True ) # nosec
79
- _LOGGER .info (f"Image successfully pulled: { dest !r} ." )
80
- print (self .generate_udocker_loading_command ())
72
+ _LOGGER .info (f"Image successfully pulled: { self .req } " )
73
+ if self .save_directory :
74
+ dest = os .path .join (self .save_directory , self .get_image_name ())
75
+ if self .save_directory and self .force_pull :
76
+ os .remove (dest )
77
+ cmd_save = [
78
+ self .cmd ,
79
+ "save" ,
80
+ "-o" ,
81
+ dest ,
82
+ self .req ,
83
+ ]
84
+ subprocess .run (cmd_save , check = True ) # nosec
85
+ _LOGGER .info (f"Image successfully saved: { dest !r} ." )
86
+ print (self .generate_udocker_loading_command ())
81
87
82
88
83
89
class SingularityImagePuller (ImagePuller ):
@@ -103,8 +109,11 @@ def get_image_name(self) -> str:
103
109
104
110
def save_docker_image (self ) -> None :
105
111
"""Pull down the Docker software container image and save it in the Singularity image format."""
112
+ save_directory : Union [str , Path ]
113
+ if self .save_directory :
114
+ save_directory = self .save_directory
106
115
if (
107
- os .path .exists (os .path .join (self . save_directory , self .get_image_name ()))
116
+ os .path .exists (os .path .join (save_directory , self .get_image_name ()))
108
117
and not self .force_pull
109
118
):
110
119
_LOGGER .info (f"Already cached { self .req } with Singularity." )
@@ -119,11 +128,11 @@ def save_docker_image(self) -> None:
119
128
cmd_pull .extend (
120
129
[
121
130
"--name" ,
122
- os .path .join (self . save_directory , self .get_image_name ()),
131
+ os .path .join (save_directory , self .get_image_name ()),
123
132
f"docker://{ self .req } " ,
124
133
]
125
134
)
126
135
ImagePuller ._run_command_pull (cmd_pull )
127
136
_LOGGER .info (
128
- f"Image successfully pulled: { self . save_directory } /{ self .get_image_name ()} "
137
+ f"Image successfully pulled: { save_directory } /{ self .get_image_name ()} "
129
138
)
0 commit comments