
 X34                 @   s  d  d l  m Z d  d l Z d  d l 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 d  d l m Z d  d l m Z d  d l m Z m Z d  d	 l m Z d  d
 l m Z m Z m Z m Z m Z m Z d  d l m Z d  d l m  Z  d  d l! m" Z" d  d l# m$ Z$ d  d l% m& Z& y d  d l' Z' d Z( Wn e) k
 rpd Z( Yn XGd d   d e  Z* Gd d   d e j+  Z, d d   Z- d S)    )unicode_literalsN)product)apps)settings)serializers)ImproperlyConfigured)BaseCommandCommandError)no_style)DEFAULT_DB_ALIASDatabaseErrorIntegrityErrorconnectionsroutertransaction)	lru_cache)upath)
force_text)cached_property)glob_escapeTFc               @   s   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 d
   Z e	 j	 d d  d d    Z
 e d d    Z d d   Z d S)Commandz.Installs the named fixture(s) in the database.zcNo database fixture specified. Please provide the path of at least one fixture in the command line.c             C   s   | j  d d d d d d d | j  d d	 d
 d d d t d d | j  d d	 d
 d d d d  d d | j  d d d	 d d d d d d d d  S)Nargsmetavarfixturenargs+helpzFixture labels.z
--databaseactionstoredestdatabasedefaultzXNominates a specific database to load fixtures into. Defaults to the "default" database.z--app	app_labelz,Only look for fixtures in the specified app.z--ignorenonexistentz-i
store_trueignoreFz[Ignores entries in the serialized data for fields that do not currently exist on the model.)add_argumentr   )selfparser r(   V/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/loaddata.pyadd_arguments(   s    zCommand.add_argumentsc          	   O   s   | d |  _  | d |  _ | d |  _ | d |  _ t j d |  j   |  j |  Wd  QXt j |  j  r t |  j j	   n  d  S)Nr$   r    r"   	verbosityusing)
r$   r,   r"   r+   r   ZatomicloaddataZget_autocommitr   close)r&   fixture_labelsoptionsr(   r(   r)   handle8   s    zCommand.handlec       	      C   sO  t  |  j } d |  _ d |  _ d |  _ t   |  _ t j   |  _	 i t
 d f d  6t j d f d 6t d f d 6|  _ t r t j d f |  j d <n  x% | D] } |  j |  r Pq q Wd  S| j   # x | D] } |  j |  q WWd  QXd d   |  j D } y | j d	 |  Wn8 t k
 rR} z d
 | f | _   WYd  d  } ~ Xn X|  j d k r| j j t   |  j  } | r|  j d k r|  j j d  n  | j   % } x | D] } | j |  qWWd  QXqn  |  j d k rK|  j |  j k r"|  j j d |  j |  j f  qK|  j j d |  j |  j |  j f  n  d  S)Nr   rbgzrzipbz2c             S   s   g  |  ] } | j  j  q Sr(   )_metaZdb_table).0modelr(   r(   r)   
<listcomp>l   s   	 z$Command.loaddata.<locals>.<listcomp>table_nameszProblem installing fixtures: %s   zResetting sequences
   z)Installed %d object(s) from %d fixture(s)z1Installed %d object(s) (of %d) from %d fixture(s)) r   r,   fixture_countloaded_object_countfixture_object_countsetmodelsr   get_public_serializer_formatsserialization_formatsopengzipGzipFileSingleZipReadercompression_formatshas_bz2r6   BZ2Filefind_fixturesZconstraint_checks_disabled
load_labelZcheck_constraints	Exceptionr   opsZsequence_reset_sqlr
   r+   stdoutwritecursorexecute)	r&   r/   
connectionfixture_labelr;   eZsequence_sqlrR   liner(   r(   r)   r-   I   sR    					zCommand.loaddatac             C   s  |  j  d k } x|  j |  D]\ } } } |  j t j j |   \ } } } |  j | \ }	 }
 |	 | |
  } zy|  j d 7_ d } d } |  j  d k r |  j j	 d | | t
 |  f  n  t j | | d |  j d |  j } x| D]} | d 7} t j |  j | j j  r | d 7} |  j j | j j  y: | j d |  j  | ry|  j j	 d | d	 d
 n  Wqt t f k
 r} zR d i | j j j d 6| j j j d 6| j j d 6t |  d 6f | _   WYd d } ~ XqXq q W| r| r|  j j	 d
  n  |  j | 7_ |  j | 7_ WnP t  k
 r} z0 t! | t"  swd | | f f | _ n    WYd d } ~ Xn XWd | j#   X| d k r t$ j% d | t&  q q Wd S)z9
        Loads fixtures files for a given label.
           r=   r   r<   z#Installing %s fixture '%s' from %s.r,   ZignorenonexistentzProcessed %i object(s).Zending zFCould not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)sr"   object_namepkZ	error_msgNz#Problem installing fixture '%s': %sz=No fixture data found for '%s'. (File format may be invalid.))'r+   rL   
parse_nameospathbasenamerI   r>   rP   rQ   humanizer   Zdeserializer,   r$   r   Zallow_migrate_modelobject	__class__rB   addsaver   r   r7   r"   rZ   r[   r   r   r?   r@   rN   
isinstancer	   r.   warningswarnRuntimeWarning)r&   rU   show_progressZfixture_filefixture_dirfixture_name_ser_fmtcmp_fmtZopen_methodmoder   Zobjects_in_fixtureZloaded_objects_in_fixtureZobjectsobjrV   r(   r(   r)   rM      s^    $	

	
zCommand.load_labelmaxsizeNc                s  |  j  |  \   } } |  j d g } | d k rH t |  j j    n | g } | d k ri t j   n | g } |  j d k r |  j j	 d    n  t
 j j    r t
 j j    g } t
 j j      nU |  j } t
 j j t
 j j    k r)  f d d   | D } t
 j j      n  d d   t | | |  D } t   f d d   | D  }	 g  }
 x!| D]} |  j d k r|  j j	 d	 t |   n  g  } t
 j j |    } xR t j t |  d
  D]7 } t
 j j |  |	 k r| j | |   f  qqW|  j d k rO| rO|  j j	 d   t |  f  n  t |  d k rt d   t |  f   n  |
 j |  qtW|
 st d     n  |
 S)z8
        Finds fixture files for a given label.
        Nr<   zLoading '%s' fixtures...c                s1   g  |  ]' } t  j j | t  j j      q Sr(   )r]   r^   joindirname)r8   Zdir_)rk   r(   r)   r:      s   	z)Command.find_fixtures.<locals>.<listcomp>c             s   s+   |  ]! } d  j  d d   | D  Vq d S).c             s   s   |  ] } | r | Vq d  S)Nr(   )r8   extr(   r(   r)   	<genexpr>   s    z2Command.find_fixtures.<locals>.<genexpr>.<genexpr>N)rr   )r8   Zcombor(   r(   r)   rv      s   z(Command.find_fixtures.<locals>.<genexpr>c             3   s$   |  ] } d  j    | f  Vq d S)rt   N)rr   )r8   suffix)rk   r(   r)   rv      s    zChecking %s for fixtures...*zNo fixture '%s' in %s.r=   z-Multiple fixtures named '%s' in %s. Aborting.zNo fixture named '%s' found.)r\   r,   listrI   keysr   rC   r+   rP   rQ   r]   r^   isabsrs   r_   fixture_dirssepnormpathr   rA   r`   rr   globiglobr   appendlenr	   extend)r&   rU   rm   rn   Z	databasesZcmp_fmtsZser_fmtsr|   suffixestargetsZfixture_filesrj   Zfixture_files_in_dirr^   	candidater(   )rk   r)   rL      sJ    *!	
	 zCommand.find_fixturesc             C   s  g  } t  j } t |  t t |   k r< t d   n  x t j   D] } | j } t j	 j
 | j	 d  } | | k r t d | | f   n  |  j r | |  j k r qI n  t j	 j |  rI | j |  qI qI W| j t |   | j d  d d   | D } | S)z
        Return a list of fixture directories.

        The list contains the 'fixtures' subdirectory of each installed
        application, if it exists, the directories in FIXTURE_DIRS, and the
        current directory.
        z*settings.FIXTURE_DIRS contains duplicates.Zfixtureszc'%s' is a default fixture directory for the '%s' app and cannot be listed in settings.FIXTURE_DIRS.rY   c             S   s4   g  |  ]* } t  t j j t j j |     q Sr(   )r   r]   r^   abspathrealpath)r8   dr(   r(   r)   r:     s   	 z(Command.fixture_dirs.<locals>.<listcomp>)r   ZFIXTURE_DIRSr   rA   r   r   Zget_app_configslabelr]   r^   rr   r"   isdirr   r   ry   )r&   dirsr|   Z
app_configr"   Zapp_dirr(   r(   r)   r|     s&    			zCommand.fixture_dirsc             C   s   | j  d d  } t |  d k rT | d |  j k rT | d } | d d	  } n d } t |  d k r | d
 |  j k r | d } | d d  } q t d d j | d d   | d f   n d } d j |  } | | | f S)zX
        Splits fixture name in name, serialization format, compression format.
        rt   r<   r=   NzHProblem installing fixture '%s': %s is not a known serialization format.rY   r   r   r   r   r   r   r   )rsplitr   rI   rD   r	   rr   )r&   rk   partsrn   rm   namer(   r(   r)   r\      s    %

*zCommand.parse_name)__name__
__module____qualname__r   Zmissing_args_messager*   r1   r-   rM   r   rL   r   r|   r\   r(   r(   r(   r)   r   !   s   A>9r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)rH   c             O   sA   t  j j |  | |  t |  j    d k r= t d   n  d  S)Nr=   z.Zip-compressed fixtures must contain one file.)zipfileZipFile__init__r   namelist
ValueError)r&   r   kwargsr(   r(   r)   r   >  s    zSingleZipReader.__init__c             C   s   t  j j |  |  j   d  S)Nr   )r   r   readr   )r&   r(   r(   r)   r   C  s    zSingleZipReader.readN)r   r   r   r   r   r(   r(   r(   r)   rH   <  s   rH   c             C   s   |  r d |  Sd S)Nz'%s'zabsolute pathr(   )rs   r(   r(   r)   r`   G  s    r`   ).
__future__r   r   rF   r]   rf   r   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Zdjango.core.exceptionsr   Zdjango.core.management.baser   r	   Zdjango.core.management.colorr
   Z	django.dbr   r   r   r   r   r   Zdjango.utilsr   Zdjango.utils._osr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   Zdjango.utils.globr   r6   rJ   ImportErrorr   r   rH   r`   r(   r(   r(   r)   <module>   s6   .
 