
    (tfF!                         d dl Z d dlZd dlZd dlmZ d dlZd dlZdZdZ	dZ
dZdZd Zd Zd	 Zd
 Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Z G d de      Zy)    Nzhiglass-manage-containerzhiglass-manage-networkzhiglass-manage-redisz/usr/local/etc/redis/redis.confz
db.sqlite3c                     t        j                         }t        | d      5 t        fdd      D ]  }|j	                  |        	 d d d        |j                         S # 1 sw Y   |j                         S xY w)Nrbc                  &     j                  d      S )Ni   )read)fs   _/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/higlass_manage/common.py<lambda>zmd5.<locals>.<lambda>   s    !&&,     r
   )hashlibmd5openiterupdate	hexdigest)fnamehash_md5chunkr   s      @r   r   r      so    {{}H	eT	 #a.4 	#EOOE"	## # s   &A!!A9c                 .    dj                  t        |       S Nz{}-{})formatCONTAINER_PREFIXhg_names    r   hg_name_to_container_namer      s    >>*G44r
   c                 .    dj                  t        |       S r   )r   NETWORK_PREFIXr   s    r   hg_name_to_network_namer      s    >>.'22r
   c                 .    dj                  t        |       S r   )r   REDIS_PREFIXr   s    r   hg_name_to_redis_namer       s    >>,00r
   c                     t        j                         }t        |       }|j                  j	                  |      }|d   d   d   d   d   }|S )N
HostConfigPortBindingsz80/tcpr   HostPortdockerfrom_envr   apiinspect_container)r   clientcontainer_nameconfigports        r   get_portr.   #   sO    __F.w7NZZ)).9F,/9!<ZHDKr
   c                 >   t        j                         }t        |       }|j                  j	                  |      }|d   d   D cg c]	  }||v s| }}|syt        |      dkD  rt        dj                  |            |\  }|j                  d      \  }}|S c c}w )z
    get SITE_URL for a given container
    using container docker-config, assuming
    there is not more than one SITE_URL in
    container's env.

    Yields "localhost" when no SITE_URL entries
    detected.
    ConfigEnvzhttp://localhost   z+There are multiple SITE_URL entry in {} env=)	r&   r'   r   r(   r)   len
ValueErrorr   split)	r   	_SITE_URLr*   r+   r,   ssite_url_entriessite_url_s	            r   get_site_urlr<   -   s     __F.w7NZZ)).9F#)(#3E#:Mai1nMM!			"9@@P
 	
 'nnS)8 Ns   	BBc                    |\t        |       }t        d|       |Ct        |       }t        dt        j                         |t        dj                  |             y|[t        |      }t        d|       |Bt        |      }t        dt        j                         |t        dj                  |             ||fS )	a  
    If no filetype or datatype are provided, add them
    based on the given filename.

    Parameters:
    ----------
    filename: str
        The name of the file
    filetype: str
        The type of the file (can be None)
    datatype: str
        The datatype for the data in the file (can be None)

    Returns:
    --------
    (filetype, datatype): (str, str)
        Filled in filetype and datatype based on the given filename
    zInferred filetype:z<Unknown filetype, please specify using the --filetype optionfilez;Based on the filename, you may want to try the filetype: {}NNzInferred datatype:z<Unknown datatype, please specify using the --datatype optionz;Based on the filetype, you may want to try the datatype: {})infer_filetypeprintrecommend_filetypesysstderrr   infer_datatyperecommend_datatype)filenamefiletypedatatyperecommended_filetyperecommended_datatypes        r   fill_filetype_and_datatyperM   M   s    & !(+"H-#5h#? NZZ $/QXX,  !(+"H-#5h#? NZZ $/QXX, hr
   c                     t        j                  |       }t        j                  |       d   dk(  ryt        j                  |       d   dk(  ryy )Nr2   z.bedbedfilez.bedpebedpe)opsplitext)rH   exts     r   rC   rC      sG    
++h
C	{{8Q6)	{{8Q8+ ,r
   c                     | dk(  ryy )NrO   bedlike rI   s    r   rG   rG      s    9 r
   c                 L    | dk(  ry| dk(  ry| dk(  ry| dk(  ry| d	k(  ry
| dk(  ryy)Nmatrix)heatmapcentervector)zhorizontal-bartopzgene-annotations)zhorizontal-gene-annotationsr]   
chromsizes)zhorizontal-chromosome-labelsr]   2d-rectangle-domains)r_   r[   rU   )rU   r]   r@   rV   )rJ   s    r   datatype_to_tracktyper`      sI    8$	X	(	'	'5	\	!6	+	+1	Y	!r
   c                 H   t        j                  |       \  }}|j                         dk(  s|j                         dk(  ry|j                         dk(  s|j                         dk(  ry|j                         dk(  ry|j                         d	k(  ry
|j                         dk(  ryy )Nz.bwz.bigwigbigwigz.mcoolz.coolcoolerz.htimetime-interval-jsonz.hitilehitilez.beddbbeddb)rQ   rR   lower)rH   r;   rS   s      r   rA   rA      s    [["FAs
yy{esyy{i7		 CIIK7$:		 #			!		 r
   c                 @    | dk(  ry| dk(  ry| dk(  ry| dk(  ry| dk(  ry	y )
Nrc   rY   rb   r\   rd   ztime-intervalre   rf   rU   rV   rW   s    r   rF   rF      s?    88''87 r
   c	                    |st        |       }	t        j                  |	      st        j                  |	       t        j
                  |      d   }
t        j                  |	|
      }||k7  rOt        j                  |      r!t        d|       t        j                  |       t        j                  ||       n|}
|dj                  |      nd}|dj                  |      nd}|dj                  |      nd}t        dj                  |             t        j                         }t        d|        t        |       }|j                  j                  |      }|r;d	d
j                  |
j!                  dd            z   dj                  |||||      z   }n:d	dj                  |
j!                  dd            z   dj                  |||||      z   }||dj                  |      z  }n(t#        j$                         }|dj                  |      z  }t        d|       |j'                  |      \  }}|dk7  r,t        d|j)                  d      t*        j,                         y |S )Nr2   z(Removing existing file in temporary dir:z--coordSystem {} z--name "{}"z--project-name "{}"zname_text: {}zhg_name:z9python higlass-server/manage.py ingest_tileset --filenamez {} z\ z1 --filetype {} --datatype {} {} {} {} --no-uploadz /tmp/{}z% --filetype {} --datatype {} {} {} {}z	 --uid {}zcommand:r   zERROR:utf8r>   )get_temp_dirrQ   existsosmakedirsr6   joinrB   removelinkr   r&   r'   r   
containersgetreplaceslugidniceexec_rundecoderD   rE   )r   filepathrI   rJ   assemblynameuid	no_uploadproject_nametemp_dirrH   to_import_pathcoordSystem	name_textproject_name_textr*   r+   	containercommand	exit_codeoutputs                        r   import_filer      s8    (yy"KK!88H%a(84X% yy(@.Q		.)GGHn-9A9M$++H5SUK.2.>$$T*BI6B6N$$\2TV  
/
 
 
+,__F	*g.w7N!!%%n5IGll8++C678AHH(I/@+ 	 H 0 0d ;<=5<<(I/@+ 	 ;%%c**kkm;%%c**	*g#,,W5YA~hf-CJJ?Jr
   c                     t        j                         }t        |       }|j                  j	                  |      }t        d|d   d          |d   d   dk7  r
t               |d   D ]  }|d   dk(  s|d   c S  y )	NstateStateRunningTMountsDestinationz/tmpSource)r&   r'   r   r(   r)   rB   HiGlassNotRunningExceptionr   r*   r+   r,   mounts        r   rm   rm     s    __F.w7NZZ)).9F	'6'?9-.gy!T)(**! #6)?"#r
   c                     t        j                         }t        |       }|j                  j	                  |      }|d   D ]  }|d   dk(  s|d   c S  y )Nr   r   z/datar   r%   r   s        r   get_data_dirr     sW    __F.w7NZZ)).9F! #7*?"#r
   c                       e Zd Zy)r   N)__name__
__module____qualname__rV   r
   r   r   r   $  s    r
   r   )SITE_URL)r&   r   ro   os.pathpathrQ   rw   rD   r   r   r   
REDIS_CONFSQLITEDBr   r   r   r    r.   r<   rM   rC   rG   r`   rA   rF   r   rm   r   	Exceptionr   rV   r
   r   <module>r      s      	   
- )%.
 531@9 x
""
CL##	 	r
   