a
    8XhI                     @   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
   /var/www/viveiro_mudafortebrasil/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std|j	|j
f nr jj|d	} j|d
   fdd jj	j
fD }|g }|D ]jr؈} qq|st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|  sd S |	D ]P}z| jst| W n2 t y0   | j d
kr,| jd|f  Y n0 qt|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 s0    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   
