
    5i                     `    d dl Z d dlZd dlmZmZmZ d dlmZ  G d de      Z	 G d de	      Z
y)    N)DXFileconfigupload_local_file)err_exitc                   j    e Zd Z	 	 	 	 ddZed        Zed        Zd Zd Zd Z	e
d        Zd	 Zd
 Zy)ImageRefNc                     t         j                  j                  d|xs d      | _        || _        d| _        || _        || _        || _        || _	        || _
        y)a  
        A class to handle an image reference from nextflow pipeline.
        :param process: An NPA proces name (aka task name) which uses a given image
        :type process: String
        :param digest: An image digest
        :type digest: String
        :param dx_file_id: dx file id on the platform
        :type dx_file_id: Optional[String]
        :param repository: Image repository
        :type repository: Optional[String]
        :param image_name: Image name (usually a basename of the image referenced with repository)
        :type image_name: Optional[String]
        :param tag: A version tag
        :type tag: Optional[String]
        z/.cached_docker_images/ N)ospathjoin_caching_dir_dx_file_id_bundled_depends_repository_image_name_tag_digest_process)selfprocessdigest
dx_file_id
repository
image_nametags          x/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/nextflow/ImageRef.py__init__zImageRef.__init__   sW    0 GGLL)BJDTRTU% $%%	    c                 \    | j                   s| j                         | _         | j                   S N)r   _package_bundler   s    r   bundled_dependszImageRef.bundled_depends,   s(    $$$($8$8$:D!$$$r   c                 ~    | j                  d| j                  | j                  | j                  | j                  g      S N_)_join_if_existsr   r   r   r   r#   s    r   
identifierzImageRef.identifier2   s5    ##C$*:*:D<L<LdiiY]YeYe)fggr   c                     t        d      )a9  
        Function to store an image on the platform as a dx file object. Should be implemented in subclasses.
        :param file_name: A file name under which the image will be saved on the platform
        :type file_name: String
        :returns: Tuple[String, String] dx file id, file name (basename)
        IAbstract class. Method not implemented. Use the concrete implementations.NotImplementedError)r   	file_names     r   _cachezImageRef._cache6   s     ""mnnr   c                     t        d      )Nr+   r,   r#   s    r   _reconstruct_image_refzImageRef._reconstruct_image_ref?   s    !"mnnr   c                 R    | j                  d| j                  | j                  g      S r&   )r(   r   r   r#   s    r   _construct_cache_file_namez#ImageRef._construct_cache_file_nameB   s$    ##C$*:*:DII)FGGr   c                 P    | j                  |D cg c]  }|s|	 c}      S c c}w r!   )r   )	delimiterpartsxs      r   r(   zImageRef._join_if_existsE   s!    ~~%5Q1q5665s   ##c                 z    t        | j                  t        d         }|j                         j	                  d      S )NDX_PROJECT_CONTEXT_IDname)r   r   r   describeget)r   dx_file_handles     r   _dx_file_get_namezImageRef._dx_file_get_nameI   s4     0 0&9P2QR&&(,,V44r   c                     | j                   s'| j                         }| j                  |      | _         n| j                         }|d| j                   idS )z
        Function to include a container image stored on the platform into NPA
        :returns: Dict in the format of {"name": "bundle.tar.gz", "id": {"$dnanexus_link": "file-xxxx"}}
        z$dnanexus_link)r:   id)r   r3   r/   r>   )r   cache_file_names     r   r"   zImageRef._package_bundleM   sU    
 "==?O#{{?;D"446O##T%5%56
 	
r   NNNN)__name__
__module____qualname__r   propertyr$   r)   r/   r1   r3   staticmethodr(   r>   r"    r   r   r   r   
   sp    
  B % %
 h hooH 7 75
r   r   c                   4     e Zd Z	 	 	 	 d fd	Zd Zd Z xZS )DockerImageRefc                 .    t         |   ||||||       y r!   )superr   )r   r   r   r   r   r   r   	__class__s          r   r   zDockerImageRef.__init__^   s$     		r   c           
      L   | j                         }dj                  |      }dj                  ||      }||fD ]  }	 t        j                  |d      } | j                  }| j                  sDdj                  |      }t        j                  |d      j                         j                         }t        |t        d   | j                  |dd| j                  xs |i	      }	|	j                         S # t        j                  $ r t        dj                  |             Y w xY w)
Nzsudo docker pull {}zsudo docker save {} | gzip > {}T)shellz&Failed to run a subprocess command: {}z#docker images --no-trunc --quiet {}r9   image_digest)filenameprojectfolderr:   parents
properties)r1   format
subprocesscheck_outputCalledProcessErrorr   r   decodestripr   r   r   get_id)
r   r.   full_image_refdocker_pull_cmddocker_save_cmdcmdr'   extracted_digest
digest_cmduploaded_dx_files
             r   r/   zDockerImageRef._cacheo   s   446/66~F;BB>S\]#_5 	OCO++Ct<	O  <<||>EEnUJ)66zNUUW]]_,23$$&(H8HI
  &&(( 00 OAHHMNOs   C33-D#"D#c                     | j                  d| j                  | j                  g      }| j                  r | j                  d|| j                  g      }|S | j                  d|| j                  g      }|S )z
        Docker image reference has the form of <REPOSITORY_NAME>/<IMAGE_NAME>:<VERSION_TAG> or
        <REPOSITORY_NAME>/<IMAGE_NAME>@<DIGEST>
        r
   @:)r(   r   r   r   r   )r   repo_and_image_namefull_refs      r   r1   z%DockerImageRef._reconstruct_image_ref   su    
 #2228H8H$JZJZ7[\<<++C2Et||1TUH  ++C2Etyy1QRHr   rB   )rC   rD   rE   r   r/   r1   __classcell__)rM   s   @r   rJ   rJ   ]   s     
 ")0
r   rJ   )r   rW   dxpyr   r   r   dxpy.exceptionsr   objectr   rJ   rH   r   r   <module>rm      s1    
  2 2 $P
v P
f4X 4r   