ó
TR¹Nc           @   s   d  Z  d d l Z d d l Z d d l m Z m Z d d l Z d d l m Z d d l	 m
 Z
 d d l m Z d e f d „  ƒ  YZ d	 „  Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d „ Z d „  Z d S(   s„   
Base classes for writing management commands (named commands which can
be executed through ``django-admin.py`` or ``manage.py``).

iÿÿÿÿN(   t   make_optiont   OptionParser(   t   ImproperlyConfigured(   t   color_style(   t	   smart_strt   CommandErrorc           B   s   e  Z d  Z RS(   sÛ  
    Exception class indicating a problem while executing a management
    command.

    If this exception is raised during the execution of a management
    command, it will be caught and turned into a nicely-printed error
    message to the appropriate output stream (i.e., stderr); as a
    result, raising this exception (with a sensible description of the
    error) is the preferred way to indicate that something has gone
    wrong in the execution of a command.

    (   t   __name__t
   __module__t   __doc__(    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR      s   c         C   sB   |  j  r |  j  t j d <n  |  j r> t j j d |  j ƒ n  d S(   s¡   
    Include any default options that all commands should accept here
    so that ManagementUtility can handle them before searching for
    user commands.

    t   DJANGO_SETTINGS_MODULEi    N(   t   settingst   ost   environt
   pythonpatht   syst   patht   insert(   t   options(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   handle_default_options   s    		t   BaseCommandc           B   sõ   e  Z d  Z e d d d d d d d d d	 d
 d d d d g d d ƒe d d d ƒe d d d ƒe d d d d d ƒf Z d Z d Z e Z e Z	 e
 Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d! e
 d „ Z d  „  Z RS("   sY  
    The base class from which all management commands ultimately
    derive.

    Use this class if you want access to all of the mechanisms which
    parse the command-line arguments and work out what code to call in
    response; if you don't need to change any of that behavior,
    consider using one of the subclasses defined in this file.

    If you are interested in overriding/customizing various aspects of
    the command-parsing and -execution behavior, the normal flow works
    as follows:

    1. ``django-admin.py`` or ``manage.py`` loads the command class
       and calls its ``run_from_argv()`` method.

    2. The ``run_from_argv()`` method calls ``create_parser()`` to get
       an ``OptionParser`` for the arguments, parses them, performs
       any environment changes requested by options like
       ``pythonpath``, and then calls the ``execute()`` method,
       passing the parsed arguments.

    3. The ``execute()`` method attempts to carry out the command by
       calling the ``handle()`` method with the parsed arguments; any
       output produced by ``handle()`` will be printed to standard
       output and, if the command is intended to produce a block of
       SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.

    4. If ``handle()`` raised a ``CommandError``, ``execute()`` will
       instead print an error message to ``stderr``.

    Thus, the ``handle()`` method is typically the starting point for
    subclasses; many built-in commands and command types either place
    all of their logic in ``handle()``, or perform some additional
    parsing work in ``handle()`` and then delegate from it to more
    specialized methods as needed.

    Several attributes affect behavior at various steps along the way:

    ``args``
        A string listing the arguments accepted by the command,
        suitable for use in help messages; e.g., a command which takes
        a list of application names might set this to '<appname
        appname ...>'.

    ``can_import_settings``
        A boolean indicating whether the command needs to be able to
        import Django settings; if ``True``, ``execute()`` will verify
        that this is possible before proceeding. Default value is
        ``True``.

    ``help``
        A short description of the command, which will be printed in
        help messages.

    ``option_list``
        This is the list of ``optparse`` options which will be fed
        into the command's ``OptionParser`` for parsing arguments.

    ``output_transaction``
        A boolean indicating whether the command outputs SQL
        statements; if ``True``, the output will automatically be
        wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is
        ``False``.

    ``requires_model_validation``
        A boolean; if ``True``, validation of installed models will be
        performed prior to executing the command. Default value is
        ``True``. To validate an individual application's models
        rather than all applications' models, call
        ``self.validate(app)`` from ``handle()``, where ``app`` is the
        application's Python module.

    s   -vs   --verbosityt   actiont   storet   destt	   verbosityt   defaultt   1t   typet   choicet   choicest   0t   2t   helps@   Verbosity level; 0=minimal output, 1=normal output, 2=all outputs
   --settingss›   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.s   --pythonpathsM   A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".s   --tracebackt
   store_trues   Print traceback on exceptiont    c         C   s   t  ƒ  |  _ d  S(   N(   R   t   style(   t   self(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   __init__Š   s    c         C   s
   t  j ƒ  S(   s«   
        Return the Django version, which should be correct for all
        built-in Django commands. User-supplied commands should
        override this method.

        (   t   djangot   get_version(   R#   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR&      s    c         C   s5   d | |  j  f } |  j r- d | |  j f S| Sd S(   s~   
        Return a brief description of how to use this command, by
        default from the attribute ``self.help``.

        s   %%prog %s [options] %ss   %s

%sN(   t   argsR   (   R#   t
   subcommandt   usage(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR)   –   s    	c      	   C   s1   t  d | d |  j | ƒ d |  j ƒ  d |  j ƒ S(   s|   
        Create and return the ``OptionParser`` which will be used to
        parse the arguments to this command.

        t   progR)   t   versiont   option_list(   R   R)   R&   R,   (   R#   t	   prog_nameR(   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   create_parser¢   s    c         C   s    |  j  | | ƒ } | j ƒ  d S(   sb   
        Print the help message for this command, derived from
        ``self.usage()``.

        N(   R.   t
   print_help(   R#   R-   R(   t   parser(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR/   ­   s    c         C   sT   |  j  | d | d ƒ } | j | d ƒ \ } } t | ƒ |  j | | j Ž  d S(   sƒ   
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command.

        i    i   i   N(   R.   t
   parse_argsR   t   executet   __dict__(   R#   t   argvR0   R   R'   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   run_from_argv¶   s    
c         O   sb  |  j  rv y! d d l m } | j d ƒ Wqv t k
 rr } t j j t |  j	 j
 d | ƒ ƒ ƒ t j d ƒ qv Xn  yŸ |  j r |  j ƒ  n  |  j | | Ž  } | r|  j rï d d l m } | j j ƒ  rï |  j	 j | j j ƒ  ƒ GHqï n  | GH|  j r|  j	 j d ƒ GHqn  WnF t k
 r]} t j j t |  j	 j
 d | ƒ ƒ ƒ t j d ƒ n Xd S(	   s  
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``). If the command raises a
        ``CommandError``, intercept it and print it sensibly to
        stderr.

        iÿÿÿÿ(   t   translations   en-uss
   Error: %s
i   (   t
   connections   COMMIT;N(   t   can_import_settingst   django.utilsR6   t   activatet   ImportErrorR   t   stderrt   writeR   R"   t   ERRORt   exitt   requires_model_validationt   validatet   handlet   output_transactiont	   django.dbR7   t   opst   start_transaction_sqlt   SQL_KEYWORDR   (   R#   R'   R   R6   t   et   outputR7   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR2   Á   s,    	&		 	&c         C   sÀ   d d l  m } y d d l m } Wn! t k
 rG d d l m } n X| ƒ  } | | | ƒ } | r’ | j d ƒ | j ƒ  } t d | ƒ ‚ n  | r¼ d | | d k r° d p³ d	 f GHn  d
 S(   s•   
        Validates the given app, raising CommandError for any errors.

        If app is None, then this will validate all installed apps.

        iÿÿÿÿ(   t   get_validation_errors(   t   StringIOi    s'   One or more models did not validate:
%ss   %s error%s foundi   t   sR!   N(   t!   django.core.management.validationRJ   t	   cStringIORK   R;   t   seekt   readR   (   R#   t   appt   display_num_errorsRJ   RK   RL   t
   num_errorst
   error_text(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRA   è   s    	c         O   s   t  ƒ  ‚ d S(   sb   
        The actual logic of the command. Subclasses must implement
        this method.

        N(   t   NotImplementedError(   R#   R'   R   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRB   ý   s    N(   R   R   R   R    R,   R   R'   t   TrueR8   R@   t   FalseRC   R$   R&   R)   R.   R/   R5   R2   t   NoneRA   RB   (    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR   +   s0   J											't
   AppCommandc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s  
    A management command which takes one or more installed application
    names as arguments, and does something with each of them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_app()``, which will be called once for each application.

    s   <appname appname ...>c   
      O   sÃ   d d l  m } | s% t d ƒ ‚ n  y& g  | D] } | j | ƒ ^ q/ } Wn) t t f k
 rv } t d | ƒ ‚ n Xg  } x6 | D]. } |  j | |  }	 |	 r„ | j |	 ƒ q„ q„ Wd j | ƒ S(   Niÿÿÿÿ(   t   modelss   Enter at least one appname.s8   %s. Are you sure your INSTALLED_APPS setting is correct?s   
(	   RD   RZ   R   t   get_appR   R;   t
   handle_appt   appendt   join(
   R#   t
   app_labelsR   RZ   t	   app_labelt   app_listRH   RI   RQ   t
   app_output(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRB     s    &c         K   s   t  ƒ  ‚ d S(   s­   
        Perform the command's actions for ``app``, which will be the
        Python module corresponding to an application name given on
        the command line.

        N(   RU   (   R#   RQ   R   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR\     s    (   R   R   R   R'   RB   R\   (    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRY     s   	t   LabelCommandc           B   s,   e  Z d  Z d Z d Z d „  Z d „  Z RS(   s€  
    A management command which takes one or more arbitrary arguments
    (labels) on the command line, and does something with each of
    them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_label()``, which will be called once for each label.

    If the arguments should be names of installed applications, use
    ``AppCommand`` instead.

    s   <label label ...>t   labelc         O   sh   | s t  d |  j ƒ ‚ n  g  } x6 | D]. } |  j | |  } | r) | j | ƒ q) q) Wd j | ƒ S(   Ns   Enter at least one %s.s   
(   R   Rd   t   handle_labelR]   R^   (   R#   t   labelsR   RI   Rd   t   label_output(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRB   8  s    c         K   s   t  ƒ  ‚ d S(   s~   
        Perform the command's actions for ``label``, which will be the
        string as given on the command line.

        N(   RU   (   R#   Rd   R   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRe   C  s    (   R   R   R   R'   Rd   RB   Re   (    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRc   (  s
   	t   NoArgsCommandc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s8  
    A command which takes no arguments on the command line.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_noargs()``; ``handle()`` itself is overridden to ensure
    no arguments are passed to the command.

    Attempting to pass arguments will raise ``CommandError``.

    R!   c         O   s"   | r t  d ƒ ‚ n  |  j |   S(   Ns$   Command doesn't accept any arguments(   R   t   handle_noargs(   R#   R'   R   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRB   X  s    c         K   s   t  ƒ  ‚ d S(   s2   
        Perform this command's actions.

        N(   RU   (   R#   R   (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRi   ]  s    (   R   R   R   R'   RB   Ri   (    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyRh   K  s   
	R!   c         C   s§  d d l  } d d l } i d d 6d d 6| } | j d | ƒ s | j d | ƒ s] d } n d } t d	 | | | f ƒ ‚ n  t j j | | ƒ }	 y t j |	 ƒ Wn t k
 rÆ }
 t |
 ƒ ‚ n Xt j j t	 j
 d
 d d | ƒ } x¶t j | ƒ D]¥\ } } } | t | ƒ d j d | | ƒ } | rRt j t j j |	 | ƒ ƒ n  x3 t | ƒ D]% \ } } | j d ƒ r_| | =q_q_Wx| D]} | j d ƒ sªqn  t j j | | ƒ } t j j |	 | | j d | | ƒ ƒ } t | d ƒ } t | d ƒ } | j | j ƒ  j d | | ƒ j d | | ƒ ƒ | j ƒ  | j ƒ  y | j | | ƒ t | ƒ Wqt k
 ršt j j |  j d | ƒ ƒ qXqWqú Wd S(   sƒ   
    Copies either a Django application layout template or a Django project
    layout template into the specified directory.

    iÿÿÿÿNRQ   t   projects   ^[_a-zA-Z]\w*$s
   ^[_a-zA-Z]s5   make sure the name begins with a letter or underscores)   use only numbers, letters and underscoress%   %r is not a valid %s name. Please %s.i    t   confs   %s_templatei   s   %s_namet   .s   .pyt   rt   ws   {{ %s_name }}sl   Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.
(   t   ret   shutilt   searchR   R   R   R^   t   mkdirt   OSErrorR%   t   __path__t   walkt   lent   replacet	   enumeratet
   startswitht   endswitht   openR=   RP   t   closet   copymodet   _make_writeableR   R<   t   NOTICE(   R"   t   app_or_projectt   namet	   directoryt
   other_nameRo   Rp   t   othert   messaget   top_dirRH   t   template_dirt   dt   subdirst   filest   relative_dirt   it   subdirt   ft   path_oldt   path_newt   fp_oldt   fp_new(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   copy_helperd  sH    	#$(3

c         C   sv   d d l  } t j j d ƒ r" d St j |  t j ƒ sr t j  |  ƒ } | j | j ƒ | j	 B} t j
 |  | ƒ n  d S(   sW   
    Make sure that the file is writeable. Useful if our source is
    read-only.

    iÿÿÿÿNt   java(   t   statR   t   platformRy   R   t   accesst   W_OKt   S_IMODEt   st_modet   S_IWUSRt   chmod(   t   filenameR•   t   stt   new_permissions(    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyR~   ž  s    (   R   R   R   t   optparseR    R   R%   t   django.core.exceptionsR   t   django.core.management.colorR   t   django.utils.encodingR   t	   ExceptionR   R   t   objectR   RY   Rc   Rh   R“   R~   (    (    (    sH   /home/panlixing/Python_Projects/gaeseries/django/core/management/base.pyt   <module>   s   	Ú##: