ó
®â0_c           @  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 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) e* Z+ Wn e, k
 re- Z+ n Xd e f d „  ƒ  YZ. d e j/ f d „  ƒ  YZ0 d „  Z1 d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   product(   t   apps(   t   settings(   t   serializers(   t   ImproperlyConfigured(   t   BaseCommandt   CommandError(   t   no_style(   t   parse_apps_and_model_labels(   t   DEFAULT_DB_ALIASt   DatabaseErrort   IntegrityErrort   connectionst   routert   transaction(   t	   lru_cache(   t   upath(   t
   force_text(   t   cached_property(   t   glob_escapet   Commandc           B  sk   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z e j d d
 ƒ d „  ƒ Z
 e d „  ƒ Z d	 „  Z RS(   u.   Installs the named fixture(s) in the database.uc   No 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 t d d ƒ| j  d d d d d	 d d g  d d ƒd  S(   Nu   argst   metavaru   fixturet   nargsu   +t   helpu   Fixture labels.u
   --databaset   actionu   storet   destu   databaset   defaultuX   Nominates a specific database to load fixtures into. Defaults to the "default" database.u   --appu	   app_labelu,   Only look for fixtures in the specified app.u   --ignorenonexistentu   -iu
   store_trueu   ignoreu[   Ignores entries in the serialized data for fields that do not currently exist on the model.u   -eu	   --excludeu   excludeu   appenduK   An app_label or app_label.ModelName to exclude. Can be used multiple times.(   t   add_argumentR
   t   Nonet   False(   t   selft   parser(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyt   add_arguments)   s    c         O  s£   | d |  _  | d |  _ | d |  _ | d |  _ t | d ƒ \ |  _ |  _ t j d |  j ƒ  |  j	 | ƒ Wd  QXt j
 |  j ƒ rŸ t |  j j ƒ  n  d  S(   Nu   ignoreu   databaseu	   app_labelu	   verbosityu   excludet   using(   t   ignoreR"   t	   app_labelt	   verbosityR	   t   excluded_modelst   excluded_appsR   t   atomict   loaddatat   get_autocommitR   t   close(   R   t   fixture_labelst   options(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyt   handle=   s    c   
      C  sI  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  QXg  |  j D] } | j j ^ qø } y | j d | ƒ Wn& t k
 rL} d | f | _ ‚  n 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 rE|  j |  j k r|  j j  d |  j |  j f ƒ qE|  j j  d |  j |  j |  j f ƒ n  d  S(   Ni    u   rbu   gzu   ru   zipu   bz2t   table_namesu   Problem installing fixtures: %si   u   Resetting sequences
i   u)   Installed %d object(s) from %d fixture(s)u1   Installed %d object(s) (of %d) from %d fixture(s)(#   R   R"   t   fixture_countt   loaded_object_countt   fixture_object_countt   sett   modelsR   t   get_public_serializer_formatst   serialization_formatst   openR   t   gzipt   GzipFilet   SingleZipReadert   compression_formatst   has_bz2t   bz2t   BZ2Filet   find_fixturest   constraint_checks_disabledt
   load_labelt   _metat   db_tablet   check_constraintst	   Exceptiont   argst   opst   sequence_reset_sqlR   R%   t   stdoutt   writet   cursort   execute(
   R   R,   t
   connectiont   fixture_labelt   modelR/   t   et   sequence_sqlRK   t   line(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR)   N   sR    			"		c         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} | j j j |  j k sõ t | j ƒ |  j k r;qõ n  t j |  j | j j ƒ rõ | d 7} |  j j | j j ƒ y: | j d |  j ƒ | r¯|  j j	 d | d	 d
 ƒn  Wqt t f k
 r} d i | j j j d 6| j j j d 6| j j d 6t  | ƒ d 6f | _! ‚  qXqõ qõ W| r?| r?|  j j	 d
 ƒ n  |  j" | 7_" |  j# | 7_# Wn> t$ k
 rž} t% | t& ƒ s˜d | | f f | _! n  ‚  n XWd | j' ƒ  X| d k r t( j) d | t* ƒ q q Wd S(   u9   
        Loads fixtures files for a given label.
        i   i   i    i   u#   Installing %s fixture '%s' from %s.R"   t   ignorenonexistentu   Processed %i object(s).t   endingu    uF   Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)su	   app_labelu   object_nameu   pku	   error_msgu#   Problem installing fixture '%s': %sNu=   No fixture data found for '%s'. (File format may be invalid.)(+   R%   R?   t
   parse_namet   ost   patht   basenameR;   R0   RI   RJ   t   humanizeR   t   deserializeR"   R#   t   objectRB   t
   app_configR'   t   typeR&   R   t   allow_migrate_modelt	   __class__R4   t   addt   saveR   R   R$   t   object_namet   pkR   RF   R1   R2   RE   t
   isinstanceR   R+   t   warningst   warnt   RuntimeWarning(   R   RN   t   show_progresst   fixture_filet   fixture_dirt   fixture_namet   _t   ser_fmtt   cmp_fmtt   open_methodt   modet   fixturet   objects_in_fixturet   loaded_objects_in_fixturet   objectst   objRP   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyRA      sf    $ 	

	
t   maxsizec           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 ˆ  ƒ ‰  np |  j } t j j t j j ˆ  ƒ k rDg  | D]' } t j j | t j j ˆ  ƒ ƒ ^ q} t j j ˆ  ƒ ‰  n  d „  t | | | ƒ Dƒ }	 t ‡  f 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 rd| rd|  j	 j
 d ˆ  t | ƒ f ƒ n  t | ƒ d k r•t d	 ˆ  t | ƒ f ƒ ‚ n  | j | ƒ q‰W| s¿t d
 ˆ  ƒ ‚ n  | S(   u8   
        Finds fixture files for a given label.
        i   u   Loading '%s' fixtures...c         s  s(   |  ] } d  j  d „  | Dƒ ƒ Vq d S(   u   .c         s  s   |  ] } | r | Vq d  S(   N(    (   t   .0t   ext(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pys	   <genexpr>è   s    N(   t   join(   Rw   t   combo(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pys	   <genexpr>è   s   c         3  s$   |  ] } d  j  ˆ  | f ƒ Vq d S(   u   .N(   Ry   (   Rw   t   suffix(   Rk   (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pys	   <genexpr>ë   s    u   Checking %s for fixtures...u   *u   No fixture '%s' in %s.i   u-   Multiple fixtures named '%s' in %s. Aborting.u   No fixture named '%s' found.N(   RU   R"   R   t   listR;   t   keysR   R5   R%   RI   RJ   RV   RW   t   isabst   dirnameRX   t   fixture_dirst   sept   normpathRy   R   R3   RY   t   globt   iglobR   t   appendt   lenR   t   extend(   R   RN   Rm   Rn   t	   databasest   cmp_fmtst   ser_fmtsR€   t   dir_t   suffixest   targetst   fixture_filesRj   t   fixture_files_in_dirRW   t	   candidate(    (   Rk   sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR?   Ð   sJ    *!	1 c         C  s4  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 ƒ g  | D]* } t t j	 j t j	 j | ƒ ƒ ƒ ^ q } | S(   uç   
        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.
        u*   settings.FIXTURE_DIRS contains duplicates.u   fixturesuc   '%s' is a default fixture directory for the '%s' app and cannot be listed in settings.FIXTURE_DIRS.u    (   R   t   FIXTURE_DIRSR†   R3   R   R   t   get_app_configst   labelRV   RW   Ry   R$   t   isdirR…   R‡   R|   R   t   abspatht   realpath(   R   t   dirsR€   R\   R$   t   app_dirt   d(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR€   	  s&    			7c         C  sÜ   | j  d d ƒ } t | ƒ d k rN | d |  j k rN | d } | d  } n d } t | ƒ d k rº | d |  j k r | d } | d  } qÀ t d d j | d  ƒ | d f ƒ ‚ n d } d j | ƒ } | | | f S(   uX   
        Splits fixture name in name, serialization format, compression format.
        u   .i   i   iÿÿÿÿuH   Problem installing fixture '%s': %s is not a known serialization format.u    N(   t   rsplitR†   R;   R   R6   R   Ry   (   R   Rk   t   partsRn   Rm   t   name(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyRU   (  s    %

$N(   t   __name__t
   __module__R   t   missing_args_messageR!   R.   R)   RA   R   R   R?   R   R€   RU   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR   "   s   			A	A9R:   c           B  s   e  Z d  „  Z d „  Z RS(   c         O  sA   t  j j |  | | Ž t |  j ƒ  ƒ d k r= t d ƒ ‚ n  d  S(   Ni   u.   Zip-compressed fixtures must contain one file.(   t   zipfilet   ZipFilet   __init__R†   t   namelistt
   ValueError(   R   RF   t   kwargs(    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR¢   F  s    c         C  s   t  j j |  |  j ƒ  d ƒ S(   Ni    (   R    R¡   t   readR£   (   R   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR¦   K  s    (   R   Rž   R¢   R¦   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyR:   D  s   	c         C  s   |  r d |  Sd S(   Nu   '%s'u   absolute path(    (   R   (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyRY   O  s    (2   t
   __future__R    Rƒ   R8   RV   Re   R    t	   itertoolsR   t   django.appsR   t   django.confR   t   django.coreR   t   django.core.exceptionsR   t   django.core.management.baseR   R   t   django.core.management.colorR   t   django.core.management.utilsR	   t	   django.dbR
   R   R   R   R   R   t   django.utilsR   t   django.utils._osR   t   django.utils.encodingR   t   django.utils.functionalR   t   django.utils.globR   R=   t   TrueR<   t   ImportErrorR   R   R¡   R:   RY   (    (    (    sJ   /tmp/pip-unpacked-wheel-BAJOf3/django/core/management/commands/loaddata.pyt   <module>   s8   .

ÿ #