o
    [h                     @   s   d Z ddlmZ 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dl
mZ eeZed Zd Zd	Zd
ZdejfddZG dd deZdefddZdedefddZdd ZG dd deZdZde de dZ e d Z!defdd Z"dS )!z
Even though this command doesn't require Django to be setup,
it should run after django.setup() just to make sure it doesn't
crash when pushed to Heroku
   )BaseCommand    N)Path)__version__.zi~ .git db.sqlite3 .pyo .pyc .pyd .idea .DS_Store .otreezip venv _static_root staticfiles __pycache__ .envzoTree-may-overwrite-this-filez!oTree-may-not-overwrite-this-filetar_infoc                 C   sf   | j }tD ]
}||r d S qd|v rd S | jd? }|dkr*td| d|  |  jdO  _| S )NZ__temp
   i  zAdding large file (z KB):    )pathEXCLUDED_PATH_ENDINGSendswithsizeloggerinfomode)r   r
   endingkb r   t/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/cli/zip.pyfilter_func   s   

r   c                   @   s    e Zd ZdZdd Zdd ZdS )CommandzZip into an archivec                 K   s   t t d S )N)zip_projectPROJECT_PATH)selfoptionsr   r   r   handle8   s   zCommand.handlec                 C   sB   |  |d |d }||dd }t|}| jdi | dS )z2
        copy-pasted from 'unzip' command
        r   r      Nr   )Zcreate_parser
parse_argsvarsr   )r   argvparserr   Zcmd_optionsr   r   r   run_from_argv;   s   zCommand.run_from_argvN)__name__
__module____qualname__helpr   r!   r   r   r   r   r   5   s    r   project_pathc           	   
   C   s  | j  d}| d }| sd}t| td t|  D ]\}}| |	| q zt
|  W n tyQ } ztt| td W Y d }~nd }~ww | d }|	d t|d}|j| dtd	 W d    n1 stw   Y  |  td
| d d S )Nz	.otreezipzsettings.pyzjCannot find oTree settings. You must run this command from the folder that contains your settings.py file.r   z.python-versionz3.12.5zw:gz )arcnamefilterzSaved your code into file "")nameexistsr   errorsysexitfix_reqs_filesitemsjoinpath
write_textvalidate_reqs_filesRequirementsErrorstrtarfileopenaddr   unlinkr   )	r&   archive_nameZsettings_filemsgfnnew_textexcZruntime_txttarr   r   r   r   F   s.   


r   returnc                 C   s   |  d}|  d}|d}| r|dnd}d}t|v r#d}n(t|vrKtd }|dkr4d}n|d	krC|jd
t d| iS tj	
d d}|r]|jti}| r[t||j< |S i S )Nrequirements.txtrequirements_base.txtutf8r'   FTzDo you want oTree to automatically keep your requirements files up to date?
(Enter 'n' if you have custom requirements in requirements.txt or requirements_base.txt)
(y/n): yn# 
z Answer not recognized; skipping
)r2   	read_textr,   OVERWRITE_TOKENDONT_OVERWRITE_TOKENinputlowerr+   r.   stdoutwriteREQS_DEFAULTREQS_BASE_DEFAULT)r&   rpathrbpathZoriginal_rtxtZoriginal_rbtxtZcan_overwriteZansdr   r   r   r0   n   s2   




r0   c                 C   s2   g }| D ]}|  }|r|ds|| q|S )N#)strip
startswithappend)flinesliner   r   r   get_non_comment_lines   s   
r\   c                   @   s   e Zd ZdS )r5   N)r"   r#   r$   r   r   r   r   r5      s    r5   z\# You should put your requirements in requirements.txt instead.
# You can delete this file.
rG   zp
# IF YOU MODIFY THIS FILE, remove these comments.
# otherwise, oTree will automatically overwrite it.
otree%s==z#
psycopg2>=2.8.4
sentry-sdk>=0.7.9
r'   c                 C   s  | d }| d }|j dd}t|}W d    n1 sw   Y  | rk|j dd}t|}W d    n1 s;w   Y  t }|| D ]"}td|}|ri|d}	|	|v rdd|	 d}
t|
||	 qGnd	}||v r{d
| d}
t|
d|	dvrd}
t|
d S )NrB   rC   rD   )encodingz(^[\w-]+).*?r   r*   zM" is listed more than once in your requirements_base.txt & requirements.txt. z-r requirements_base.txtz,your requirements.txt has a line that says "z". You should remove that line.Zpsycopg2zBYou should add a line to your requirements.txt that says: psycopg2)
r8   r\   r,   setrematchgroupr5   r9   rI   )r&   rR   rS   rY   ZrlinesZrblinesalready_seenlnmpackager<   ZREFERENCE_TO_REQS_BASEr   r   r   r4      s:   




r4   )#__doc__baser   r7   osloggingpathlibr   r.   r_   Zotreer   Zotree_version	getLoggerr"   r   resolver   splitr   rJ   rK   TarInfor   r   r   dictr0   r\   	Exceptionr5   rQ   Z_REQS_DEFAULT_FMTrP   r4   r   r   r   r   <module>   s8    
( 	
	