ó
j4Vdc           @€  sS  d  Z  d d l m 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
 Td d l m Z e e d „ Z e e d	 „ Z d
 „  Z d e d e e e d d e d d „
 Z d e d d e d „ Z e d d e d „ Z e d d e d „ Z e e e e e d „ Z e e e e d „ Z d „  Z d „  Z d S(   sF   
Module providing easy API for working with remote files and folders.
iÿÿÿÿ(   t   with_statementN(   t   StringIO(   t   partial(   t   *(   t
   apply_lcwdc         C€  s€   | r t  p t } d t |  ƒ } | rO t d t ƒ  | | ƒ j SWd QXn  t t d ƒ d t ƒ | | ƒ j SWd QXd S(   s  
    Return True if given path exists on the current remote host.

    If ``use_sudo`` is True, will use `sudo` instead of `run`.

    `exists` will, by default, hide all output (including the run line, stdout,
    stderr and any warning resulting from the file not existing) in order to
    avoid cluttering output. You may specify ``verbose=True`` to change this
    behavior.
    s
   test -e %st	   warn_onlyNt
   everything(   t   sudot   runt   _expand_patht   settingst   Truet   failedt   hide(   t   patht   use_sudot   verboset   funct   cmd(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   exists   s    c         C€  so   | r t  n t } d |  } g  i t d 6} } | sH t d ƒ g } n  t | | Ž   | | ƒ j SWd QXd S(   sï   
    Return True if the given path is a symlink on the current remote host.

    If ``use_sudo`` is True, will use `.sudo` instead of `.run`.

    `.is_link` will, by default, hide all output. Give ``verbose=True`` to
    change this.
    s   test -L "$(echo %s)"R   R   N(   R   R   R   R   R
   t	   succeeded(   R   R   R   R   R   t   argst   kwargs(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   is_link(   s    	
c          O€  s(   x! |  D] } t  | |  r | Sq Wd S(   s¦   
    Given one or more file paths, returns first one found, or None if none
    exist. May specify ``use_sudo`` and ``verbose`` which are passed to
    `exists`.
    N(   R   (   R   R   t	   directory(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   first:   s    t    c         C€  sl  | r t  p t } |	 d k	 r3 t | d |	 ƒ} n  t t d ƒ d t ƒV | d t | ƒ ƒ j r | j	 d ƒ rz d n d } | | t
 j j |  ƒ 7} n  Wd QX| rÙ | d k rÙ t
 j t |  t ƒ ƒ j } t } n  d } | r¥yƒ | p÷ t
 j ƒ  } t | t ƒ } d d	 l m } m } | d
 | | ƒ d |
 ƒ } | j |  ƒ j | pRi    } | j d ƒ } Wqt k
 r¡d d l } | j ƒ  } t | d ƒ qXnm | rÃt
 j j | |  ƒ }  n  t |  t ƒ }  t t
 j j |  ƒ ƒ  } | j  ƒ  } Wd QX| r| | } n  | r;t! | ƒ r;| d t | ƒ ƒ n  t" d t# | ƒ d | d | d | d | d | ƒ S(   s§  
    Render and upload a template text file to a remote host.

    Returns the result of the inner call to `~fabric.operations.put` -- see its
    documentation for details.

    ``filename`` should be the path to a text file, which may contain `Python
    string interpolation formatting
    <http://docs.python.org/library/stdtypes.html#string-formatting>`_ and will
    be rendered with the given context dictionary ``context`` (if given.)

    Alternately, if ``use_jinja`` is set to True and you have the Jinja2
    templating library available, Jinja will be used to render the template
    instead. Templates will be loaded from the invoking user's current working
    directory by default, or from ``template_dir`` if given.

    The resulting rendered file will be uploaded to the remote file path
    ``destination``.  If the destination file already exists, it will be
    renamed with a ``.bak`` extension unless ``backup=False`` is specified.

    By default, the file will be copied to ``destination`` as the logged-in
    user; specify ``use_sudo=True`` to use `sudo` instead.

    The ``mirror_local_mode``, ``mode``, and ``temp_dir`` kwargs are passed
    directly to an internal `~fabric.operations.put` call; please see its
    documentation for details on these two options.

    The ``pty`` kwarg will be passed verbatim to any internal
    `~fabric.operations.run`/`~fabric.operations.sudo` calls, such as those
    used for testing directory-ness, making backups, etc.

    The ``keep_trailing_newline`` kwarg will be passed when creating
    Jinja2 Environment which is False by default, same as Jinja2's
    behaviour.

    .. versionchanged:: 1.1
        Added the ``backup``, ``mirror_local_mode`` and ``mode`` kwargs.
    .. versionchanged:: 1.9
        Added the ``pty`` kwarg.
    .. versionchanged:: 1.11
        Added the ``keep_trailing_newline`` kwarg.
    .. versionchanged:: 1.11
        Added the  ``temp_dir`` kwarg.
    t   ptyR   R   s
   test -d %st   /R   Niÿÿÿÿ(   t   Environmentt   FileSystemLoadert   loadert   keep_trailing_newlines   utf-8s&   
Unable to import Jinja2 -- see above.s   cp %s{,.bak}t
   local_patht   remote_pathR   t   mirror_local_modet   modet   temp_dir($   R   R   t   NoneR   R
   R   R   R	   R   t   endswitht   osR   t   basenamet   statR   t   envt   st_modet   Falset   getcwdt   jinja2R   R   t   get_templatet   rendert   encodet   ImportErrort	   tracebackt
   format_exct   abortt   joint   opent
   expandusert   readR   t   putR   (   t   filenamet   destinationt   contextt	   use_jinjat   template_dirR   t   backupR#   R$   R   R    R%   R   t   sept   textR   R   t   jenvR4   t   tbt	   inputfile(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   upload_templateE   sP    /#		s   .bakc         C€  sj  | r t  p t } x: d D]2 }	 | j |	 d |	 ƒ } | j |	 d |	 ƒ } q Wx$ d D] }	 | j |	 d |	 ƒ } qV W| r‰ d | } n  i d | | | | f d 6t |  ƒ d 6| d 6}
 t d	 d
 ƒ  t d ƒ } Wd QX| d k r.t j ƒ  } | j t j	 ƒ | j |  ƒ d | j
 ƒ  |
 d <d } n" | d k r@d n d |
 d <d } | |
 } | | d | ƒS(   s  
    Run a search-and-replace on ``filename`` with given regex patterns.

    Equivalent to ``sed -i<backup> -r -e "/<limit>/ s/<before>/<after>/<flags>g"
    <filename>``. Setting ``backup`` to an empty string will, disable backup
    file creation.

    For convenience, ``before`` and ``after`` will automatically escape forward
    slashes, single quotes and parentheses for you, so you don't need to
    specify e.g.  ``http:\/\/foo\.com``, instead just using ``http://foo\.com``
    is fine.

    If ``use_sudo`` is True, will use `sudo` instead of `run`.

    The ``shell`` argument will be eventually passed to `run`/`sudo`. It
    defaults to False in order to avoid problems with many nested levels of
    quotes and backslashes. However, setting it to True may help when using
    ``~fabric.operations.cd`` to wrap explicit or implicit ``sudo`` calls.
    (``cd`` by it's nature is a shell built-in, not a standalone command, so it
    should be called within a shell.)

    Other options may be specified with sed-compatible regex flags -- for
    example, to make the search and replace case insensitive, specify
    ``flags="i"``. The ``g`` flag is always specified regardless, so you do not
    need to remember to include it when overriding this parameter.

    .. versionadded:: 1.1
        The ``flags`` parameter.
    .. versionadded:: 1.6
        Added the ``shell`` keyword argument.
    s   /'s   \%ss   ()s   /%s/ s   '%ss/%s/%s/%sg't   scriptR<   RA   t   runningt   stdoutt   unameNt   NetBSDt   OpenBSDt   QNXs   /tmp/%st   tmps—   cp -p %(filename)s %(tmp)s \
&& sed -r -e %(script)s %(filename)s > %(tmp)s \
&& cp -p %(filename)s %(filename)s%(backup)s \
&& mv %(tmp)s %(filename)st   Darwins   -Es   -rt   extended_regexs>   sed -i%(backup)s %(extended_regex)s -e %(script)s %(filename)st   shell(   RL   RM   RN   (   R   R   t   replaceR	   R   t   hashlibt   sha1t   updateR+   t   host_stringt	   hexdigest(   R<   t   beforet   aftert   limitR   RA   t   flagsRR   R   t   charR>   t   platformt   hashert   exprt   command(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   sed¯   s0    !
	
t   #c         C€  s2   t  |  d d | d d d | d | d | d | ƒS(	   sï  
    Attempt to uncomment all lines in ``filename`` matching ``regex``.

    The default comment delimiter is `#` and may be overridden by the ``char``
    argument.

    This function uses the `sed` function, and will accept the same
    ``use_sudo``, ``shell`` and ``backup`` keyword arguments that `sed` does.

    `uncomment` will remove a single whitespace character following the comment
    character, if it exists, but will preserve all preceding whitespace.  For
    example, ``# foo`` would become ``foo`` (the single space is stripped) but
    ``    # foo`` would become ``    foo`` (the single space is still stripped,
    but the preceding 4 spaces are not.)

    .. versionchanged:: 1.6
        Added the ``shell`` keyword argument.
    RY   s   ^([[:space:]]*)%s[[:space:]]?RZ   s   \1R[   R   RA   RR   (   Rb   (   R<   t   regexR   R]   RA   RR   (    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt	   uncommentö   s    
c         C€  s   d \ } } | j  d ƒ r. d } | d } n  | j d ƒ rP d } | d  } n  d | | | f } t |  d | d d	 | d
 | d | d | ƒS(   s2  
    Attempt to comment out all lines in ``filename`` matching ``regex``.

    The default commenting character is `#` and may be overridden by the
    ``char`` argument.

    This function uses the `sed` function, and will accept the same
    ``use_sudo``, ``shell`` and ``backup`` keyword arguments that `sed` does.

    `comment` will prepend the comment character to the beginning of the line,
    so that lines end up looking like so::

        this line is uncommented
        #this line is commented
        #   this line is indented and commented

    In other words, comment characters will not "follow" indentation as they
    sometimes do when inserted by hand. Neither will they have a trailing space
    unless you specify e.g. ``char='# '``.

    .. note::

        In order to preserve the line being commented out, this function will
        wrap your ``regex`` argument in parentheses, so you don't need to. It
        will ensure that any preceding/trailing ``^`` or ``$`` characters are
        correctly moved outside the parentheses. For example, calling
        ``comment(filename, r'^foo$')`` will result in a `sed` call with the
        "before" regex of ``r'^(foo)$'`` (and the "after" regex, naturally, of
        ``r'#\1'``.)

    .. versionadded:: 1.5
        Added the ``shell`` keyword argument.
    R   t   ^i   t   $iÿÿÿÿs   %s(%s)%sRY   RZ   s   %s\1R   RA   RR   (   R   R   (   t
   startswithR'   Rb   (   R<   Rd   R   R]   RA   RR   t   carott   dollar(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   comment  s    #
c   	      C€  s¤   | r t  p t } | r: t | ƒ } | r: d | } q: n  t t d ƒ d t ƒL d | t |  ƒ f } | s‡ | j d d d ƒ } n  | | d | ƒj SWd	 QXd	 S(
   sÛ  
    Return True if ``filename`` contains ``text`` (which may be a regex.)

    By default, this function will consider a partial line match (i.e. where
    ``text`` only makes up part of the line it's on). Specify ``exact=True`` to
    change this behavior so that only a line containing exactly ``text``
    results in a True return value.

    This function leverages ``egrep`` on the remote end (so it may not follow
    Python regular expression syntax perfectly), and skips ``env.shell``
    wrapper by default.

    If ``use_sudo`` is True, will use `sudo` instead of `run`.

    If ``escape`` is False, no extra regular expression related escaping is
    performed (this includes overriding ``exact`` so that no ``^``/``$`` is
    added.)

    The ``shell`` argument will be eventually passed to ``run/sudo``. See
    description of the same argumnet in ``~fabric.contrib.sed`` for details.

    If ``case_sensitive`` is False, the `-i` flag will be passed to ``egrep``.

    .. versionchanged:: 1.0
        Swapped the order of the ``filename`` and ``text`` arguments to be
        consistent with other functions in this module.
    .. versionchanged:: 1.4
        Updated the regular expression related escaping to try and solve
        various corner cases.
    .. versionchanged:: 1.4
        Added ``escape`` keyword argument.
    .. versionadded:: 1.6
        Added the ``shell`` keyword argument.
    .. versionadded:: 1.11
        Added the ``case_sensitive`` keyword argument.
    s   ^%s$R   R   s   egrep "%s" %st   egreps   egrep -ii   RR   N(	   R   R   t   _escape_for_regexR
   R   R   R	   RS   R   (	   R<   RC   t   exactR   t   escapeRR   t   case_sensitiveR   t	   egrep_cmd(    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   containsJ  s    &c   	   
   C€  sÙ   | r t  p t } t | t ƒ r- | g } n  x¥ | D] } d t | ƒ | rS d n d } t |  d | ƒr™ | r™ t |  | d | d t d | ƒr™ q4 n  | r± | j d d ƒ n | } | d	 | t	 |  ƒ f ƒ q4 Wd
 S(   sA  
    Append string (or list of strings) ``text`` to ``filename``.

    When a list is given, each string inside is handled independently (but in
    the order given.)

    If ``text`` is already found in ``filename``, the append is not run, and
    None is returned immediately. Otherwise, the given text is appended to the
    end of the given ``filename`` via e.g. ``echo '$text' >> $filename``.

    The test for whether ``text`` already exists defaults to a full line match,
    e.g. ``^<text>$``, as this seems to be the most sensible approach for the
    "append lines to a file" use case. You may override this and force partial
    searching (e.g. ``^<text>``) by specifying ``partial=True``.

    Because ``text`` is single-quoted, single quotes will be transparently
    backslash-escaped. This can be disabled with ``escape=False``.

    If ``use_sudo`` is True, will use `sudo` instead of `run`.

    The ``shell`` argument will be eventually passed to ``run/sudo``. See
    description of the same argumnet in ``~fabric.contrib.sed`` for details.

    .. versionchanged:: 0.9.1
        Added the ``partial`` keyword argument.
    .. versionchanged:: 1.0
        Swapped the order of the ``filename`` and ``text`` arguments to be
        consistent with other functions in this module.
    .. versionchanged:: 1.0
        Changed default value of ``partial`` kwarg to be ``False``.
    .. versionchanged:: 1.4
        Updated the regular expression related escaping to try and solve
        various corner cases.
    .. versionadded:: 1.6
        Added the ``shell`` keyword argument.
    Rf   R   Rg   R   Ro   RR   t   's   '\\''s   echo '%s' >> %sN(
   R   R   t
   isinstancet
   basestringRm   R   Rr   R-   RS   R	   (	   R<   RC   R   R   Ro   RR   R   t   lineRd   (    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   append|  s    & 	c         C€  sI   t  j |  ƒ } | j d d ƒ } | j d d ƒ } | j d d ƒ } | S(   s5   Escape ``text`` to allow literal matching using egreps   \\s   \\\s   \$s   \\\$s   \'Rs   (   t   reRo   RS   (   RC   Rd   (    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyRm   ¯  s
    c         C€  s   d |  S(   Ns   "$(echo %s)"(    (   R   (    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyR	   º  s    (   t   __doc__t
   __future__R    RT   t   tempfileRx   R(   R   t	   functoolsR   t
   fabric.apit   fabric.utilsR   R-   R   R   R   R&   R   RG   Rb   Re   Rk   Rr   Rw   Rm   R	   (    (    (    sb   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/contrib/files.pyt   <module>   s6   
	h	F		4	1	2	