ó
ú2ec           @   sC  d  Z  d d l Z d d l m Z d d l m Z y d d l j Z Wn! e k
 rh d d l m Z n Xd d l Z d d l	 Z	 d d d d	 d
 d g Z
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d „  ƒ  YZ d d e d „ Z e d „ Z d d d „ Z d d d „ Z d „  Z d „  Z d S(   s  
Module to find differences over time in a filesystem

Basically this takes a snapshot of a directory, then sees what changes
were made.  The contents of the files are not checked, so you can
detect that the content was changed, but not what the old version of
the file was.
iÿÿÿÿN(   t   fnmatch(   t   datetime(   t   IterableUserDictt   Difft   Snapshott   Filet   Dirt   report_expected_diffst	   show_diffc           B   s8   e  Z d  Z d „  Z d „  Z d „  Z e e d „ Z RS(   s9   
    Represents the difference between two snapshots
    c         C   s    | |  _  | |  _ |  j ƒ  d  S(   N(   t   beforet   aftert
   _calculate(   t   selfR	   R
   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   __init__"   s    		c         C   s­   |  j  j } |  j j } i  |  _ i  |  _ | j ƒ  |  _ xm | j ƒ  D]_ \ } } | | k rq | |  j | <qF n  |  j | =| j | | j k  rF | | |  j | <qF qF Wd  S(   N(	   R	   t   dataR
   t   deletedt   updatedt   copyt   createdt   itemst   mtime(   R   R	   R
   t   patht   f(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   '   s    		
c         C   s
   |  j  ƒ  S(   N(   t   report(   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   __str__5   s    c      
   C   sà  g  } | r; | j  d |  j j |  j j |  j j f ƒ n  xsd |  j t f d |  j t f d |  j t f g D]>\ } } } | ro | j  d | ƒ | j	 ƒ  } | j
 ƒ  d } xù | D]î \ } }	 d t | | d d	 d
 t ƒ}
 | } | r|	 j d k r|
 d |	 j 7}
 n  | r™g  } |  j j | ƒ rJ| j  |  j | j ƒ n  |  j j | ƒ rv| j  |  j | j ƒ n  |
 d d j t t | ƒ ƒ 7}
 n  | j  |
 ƒ q¸ Wqo qo Wt | ƒ d k rÓ| j  d ƒ n  d j | ƒ S(   Ns   Difference in %s from %s to %s:R   R   R   s   -- %s: -------------------t    s     %st   indenti   t   include_seps   N/As     (%s bytes)s    (mtime: %s)s   ->i   s     (no changes)s   
(   t   appendR	   t	   base_patht
   calculatedR
   R   t   TrueR   R   R   t   sortt   _space_prefixt   Falset   sizet   getR   t   joint   mapt   reprt   len(   R   t   headert   datest   st   namet   filest	   show_sizet   lastR   R   t   tt   parts(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   8   s@    			"

#(	   t   __name__t
   __module__t   __doc__R   R   R   R   R"   R   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR      s
   			c           B   sq   e  Z d  Z d	 d
 d e d „ Z d „  Z d „  Z d „  Z d „  Z	 e
 j d	 d	 e d „ Z d „  Z d „  Z RS(   sx   
    Represents a snapshot of a set of files.  Has a dictionary-like
    interface, keyed relative to ``base_path``
    c         C   sY   | |  _  | |  _ | |  _ | |  _ d  |  _ | p6 i  |  _ | d  k rU |  j ƒ  n  d  S(   N(   R   t   ignore_wildcardst   ignore_hiddent   ignore_pathst   NoneR   R   t
   find_files(   R   R   R-   R5   R7   R6   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   a   s    					c         C   s&   |  j  d |  j ƒ t j ƒ  |  _ d S(   s_   
        Find all the files under the base path, and put them in
        ``self.data``
        R   N(   t   _find_traverseR   R   t   nowR   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR9   p   s    c         C   sf   | |  j  k r t S|  j r; t j j | ƒ j d ƒ r; t Sx$ |  j D] } t | | ƒ rE t SqE Wt	 S(   Nt   .(
   R7   R   R6   t   osR   t   basenamet
   startswithR5   R    R"   (   R   t   fnt   pat(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   _ignore_filex   s    $c         C   sº   t  j j |  j | ƒ } t  j j | ƒ r  | rI t |  j | ƒ | | <n  xj t  j | ƒ D]@ } t  j j | | ƒ } |  j | ƒ r‰ qY n  |  j | | ƒ qY Wn t	 |  j | ƒ | | <d  S(   N(
   R=   R   R%   R   t   isdirR   t   listdirRB   R:   R   (   R   R   t   resultt   fullR@   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR:   ‚   s    c         C   s#   d |  j  j |  j |  j p d f S(   Ns   <%s in %r from %r>s   (no calculation done)(   t	   __class__R2   R   R   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   __repr__   s    c         C   sA  i  } x4| D],} | } | j  d ƒ } | |  j k rb | rL | | ƒ }	 n d }	 |	 | | <q n  | | }
 |  j | } | j } | | |
 ƒ s&| r© | | |
 ƒ }	 np t | ƒ t |
 ƒ k  rÞ d t |
 ƒ t | ƒ }	 n; t | ƒ t |
 ƒ k rd t | ƒ t |
 ƒ }	 n d }	 |	 | | <q | r d | | <q q W| S(   sè  
        Compares a dictionary of ``path: content`` to the
        found files.  Comparison is done by equality, or the
        ``comparison(actual_content, expected_content)`` function given.

        Returns dictionary of differences, keyed by path.  Each
        difference is either noted, or the output of
        ``differ(actual_content, expected_content)`` is given.

        If a file does not exist and ``not_found`` is given, then
        ``not_found(path)`` is put in.
        t   /s	   not founds   differ (%i bytes smaller)s   differ (%i bytes larger)s   diff (same size)s   same!(   t   stripR   t   bytesR(   (   R   t   expectedt
   comparisont   differt	   not_foundt   include_successRE   R   t	   orig_patht   msgt   expected_contentt   filet   actual_content(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   compare_expected•   s6    

	c         C   s   t  |  |  j ƒ  ƒ S(   N(   R   t   clone(   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   diff_to_nowÃ   s    c      	   C   s.   |  j  d |  j d |  j d |  j d |  j ƒ S(   NR   R5   R7   R6   (   RG   R   R5   R7   R6   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyRW   Æ   s    		N(    (    (   R2   R3   R4   R8   R   R   R9   RB   R:   RH   t   operatort   eqR"   RV   RX   RW   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   Z   s   		
		,	c           B   sS   e  Z d  Z e Z e Z d „  Z d „  Z e	 e ƒ Z
 d „  Z d „  Z d „  Z RS(   s  
    Represents a single file found as the result of a command.

    Has attributes:

    ``path``:
        The path of the file, relative to the ``base_path``

    ``full``:
        The full path

    ``stat``:
        The results of ``os.stat``.  Also ``mtime`` and ``size``
        contain the ``.st_mtime`` and ``st_size`` of the stat.

    ``bytes``:
        The contents of the file.

    You may use the ``in`` operator with these objects (tested against
    the contents of the file), and the ``.mustcontain()`` method.
    c         C   sj   | |  _  | |  _ t j j | | ƒ |  _ t j |  j ƒ |  _ |  j j |  _ |  j j |  _	 d  |  _ d  S(   N(   R   R   R=   R%   RF   t   statt   st_mtimeR   t   st_sizeR#   R8   t   _bytes(   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   ç   s    		c         C   sD   |  j  d  k r= t |  j d ƒ } | j ƒ  |  _  | j ƒ  n  |  j  S(   Nt   rb(   R^   R8   t   openRF   t   readt   close(   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt
   bytes__getð   s
    c         C   s   | |  j  k S(   N(   RK   (   R   R+   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   __contains__ø   s    c         C   sB   t  } |  j } | | k r> d | GH| GH| | k s> t ‚ n  d  S(   Ns   Could not find %r in:(   R   RK   t   AssertionError(   R   R+   t   __tracebackhide__RK   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   mustcontainû   s    		c         C   s   d |  j  j |  j |  j f S(   Ns
   <%s %s:%s>(   RG   R2   R   R   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyRH     s    	(   R2   R3   R4   R   RT   R"   t   dirR   Rc   t   propertyRK   Rd   Rg   RH   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   Ì   s   					c           B   sA   e  Z d  Z e Z e Z d „  Z d „  Z d „  Z	 e
 e	 ƒ Z RS(   s6   
    Represents a directory created by a command.
    c         C   s@   | |  _  | |  _ t j j | | ƒ |  _ d |  _ d |  _ d  S(   Ns   N/A(   R   R   R=   R%   RF   R#   R   (   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR     s
    			c         C   s   d |  j  j |  j |  j f S(   Ns
   <%s %s:%s>(   RG   R2   R   R   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyRH     s    	c         C   s   t  d |  ƒ ‚ d  S(   Ns!   Directory %r doesn't have content(   t   NotImplementedError(   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyRc     s    (   R2   R3   R4   R"   RT   R   Rh   R   RH   Rc   Ri   RK   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR     s   			c         C   s  | d k r t j j } n  |  j | ƒ }  | j | ƒ } g  } xƒ |  rÄ | rÄ |  d | d k rÄ | d k r– | j d t | d ƒ t | ƒ ƒ n | j d | ƒ | j d ƒ |  j d ƒ qB W| r| rï d j | ƒ | | j | ƒ Sd j | ƒ | j | ƒ Sn | j | ƒ Sd S(   s¸   
    Anything shared by pref and full will be replaced with spaces
    in full, and full returned.

    Example::

        >>> _space_prefix('/foo/bar', '/foo')
        '    /bar'
    i    t    R   N(	   R8   R=   R   t   sept   splitR   R(   t   popR%   (   t   prefRF   Rl   R   R   t   padding(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR!   $  s     
#(c   	      C   s­  |  s
 d S|  j  ƒ  }  |  j ƒ  g  } d } xZ|  D]R\ } } t | | d d d t ƒ} | rr t | d ƒ } n  | } t | j ƒ  ƒ d k rjt t j d | ƒ j	 d	 ƒ ƒ } t
 | d
 | ƒ } | rY| d 7} x | j ƒ  D]q } | j ƒ  j d ƒ rt | d ƒ } n6 | j ƒ  j d ƒ r5t | d ƒ } n t | d ƒ } | | d 7} qá Wqx| d | 7} n | d | 7} | j | ƒ q3 W| j d t |  ƒ ƒ d j | ƒ S(   sh   
    Takes the output of compare_expected, and returns a string
    description of the differences.
    s   No differencesR   R   i   R   i   i   s   ^[ ]*i    i   s   
t   +i
   t   -i	   i   Rk   s   Files with differences: %s(   R   R    R!   R"   t
   color_lineR(   t
   splitlinest   ret   searcht   groupR   RJ   R?   R   R%   (	   t   diffst   colorizeR+   R/   R   t   descR0   t
   cur_indentt   line(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   B  s8    
!
c         C   sÆ   |  d k r | d k r d Sg  } |  d k r> | j d ƒ nF |  d k ro | j d ƒ | j d d |  ƒ n | j d d |  ƒ | d k r  | j d ƒ n | j d d	 | ƒ d
 d
 j | ƒ S(   s   
    0  black
    1  red
    2  green
    3  yellow
    4  blue
    5  magenta (purple)
    6  cyan
    7  white (gray)

    Add 8 to get high-intensity
    s   [0ms   [39mi   s   [1ms   [%imi   i   s   [49mi(   s   N(   R8   R   R%   (   t
   foregroundt
   backgroundt   codes(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt
   color_codeg  s    c         C   sA   t  j d |  ƒ } | j d ƒ t | | ƒ |  | j ƒ  t ƒ  S(   Ns   ^(\s*)i   (   Ru   Rv   Rw   R€   t   end(   R|   R}   R~   t   match(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyRs   …  s    c         C   s.   d j  g  | j ƒ  D] } d |  | ^ q ƒ S(   Ns   
Rk   (   R%   Rt   (   R   t   textt   l(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   Š  s    c         C   sÓ   g  |  j  ƒ  D] } | j ƒ  r | j ƒ  ^ q } g  | j  ƒ  D] } | j ƒ  r> | j ƒ  ^ q> } t | ƒ t | ƒ k o… d k n r  d | d | d f S| s® d | Sd d  l } d j | j | | ƒ ƒ S(   Ni   s	   %r not %ri    s   Empty; should have:
iÿÿÿÿs   
(   Rt   RJ   R(   t   difflibR%   t   ndiff(   RU   RS   R„   t   actual_linest   expected_linesR…   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyR   Ž  s    ((   R4   R=   R    R   t   collections.UserDictt   UserDictR   t   ImportErrorRY   Ru   t   __all__t   objectR   R   R   R   R8   R   R!   R"   R   R€   Rs   R   R   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/fsdiff.pyt   <module>
   s*   	>r<%	