o
    [h"                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ ddlmZ d dlmZ dd	lmZ d d
lmZ e eZeZdZG dd deZdedefddZdZ dZ!dZ"defddZ#G dd dZ$dZ%dZ&dee$ fddZ'dS )    N)Path)TemporaryDirectory)sleep)Optional   )unzip)send_termination_notice)BaseCommand)check_update_neededZ8000c                   @   s   e Zd Zdd Zdd ZdS )Commandc                 C   s   |j ddd d S )Nzipfile?)nargs)add_argument)selfparser r   z/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/cli/zipserver.pyadd_arguments   s   zCommand.add_argumentsc                 K   sd   | d}z"|rt|}nt }|dk r tt |  W d S t| W d S  ty1   Y d S w )Nr   r   )	getrun_single_zipfileautoreload_for_new_zipfilesoskillgetpidsysexitKeyboardInterrupt)r   optionsr   	exit_coder   r   r   handle   s   

zCommand.handleN)__name__
__module____qualname__r   r    r   r   r   r   r      s    r   fnreturnc                 C   s$   t t| }|  |  | S N)Projectr   unzip_to_tempdirstartwait)r$   projectr   r   r   r   .   s   r   z7No *.otreezip file found in this folder yet, waiting...zNewer project foundz
Running {}c               	   C   s  d } t  }d }|stt 	 t  }|rntd qg }zY	 |r!|}tt|  |  |r8||d j	 |
|j |  	 | } | d kr[| W |  |D ]}|  qSS td t  }|rs||krs|}tt |  nqCq|  |D ]}|  q{w )NTr   )get_newest_projectstdout_writeMSG_NO_OTREEZIP_YETr   MSG_RUNNING_OTREEZIP_NAMEformatzipnamer(   take_db_from_previousnameappendtmpdirr)   pollr*   cleanupMSG_FOUND_NEWER_OTREEZIP	terminate)r   r+   Znewer_projectZtempdirstdZlatest_projectr   r   r   r   <   sV   

r   c                   @   s   e Zd ZU dZeed< ejed< defddZ	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd ZdefddZdd ZdefddZdd ZdS )r'   Nr6   _procotreezipc                 C   s
   || _ d S r&   	_otreezip)r   r=   r   r   r   __init__r      
zProject.__init__c                 C   s   | j jS r&   )r?   r4   r   r   r   r   r2   u   s   zProject.zipnamec                 C   s   | j  jS r&   )r?   statst_mtimerB   r   r   r   mtimex      zProject.mtimec                 C   s   | j |j kS r&   r>   )r   otherr   r   r   __eq__{   rF   zProject.__eq__c                 C   s"   t  | _tt| j| jj d S r&   )r   r6   r   strr?   r4   rB   r   r   r   r(   ~   s   zProject.unzip_to_tempdirc                 C   s.   |    tjddtg| jjtj d| _	d S )NZotreeZdevserver_inner)cwdenv)
r
   
subprocessPopenPORTr6   r4   r   environcopyr<   rB   r   r   r   r)      s   zProject.startc                 C   s   | j   d S r&   )r?   unlinkrB   r   r   r   delete_otreezip   s   zProject.delete_otreezipc                 C   
   | j  S r&   )r<   r7   rB   r   r   r   r7      rA   zProject.pollr%   c                 C   rS   r&   )r<   r*   rB   r   r   r   r*      rA   zProject.waitc                 C   s"   t t}| j  t|d d S )N	   )r   rN   r<   r:   r   r   )r   Z	child_pidr   r   r   r:      s   
zProject.terminateother_tmpdirc                 C   s6   dD ]}t || }| rtt|| jj qd S )N)z
db.sqlite3)r   existsshutilmoverI   r6   r4   )r   rU   itemZ	item_pathr   r   r   r3      s   zProject.take_db_from_previousc                 C   s,   t t| jjd}|rt| dS dS )a<  
        The main need to check if requirements.txt matches the current version
        is for oTree Studio users, since they have no way to control what version
        is installed on the server. we instead need the otreezip file to tell
        their local installation what version to use.

        We used to check if an update was needed for any otree command (devserver etc),
        but i think putting it here is more targeted with a clearer scenario.
        other cases are not really essential and there are already other ways
        to handle those.
        zrequirements.txtN)r
   r   r6   r4   joinpathloggerwarning)r   r\   r   r   r   r
      s   zProject.check_update_needed)r!   r"   r#   r6   r   __annotations__rL   rM   r   r@   r2   rE   rH   r(   r)   rR   r7   intr*   r:   rI   r3   r
   r   r   r   r   r'   n   s   
 
r'   
   zDeleting old file: {}c                  C   sj   dd t ddD } | sd S t| dd dd}|d	 }|td  D ]}tt|  |  q#|S )
Nc                 S   s   g | ]}t |qS r   )r'   ).0pathr   r   r   
<listcomp>   s    z&get_newest_project.<locals>.<listcomp>.z
*.otreezipc                 S   s   |   S r&   )rE   )Zprojr   r   r   <lambda>   s    z$get_newest_project.<locals>.<lambda>T)keyreverser   )	r   globsortedMAX_OTREEZIP_FILESr.   MSG_DELETING_OLD_OTREEZIPr1   r2   rR   )ZprojectsZsorted_projectsZnewest_projectZold_projr   r   r   r-      s   
r-   )(loggingr   Zos.pathrW   rL   r   pathlibr   tempfiler   timer   typingr    r   Z
otree.mainr   baser	   Zotree.updater
   	getLoggerr!   r[   printr.   rN   r   rI   r^   r   r/   r9   r0   r   r'   ri   rj   r-   r   r   r   r   <module>   s8    
	2H