
^Q\!                 @   s   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z Gd d   d e  Z d S)	a  
SyncData
========

Django command similar to 'loaddata' but also deletes.
After 'syncdata' has run, the database will have the same data as the fixture - anything
missing will of been added, anything different will of been updated,
and anything extra will of been deleted.
    N)BaseCommand)no_style)
connectiontransaction)signalcommandc                   s[   e  Z d  Z d Z d Z d Z   f d d   Z d d   Z e e	 j
 d d	     Z   S)
Commandz syncdata command zRMakes the current database have the same data as the fixture(s), no more, no less.zfixture [fixture ...]c          
      sB   t  t |   j |  | j d d d d d d d d d	 f d  S)
Nz--skip-removeactionstore_falsedestremovedefaultThelpzAvoid remove any object from db)superr   add_argumentsadd_argument)selfparser)	__class__ _/var/www/dbchiro/venv/build/django-extensions/django_extensions/management/commands/syncdata.pyr      s    zCommand.add_argumentsc             C   s?  x8| j    D]*} | j j   } t d d   | D  } t d d   | | D  } | j |  } | r xS | D]H } | j | k rt | j   | d k r t d t j	 |   q qt qt Wn  | d k r | r t
 |  }	 |	 d k rt j	 | j j  }
 n t j	 | j j  }
 t d t |	  |
 f  q q Wd	 S)
z
        Deletes all the objects in the database that are not in objects_to_keep.
        - objects_to_keep: A map where the keys are classes, and the values are a
         set of the objects of that class we should keep.
        c             S   s   g  |  ] } | j   q Sr   )pk).0xr   r   r   
<listcomp>+   s   	 z1Command.remove_objects_not_in.<locals>.<listcomp>c             S   s   g  |  ] } | j   q Sr   )r   )r   r   r   r   r   r   ,   s   	    zDeleted object: %sr      zDeleted %s %sN)keysobjectsallset
differencer   deleteprintsixulenZ_metaZverbose_name_pluralZverbose_namestr)r   objects_to_keep	verbosityclass_currentZcurrent_idsZkeep_idsZremove_these_onesobjZnum_deletedZtype_deletedr   r   r   remove_objects_not_in#   s"    
$zCommand.remove_objects_not_inc       !      O   s  d d l  m } d d l m } d d l m } t   |  _ t | j	 d d   } | j	 d d  } d } d }	 g  }
 t
   } d	 d
   } t j   } d d   | j   D } d d   | D } x| D]} | j d  } t |  d k r| } | j   } nH d j | d d    | d! } } | | j   k rJ| g } n g  } | rv| d k rt d |  qn4 t j j |  j j d | | f   t j   d St j j |  r| g } n | t | j  d g } x| D]} | d k rt d | |   n  d } x| D]} | d k rQt d | |  | | f  n  yt j j | d j | | g   } t | d  } | r| j   t |  j j d | | |  f   t j   d S| d 7} |
 j  d  | d k rt d | | | |  f  n  y i  } | j! | |  } xy | D]q } |	 d 7}	 |
 d" d 7<| j" j# } | | k rvt
   | | <n  | | j$ | j"  | j$ |  | j%   q0W| j	 d  r|  j& | |  n  d } Wn t' t( f k
 r  Ynu t) k
 r_d d l* } | j   t j   | r+| j+   n, t j j |  j j d | | j,   f   d SYn X| j   Wqt) k
 r| d k rt d | | | |  f  n  YqXqWqWq Wd |
 k rt j j |  j j d |   t j   d S|	 d k rYt j- j. |  j |  } | rY| d k r5t d  n  x | D] }  | j/ |   q<WqYn  t j0   |	 d k r| d k rt d  qn# | d k rt d |	 | f  n  t j   d S)#z! Main method of a Django command r   )serializers)settings)appsr(   r   	tracebackFc             S   s   |  r d |  p d S)Nz'%s'zabsolute pathr   )dirnamer   r   r   <lambda>R   s    z Command.handle.<locals>.<lambda>c             S   s   g  |  ] } | j   q Sr   )module)r   appr   r   r   r   Y   s   	 z"Command.handle.<locals>.<listcomp>c             S   s4   g  |  ]* } t  j j t  j j | j  d    q S)Zfixtures)ospathjoinr1   __file__)r   r4   r   r   r   r   Z   s   	 .NzLoading '%s' fixtures...zHProblem installing fixture '%s': %s is not a known serialization format. zChecking %s for fixtures...z Trying %s for %s fixture '%s'...rz-Multiple fixtures named '%s' in %s. Aborting.z#Installing %s fixture '%s' from %s.r   Tz$Problem installing fixture '%s': %s
zNo %s fixture '%s' in %s.z=No fixture data found for '%s'. (File format may be invalid.)zResetting sequenceszNo fixtures found.z)Installed %d object(s) from %d fixture(s)r<   r<   )1Zdjango.corer-   Zdjango.confr.   Zdjango.appsr/   r   styleintgetr   r   cursorZget_app_configssplitr%   Zget_public_serializer_formatsr7   r"   sysstderrwriteERRORr   rollbackr5   r6   isabslistZFIXTURE_DIRSopencloseappendZdeserializeobjectr   addsaver,   
SystemExitKeyboardInterrupt	Exceptionr0   	print_exc
format_excopsZsequence_reset_sqlexecutecommit)!r   Zfixture_labelsoptionsr-   r.   r/   r(   Zshow_tracebackZfixture_countZobject_countZobjects_per_fixturemodelsZhumanizer@   Zapp_modulesZapp_fixturesZfixture_labelpartsZfixture_nameformatsformatZfixture_dirsZfixture_dirZlabel_found	full_pathZfixturer'   r   r+   r)   r0   Zsequence_sqlliner   r   r   handle?   s    	$&
 $
&

 



,	1	

zCommand.handle)__name__
__module____qualname____doc__r   argsr   r,   r   r   Zatomicr^   r   r   )r   r   r      s   r   )rb   r5   rB   r#   Zdjango.core.management.baser   Zdjango.core.management.colorr   Z	django.dbr   r   Z"django_extensions.management.utilsr   r   r   r   r   r   <module>
   s   