3
gI                 @   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j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/tester-filtro-web/env/lib/python3.6/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krXtd| d }|r| j ||}|jstd|j	|j
f nv jj|d	} j|d
   fdd jjj	j
fD }|g }x|D ]jrڈ}P qW |s td| g }	x|jD ]\}
}y j|
|f W n6 tk
r\   | j d
krX| jjd|
|f  Y n6X tjj}|	j| |jtrt|}|	j| qW | j d
ks| jr| jj| jjd x|	D ]}| jjd|  qW | j sd S xZ|	D ]R}y| jstj| W n4 t k
rB   | j d
kr>| jjd|f  Y nX qW tj|j}|jtrjt|}t!|}t"|}W d Q R X g }x^t#|D ]R\}}t$j%|r|j| |d
kr||d  j& dkr|j'd
|d  P qW |std|f | j d
ks| jr`| jj| jjd|d
 t(|dkrDdt)|d  nd|f  | j s`d S xt*|ddD ]}||= qnW t!|d}| js|jdj+| W d Q R X 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 j||qS r
   )r   get_migration).0almn)loader	migrationr
   r   
<listcomp>I   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%   r   handle-   s    











 
 
zCommand.handlec             C   sJ   | j rFd }x2| s|dkr<td}|s.d}P q|d j }qW |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   sX   y|j ||S  tk
r0   td||f Y n$ tk
rR   td||f Y nX 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   
