o
    Nrf                     @   s   d dl Z d dlZd dlmZ d dlZd dlZd dlZddlm	Z	m
Z
mZmZmZ ddlmZ e ejddddejd	d
ddejddddedejddddejdddddejddddedejddde deddd ZdS )    N   )get_data_dirget_site_urlget_porthg_name_to_container_nameSQLITEDB)_stopz--old-hg-namezCThe name of the running higlass container that needs to be updated.F)helprequiredz--old-site-urlz_site-url at the old location. Provide this when higlass container to be updated is not running.z
--old-portz[port at the old location. Provide this when higlass container to be updated is not running.80)r	   r
   defaulttypez--old-data-dirz|data directory of the higlass that is to be updated (usually 'hg-data'). Provide this when higlass container is not running.z--new-site-urlzhttp://localhostzsite-url at the new location.T)r   r	   r
   z
--new-portzport at the new locationz--db-backup-namezname of the database (db) backup file. db backup will be stored in the data directory provided explicitly or inferred from the running container.z.updatedc                 C   s  | dur1zt | }t| }t| }W n* tjjy0 } ztjd|  d W Y d}~nd}~ww |du s9|du r=t	d|dkrC|n| d| }|dkrP|n| d| }	t
|t}
t
||}| durot| gddd zJzt|
| W n ttfy   tjd|
 d	|  td
 Y nw W | durtjd|  d tj  t }t| }|j|  n%| durtjd|  d tj  t }t| }|j|  w w d}zt|}W n# tjy } ztjd| d td
 W Y d}~nd}~ww d| d|	 d}| | }|| W d   n	1 s,w   Y  |  tjd| dt d tj  td dS )aQ  Update stored viewconfs from one host to another

    The script allows one to update viewconfs saved
    in an existing higlass database. It does so
    by modifying references to tilesets that use
    old-site-url:old-port --> new-site-url:new-port

    old/new-site-urls must include schema (http, https):
    http://localhost
    http://old.host.org
    ...

    if 'old-hg-name' is provided and higlass is running,
    then 'old-site-url,old-port,old-data-dir' are inferred.

    if 'old-hg-name' is NOT provided
    then at least 'old-site-url'and 'old-data-dir'
    are required. This scenario should be invoked
    only when the container is not running, otherwise
    integrity of the DB-backup can not be guaranteed.

    Post 80 is default http port and both
    new-port and old-port defaults to it,
    if not specified otherwise.
    site-url:80 is equivalent to site-url

    Script keeps existing database unchanged,
    but modifies a backed up version located
    in the same path as the original one.

    Running higlass-container would be interrupted
    during database backup.

    NzInstance not running: 
zlold-site-url and old-data-dir must be provided, when instance is not running and no old-hg-name is provided
r   :FzFailed to copy z to zRestarting container z ...
zFailed to connect to zS
            UPDATE tilesets_viewconf
            SET viewconf = replace(viewconf,'z','z')
            z"Backed up version of the database z
 has been updated and ready for migration

 copy it to the new host along with the media folder
 rename the database file back to z and restart higlass.
r   )r   r   r   dockererrorsZNotFoundsysstderrwrite
ValueErroropjoinr   r   shutilcopyfileOSErrorIOErrorexitflushZfrom_envr   
containersgetZrestartsqlite3connectErrorcursorexecuteclose)Zold_hg_nameZold_site_urlZold_portZold_data_dirZnew_site_urlZnew_portZdb_backup_nameexorigindestinationZorigin_db_pathZupdate_db_pathclientZcontainer_nameconneZdb_querycur r.   h/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/higlass_manage/update_viewconfs.pyupdate_viewconfs   s   b 


r0   )r   ZclickZos.pathpathr   r!   r   r   commonr   r   r   r   r   stopr   commandoptionstrr0   r.   r.   r.   r/   <module>   sh    	
