
v^8                 @   s  d  d l  Z  d  d l 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 d  d
 l m Z m Z m Z m Z m Z m Z d  d l m  Z  y d  d l! Z! d Z" Wn e# k
 rHd Z" Yn Xd Z$ Gd d   d e  Z% Gd d   d e j&  Z' d d   Z( d S)    N)product)apps)settings)serializers)ImproperlyConfigured)BaseCommandCommandError)no_style)parse_apps_and_model_labels)DEFAULT_DB_ALIASDatabaseErrorIntegrityErrorconnectionsroutertransaction)cached_propertyTF-c               @   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	 t d d
 | j  d d d d d | j  d d d d d d d d | j  d d d d d	 g  d d | j  d d d d  S)Nargsmetavarfixturenargs+helpzFixture labels.z
--databasedefaultzXNominates a specific database to load fixtures into. Defaults to the "default" database.z--appdest	app_labelz,Only look for fixtures in the specified app.z--ignorenonexistentz-iaction
store_trueignorez[Ignores entries in the serialized data for fields that do not currently exist on the model.z-ez	--excludeappendzKAn app_label or app_label.ModelName to exclude. Can be used multiple times.z--formatz2Format of serialized data when reading from stdin.)add_argumentr   )selfparser r$   J/tmp/pip-build-8lau8j11/django/django/core/management/commands/loaddata.pyadd_arguments'   s     zCommand.add_argumentsc          	   O   s   | d |  _  | d |  _ | d |  _ | d |  _ t | d  \ |  _ |  _ | d |  _ t j	 d |  j   |  j
 |  Wd  QRXt j |  j  r t |  j j   d  S)Nr   Zdatabaser   	verbosityexcludeformatusing)r   r*   r   r'   r
   excluded_modelsexcluded_appsr)   r   ZatomicloaddataZget_autocommitr   close)r"   fixture_labelsoptionsr$   r$   r%   handle?   s    zCommand.handlec       
   !   C   s~  t  |  j } d |  _ d |  _ d |  _ t   |  _ t j   |  _	 d  t
 d f d t j d f d t d f d d d   d  f i |  _ t r t j d f |  j d	 <x" | D] } |  j |  r Pq Wd  S| j   S g  |  _ x | D] } |  j |  q Wx$ |  j D] } | j d
 |  j  qWWd  QRXd d   |  j D } y | j d |  Wn8 t k
 r} z d | f | _   WYd  d  } ~ Xn X|  j d k r| j j t   |  j  } | r|  j d k r|  j j d  | j    % } x | D] }	 | j! |	  qWWd  QRX|  j d k rz|  j |  j k rT|  j j d |  j |  j f  n& |  j j d |  j |  j |  j f  d  S)Nr   rbgzziprstdinc              W   s   t  j S)N)sysr6   )r   r$   r$   r%   <lambda>`   s    z"Command.loaddata.<locals>.<lambda>bz2r*   c             S   s   g  |  ] } | j  j  q Sr$   )_metaZdb_table).0modelr$   r$   r%   
<listcomp>x   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_bz2r9   BZ2Filefind_fixturesZconstraint_checks_disabledobjs_with_deferred_fields
load_labelZsave_deferred_fieldsZcheck_constraints	Exceptionr   opsZsequence_reset_sqlr	   r'   stdoutwritecursorexecute)
r"   r/   
connectionfixture_labelobjr>   eZsequence_sqlrV   liner$   r$   r%   r-   Q   sX    						zCommand.loaddatac          #   C   s   |  j  d k } x|  j |  D]\ } } } |  j t j j |   \ } } } |  j | \ }	 }
 |	 | |
  } zVy|  j d 7_ d } d } |  j  d k r |  j j	 d | | t
 |  f  t j | | d |  j d |  j d d	 } xJ| D]B} | d 7} | j j j |  j k s t | j  |  j k r;q t j |  j | j j  r!| d 7} |  j j | j j  y7 | j d |  j  | r|  j j	 d
 | d d Wnq t t t f k
 r } zH d d | j j j d | j j j d | j j  d | i f | _!   WYd d } ~ Xn X| j" r |  j# j$ |  q W| rZ| rZ|  j j	 d  |  j% | 7_% |  j& | 7_& WnM t' k
 r} z- t( | t)  sd | | f f | _!   WYd d } ~ Xn XWd | j*   X| d k r t+ j, d | t-  q Wd S)z&Load fixtures files for a given label.   r@   r   r?   z#Installing %s fixture '%s' from %s.r*   ZignorenonexistentZhandle_forward_referencesTz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'   rO   
parse_nameospathbasenamerL   rA   rT   rU   humanizer   Zdeserializer*   r   objectr:   
app_configr,   typer+   r   Zallow_migrate_model	__class__rE   addsaver   r   
ValueErrorr   r_   r`   r   Zdeferred_fieldsrP   r    rB   rC   rR   
isinstancer   r.   warningswarnRuntimeWarning)r"   rY   show_progressZfixture_filefixture_dirfixture_name_ser_fmtcmp_fmtZopen_methodmoder   Zobjects_in_fixtureZloaded_objects_in_fixtureZobjectsrZ   r[   r$   r$   r%   rQ      sj    $		

	
	zCommand.load_labelmaxsizeNc                s  | t  k r t  d t  f g S|  j |  \   } } |  j d g } | d k r^ t |  j  n | g } | d k r t j   n | g } |  j d k r |  j j	 d    t
 j j    r t
 j j    g } t
 j j      nR |  j } t
 j j t
 j j    k r9  f d d   | D } t
 j j      d d   t | | |  D }   f d d	   | D }	 g  }
 x| D]} |  j d k r|  j j	 d
 t |   g  } t
 j j |    } xR t j t j |  d  D]4 } t
 j j |  |	 k r| j | |   f  qW|  j d k rS| rS|  j j	 d   t |  f  t |  d k rt d   t |  f   |
 j |  q~W|
 st d     |
 S)z%Find fixture files for a given label.Nr?   zLoading '%s' fixtures...c                s1   g  |  ]' } t  j j | t  j j      q Sr$   )rb   rc   joindirname)r;   Zdir_)rs   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$   )r;   extr$   r$   r%   	<genexpr>   s    z2Command.find_fixtures.<locals>.<genexpr>.<genexpr>N)ry   )r;   Zcombor$   r$   r%   r}      s   z(Command.find_fixtures.<locals>.<genexpr>c                s%   h  |  ] } d  j    | f   q S)r{   )ry   )r;   suffix)rs   r$   r%   	<setcomp>   s   	 z(Command.find_fixtures.<locals>.<setcomp>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.)
READ_STDINra   r*   listrL   r   rF   r'   rT   rU   rb   rc   isabsrz   rd   fixture_dirssepnormpathr   re   ry   globiglobescaper    lenr   extend)r"   rY   ru   rv   Z	databasesZcmp_fmtsZser_fmtsr   suffixestargetsZfixture_filesrr   Zfixture_files_in_dirrc   	candidater$   )rs   r%   rO      sN    $!	
	#zCommand.find_fixturesc             C   s   g  } t  j } t |  t t |   k r9 t d   x t j   D] } | j } t j	 j
 | j	 d  } | | k r t d | | f   |  j r | |  j k r qF t j	 j |  rF | j |  qF W| j |  | 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.r^   c             S   s"   g  |  ] } t  j j |   q Sr$   )rb   rc   realpath)r;   dr$   r$   r%   r=   /  s   	 z(Command.fixture_dirs.<locals>.<listcomp>)r   ZFIXTURE_DIRSr   rD   r   r   Zget_app_configslabelrb   rc   ry   r   isdirr    r   )r"   dirsr   rg   r   Zapp_dirr$   r$   r%   r     s$    			zCommand.fixture_dirsc             C   s  | t  k r1 |  j s! t d   t  |  j d f S| j d d  } t |  d k r | d |  j k r | 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)zW
        Split fixture name in name, serialization format, compression format.
        z3--format must be specified when reading from stdin.r6   r{   r?   r@   NzHProblem installing fixture '%s': %s is not a known serialization format.r   r   r   r   r   r   r   )r   r)   r   rsplitr   rL   rG   ry   )r"   rs   partsrv   ru   namer$   r$   r%   ra   1  s&    	%

*zCommand.parse_name)__name__
__module____qualname__r   Zmissing_args_messager&   r1   r-   rQ   	functools	lru_cacherO   r   r   ra   r$   r$   r$   r%   r       s   EC:r   c                   s.   e  Z d  Z   f d d   Z d d   Z   S)rK   c                s;   t    j | |   t |  j    d k r7 t d   d  S)Nr@   z.Zip-compressed fixtures must contain one file.)super__init__r   namelistrl   )r"   r   kwargs)ri   r$   r%   r   T  s    zSingleZipReader.__init__c             C   s   t  j j |  |  j   d  S)Nr   )zipfileZipFilereadr   )r"   r$   r$   r%   r   Y  s    zSingleZipReader.read)r   r   r   r   r   r$   r$   )ri   r%   rK   R  s   rK   c             C   s   |  r d |  Sd S)Nz'%s'zabsolute pathr$   )rz   r$   r$   r%   re   ]  s    re   ))r   r   rI   rb   r7   rn   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.core.management.utilsr
   Z	django.dbr   r   r   r   r   r   Zdjango.utils.functionalr   r9   rM   ImportErrorr   r   r   rK   re   r$   r$   r$   r%   <module>   s4   .
 3