ó
¸9—^c           @   sª   d  d l  Z  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 „  Z d e f d	 „  ƒ  YZ d S(
   iÿÿÿÿN(   t   BaseCommandt   CommandError(   t   DEFAULT_DB_ALIASt   connections(   t   AmbiguityErrort   MigrationLoaders   \s+replaces\s*=\s*\[[^\]]+\]\s*s   .pycc         C   s   |  t  t ƒ  d S(   Ns   .py(   t   lent   PYC(   t   pyc_fn(    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyt   py_from_pyc   s    t   Commandc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s:   Deletes left over migrations that have been replaced by a c         C   s}   | j  d d d ƒ| j  d d d  d d d d ƒ| j  d	 d
 d d d d d t d d ƒ| j  d d d d t d d ƒd  S(   Nt	   app_labelt   helps@   App label of the application to delete replaced migrations from.t   squashed_migration_namet   defaultt   nargst   ?sP   The squashed migration to replace. If not specified defaults to the first found.s	   --noinputs
   --no-inputt   actiont   store_falset   destt   interactives:   Tells Django to NOT prompt the user for input of any kind.s	   --dry-runt
   store_trues*   Do not actually delete or change any files(   t   add_argumentt   Nonet   Truet   False(   t   selft   parser(    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyt   add_arguments   s    c      	   K   sâ  | d |  _  | d |  _ | d |  _ | d } | d } t t t ƒ } | | j k rm t d | ƒ ‚ n  d  } | r¹ |  j	 | | | ƒ } | j
 s}t d | j | j f ƒ ‚ q}nÄ | j j d | ƒ } | j | d	 Œ  } g  | j j | j | j f ƒ D]- \ } }	 | | j k r | j | |	 ƒ ^ q }
 |
 | g } x! | D] } | j
 rG| } PqGqGW| s}t d
 | ƒ ‚ n  g  } x¸ | j
 D]­ \ } }	 y | j | |	 f } Wn= t k
 rï|  j  d	 k r:|  j j d | |	 f ƒ q:qXt j | j ƒ } | j | ƒ | j t ƒ rt | ƒ } | j | ƒ qqW|  j  d	 k sV|  j rª|  j j |  j j d ƒ ƒ x" | D] } |  j j d | ƒ qyW|  j ƒ  sªd  Sn  xh | D]` } y |  j sÓt  j! | ƒ n  Wq±t" k
 r|  j  d	 k r|  j j d | f ƒ qq±Xq±Wt j | j ƒ } | j t ƒ rEt | ƒ } n  t# | ƒ  } t$ | ƒ } Wd  QXg  } xw t% | ƒ D]i \ } } t& j' | ƒ ry| j | ƒ | d	 k rÞ| | d j( ƒ  d k rÞ| j) d	 | d ƒ n  PqyqyW| st d | f ƒ ‚ n  |  j  d	 k s|  j r€|  j j |  j j d | d	 t* | ƒ d k r\d t+ | d ƒ n d | f ƒ ƒ |  j ƒ  s€d  Sn  x! t, | d t- ƒD] } | | =q“Wt# | d ƒ ) } |  j sØ| j d j. | ƒ ƒ n  Wd  QXd  S(   Nt	   verbosityR   t   dry_runR   R   sV   App '%s' does not have migrations (so delete_squashed_migrations on it makes no sense)s0   The migration %s %s is not a squashed migration.t   appi    s-   Cannot find a squashed migration in app '%s'.s'   Couldn't find migration file for %s %s
s    Will delete the following files:s    - %ss   Couldn't delete %s
i   t    sO   Couldn't find 'replaces =' line in file %s. Please finish cleaning up manually.s"   Will delete line %s%s from file %ss    and t   reverset   w(/   R   R   R   R   R   R   t   migrated_appsR   R   t   find_migrationt   replacesR   t   namet   grapht
   leaf_nodest   get_migrationt   forwards_plant   disk_migrationst   KeyErrort   stderrt   writet   inspectt   getfilet	   __class__t   appendt   endswithR   R	   t   stdoutt   stylet   MIGRATE_HEADINGt   confirmt   ost   removet   OSErrort   opent   listt	   enumeratet   REPLACES_REGEXt   matcht   stript   insertR   t   strt   sortedR   t   join(   R   t   optionsR   R   t   loadert   squashed_migrationR(   t	   migrationt   alt   mnt   previous_migrationst
   migrationst   files_to_deletet   pyc_filet   py_filet   fnt   squashed_migration_fnt   fpt   squashed_migration_linest   delete_linest   it   linet   line_num(    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyt   handle+   s¬    

	('		"&)	c         C   si   |  j  re d  } xI | s% | d k rZ t j j d ƒ } | sG d } Pq | d j ƒ  } q W| d k St S(   Nt   yns   Do you wish to proceed? [yN] t   ni    t   y(   R   R   t   sixt   movest   inputt   lowerR   (   R   t   answer(    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyR7   ›   s    	
c         C   sh   y | j  | | ƒ SWnM t k
 r= t d | | f ƒ ‚ n' t k
 rc t d | | f ƒ ‚ n Xd  S(   NsJ   More than one migration matches '%s' in app '%s'. Please be more specific.s4   Cannot find a migration matching '%s' from app '%s'.(   t   get_migration_by_prefixR   R   R,   (   R   RF   R   R&   (    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyR$   ¨   s    (   t   __name__t
   __module__R   R   RX   R7   R$   (    (    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyR
      s
   		p	(   R8   R/   t   reR\   t   django.core.management.baseR    R   t	   django.dbR   R   t   django.db.migrations.loaderR   R   t   compileR>   R   R	   R
   (    (    (    sb   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/delete_squashed_migrations.pyt   <module>   s   	