55import subprocess # nosec
66from abc import ABC , abstractmethod
77from pathlib import Path
8- from typing import List , Union
8+ from typing import List , Optional , Union
99
1010from .singularity import get_version as get_singularity_version
1111from .singularity import is_version_2_6 as is_singularity_version_2_6
1717
1818class ImagePuller (ABC ):
1919 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 ,
2125 ) -> None :
2226 """Create an ImagePuller."""
2327 self .req = req
@@ -62,22 +66,24 @@ def generate_udocker_loading_command(self) -> str:
6266
6367 def save_docker_image (self ) -> None :
6468 """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 } ..." )
6670 cmd_pull = [self .cmd , "pull" , self .req ]
6771 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 ())
8187
8288
8389class SingularityImagePuller (ImagePuller ):
@@ -103,8 +109,11 @@ def get_image_name(self) -> str:
103109
104110 def save_docker_image (self ) -> None :
105111 """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
106115 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 ()))
108117 and not self .force_pull
109118 ):
110119 _LOGGER .info (f"Already cached { self .req } with Singularity." )
@@ -119,11 +128,11 @@ def save_docker_image(self) -> None:
119128 cmd_pull .extend (
120129 [
121130 "--name" ,
122- os .path .join (self . save_directory , self .get_image_name ()),
131+ os .path .join (save_directory , self .get_image_name ()),
123132 f"docker://{ self .req } " ,
124133 ]
125134 )
126135 ImagePuller ._run_command_pull (cmd_pull )
127136 _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 ()} "
129138 )
0 commit comments