a
    ÆZEgI  ã                   @   sr   d dl Z d dlZd dlZd dlmZmZ d dlmZmZ d dl	m
Z
mZ e d¡ZdZdd„ ZG d	d
„ d
eƒZdS )é    N)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIASÚconnections)ÚAmbiguityErrorÚMigrationLoaderz\s+replaces\s*=\s*\[[^\]]+\]\s*z.pycc                 C   s   | d t tƒ … d S )Nz.py)ÚlenÚPYC)Zpyc_fn© r
   úy/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/management/commands/delete_squashed_migrations.pyÚpy_from_pyc   s    r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCommandz:Deletes left over migrations that have been replaced by a c                 C   s`   |j ddd� |j dd ddd� |j dd	d
dddd� |j ddddd� |j dtdt d� d S )NÚ	app_labelz@App label of the application to delete replaced migrations from.)ÚhelpÚsquashed_migration_nameú?zPThe squashed migration to replace. If not specified defaults to the first found.)ÚdefaultÚnargsr   z	--noinputz
--no-inputÚstore_falseÚinteractiveTz:Tells Django to NOT prompt the user for input of any kind.)ÚactionÚdestr   r   z	--dry-runÚ
store_trueFz*Do not actually delete or change any files)r   r   r   z
--databasezGNominates a database to run command for. Defaults to the "%s" database.)r   r   )Úadd_argumentr   )ÚselfÚparserr
   r
   r   Úadd_arguments   s(    þþ
þþþzCommand.add_argumentsc              
      sÄ  |d | _ |d | _|d | _|d }|d }|d }tt| ƒ‰ |ˆ jvrXtd| ƒ‚d }|rŠ|  ˆ ||¡}|jsütd|j	|j
f ƒ‚nrˆ jj|d	�}ˆ j|d
 Ž ‰‡ ‡fdd„ˆ j ˆj	ˆj
f¡D ƒ}|ˆg }|D ]‰ˆjrØˆ} qìqØ|sütd| ƒ‚g }	|jD ]†\}
}zˆ j|
|f ‰W n4 t�yT   | j d
k�rP| j d|
|f ¡ Y n60 t ˆj¡}|	 |¡ | t¡�rt|ƒ}|	 |¡ �q| j d
k�s¢| j�rà| j | j d¡¡ |	D ]}| j d| ¡ �qº|  ¡ �sàd S |	D ]P}z| j�süt |¡ W n2 t �y0   | j d
k�r,| j d|f ¡ Y n0 �qät |j¡}| t¡�rVt|ƒ}t!|ƒ�}t"|ƒ}W d   ƒ n1 �s~0    Y  g }t#|ƒD ]V\}}t$ %|¡�r”| |¡ |d
k�râ||d   &¡ dk�râ| 'd
|d ¡  �qì�q”|�s td|f ƒ‚| j d
k�s| j�rb| j | j d|d
 t(|ƒdk�rFdt)|d ƒ nd|f ¡¡ |  ¡ �sbd S t*|dd�D ]}||= �qnt!|dƒ�(}| j�s | d +|¡¡ W d   ƒ n1 �s¶0    Y  d S )NÚ	verbosityr   Údry_runr   r   ÚdatabasezVApp '%s' does not have migrations (so delete_squashed_migrations on it makes no sense)z0The migration %s %s is not a squashed migration.)Zappr   c                    s&   g | ]\}}|ˆj krˆ  ||¡‘qS r
   )r   Úget_migration)Ú.0ÚalÚmn©ÚloaderZ	migrationr
   r   Ú
<listcomp>H   s   
þz"Command.handle.<locals>.<listcomp>z-Cannot find a squashed migration in app '%s'.z'Couldn't find migration file for %s %s
z Will delete the following files:z - %szCouldn't delete %s
é   Ú zOCouldn't find 'replaces =' line in file %s. Please finish cleaning up manually.z"Will delete line %s%s from file %sz and T)ÚreverseÚw),r   r   r   r   r   Zmigrated_appsr   Úfind_migrationZreplacesr   ÚnameÚgraphÚ
leaf_nodesr    Zforwards_planZdisk_migrationsÚKeyErrorÚstderrÚwriteÚinspectÚgetfileÚ	__class__ÚappendÚendswithr	   r   ÚstdoutÚstyleZMIGRATE_HEADINGÚconfirmÚosÚremoveÚOSErrorÚopenÚlistÚ	enumerateÚREPLACES_REGEXÚmatchÚstripÚinsertr   ÚstrÚsortedÚjoin)r   Úoptionsr   r   r   Zsquashed_migrationr.   Zprevious_migrationsZ
migrationsZfiles_to_deleter"   r#   Zpyc_fileZpy_fileÚfnZsquashed_migration_fnÚfpZsquashed_migration_linesZdelete_linesÚiÚlineÚline_numr
   r$   r   Úhandle-   sÆ    



ÿÿ
ÿÿþ
ÿÿÿ


(
 
ÿÿ þÿÿ

zCommand.handlec                 C   sD   | j r@d }|r|dvr8tdƒ}|s*d}q8q
|d  ¡ }q
|dkS dS )NZynzDo you wish to proceed? [yN] Únr   ÚyT)r   ÚinputÚlower)r   Zanswerr
   r
   r   r9   ž   s    zCommand.confirmc                 C   sV   z|  ||¡W S  ty0   td||f ƒ‚Y n" tyP   td||f ƒ‚Y n0 d S )NzJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.)Zget_migration_by_prefixr   r   r/   )r   r%   r   r,   r
   r
   r   r+   «   s    ÿÿÿÿzCommand.find_migrationN)Ú__name__Ú
__module__Ú__qualname__r   r   rM   r9   r+   r
   r
   r
   r   r      s
   qr   )r:   r2   ÚreZdjango.core.management.baser   r   Z	django.dbr   r   Zdjango.db.migrations.loaderr   r   Úcompiler@   r	   r   r   r
   r
   r
   r   Ú<module>   s   
