ó
Òbc           @@  sŽ  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
 m Z m Z m Z d d	 l m Z d d
 l m Z d  d l m Z m Z 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 Z e j d k Z d d d g Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# e$ d d „ Z& e$ d i  d „ Z' d „  Z( d S(   i    (   t   absolute_importi   (   t   S3(   t   Config(   t   S3Uri(   t   FileDict(   t   dateS3toUnixt   dateRFC822toUnix(   t	   unicodiset   deunicodiset   deunicodise_st   replace_nonprintables(   t   ParameterError(   t	   HashCache(   t   debugt   infot   warningNi   t   fetch_local_listt   fetch_remote_listt   compare_filelistsc         c@  s  y t  j t |  ƒ ƒ } Wn t k
 r- d SXg  g  } } xp | D]h } t | ƒ } t  j j t t  j j |  | ƒ ƒ ƒ r t |  | ƒ sª | j	 | ƒ qª qB | j	 | ƒ qB W|  | | f VxZ | D]R } t  j j |  | ƒ } t  j j
 t | ƒ ƒ sÃ x t | ƒ D] } | VqWqÃ qÃ Wd S(   s_   
    Reimplementation of python's os.walk to nicely support unicode in input as in output.
    N(   t   ost   listdirR   t	   ExceptionR   t   patht   isdirt   joint   handle_exclude_include_walk_dirt   appendt   islinkt   _os_walk_unicode(   t   topt   namest   dirst   nondirst   namet   new_patht   x(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyR   !   s"    'c         @  sL  t  ƒ  } d } t j j |  | d ƒ } t d | ƒ t } xa | j D]V ‰  t ‡  f d †  | Dƒ ƒ sm qE n  ˆ  j | ƒ rE t	 } t d | j
 ˆ  ƒ PqE qE W| r#x{ | j D]m ‰  t ‡  f d	 †  | Dƒ ƒ s× q¯ n  t d
 | ˆ  j f ƒ ˆ  j | ƒ r¯ t } t d | j ˆ  ƒ Pq¯ q¯ Wn  | r:t d | ƒ n t d | ƒ | S(   s
  
    Should this root/dirname directory be excluded? (otherwise included by default)
    Exclude dir matches in the current directory
    This prevents us from recursing down trees we know we want to ignore
    return True for excluding, and False for including
    u   /)$u   /)\Zu   \/$u	   \/\Z(?ms)t    u	   CHECK: %rc         3@  s!   |  ] } ˆ  j  j | ƒ Vq d  S(   N(   t   patternt   endswith(   t   .0t   dp(   t   r(    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pys	   <genexpr>I   s    u   EXCL-MATCH: '%s'c         3@  s!   |  ] } ˆ  j  j | ƒ Vq d  S(   N(   R%   R&   (   R'   R(   (   R)   (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pys	   <genexpr>S   s    u   INCL-TEST: %s ~ %su   INCL-MATCH: '%s'u   EXCLUDE: %ru   PASS: %r(   u   /)$u   /)\Zu   \/$u	   \/\Z(?ms)(   R   R   R   R   R   t   Falset   excludet   anyt   searcht   Truet   debug_excludet   includeR%   t   debug_include(   t   roott   dirnamet   cfgt   directory_patternst   dt   excluded(    (   R)   sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyR   :   s2    	c   
      c@  sP  t  j j t |  ƒ ƒ s t ‚ t |  g ƒ } xÝ t |  ƒ D]Ï \ } } } t t  j j t | ƒ ƒ ƒ } xŸ | D]— } t  j j	 | | ƒ } t t  j j t | ƒ ƒ ƒ } t  j j
 t | ƒ ƒ rn | | k sä | j | t  j j ƒ rõ t d | ƒ q| j | ƒ qn qn Wq: Wx< | D]4 }	 x+ t |	 ƒ D] \ } } } | | | f Vq'WqWd S(   sÑ   
    Walk filesystem, following symbolic links (but without recursion), on python2.4 and later

    If a symlink directory loop is detected, emit a warning and skip.
    E.g.: dir1/dir2/sym-dir -> ../dir2
    s+   Skipping recursively symlinked directory %sN(   R   R   R   R   t   AssertionErrort   setR   R   t   realpathR   R   t
   startswitht   sepR   t   add(
   R   t   walkdirst   dirpatht   dirnamest	   filenamest   real_dirpathR3   t   currentt   real_currentt   walkdir(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _fswalk_follow_symlinksb   s    c         c@  s2   x+ t  |  ƒ D] \ } } } | | | f Vq Wd S(   s\   
    Directory tree generator

    path (str) is the root of the directory tree to walk
    N(   R   (   R   R?   R@   RA   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _fswalk_no_symlinksz   s    c         C@  s!  t  d ƒ t ƒ  } t d t ƒ } xò |  j ƒ  D]ä } t  d | ƒ t } x? | j D]4 } | j | ƒ rS t } t  d | j | ƒ PqS qS W| rÖ xB | j	 D]4 } | j | ƒ r› t } t  d | j
 | ƒ Pq› q› Wn  | rt  d | ƒ |  | | | <|  | =q/ q/ t  d | ƒ q/ W|  | f S(   Nu   Applying --exclude/--includet   ignore_caseu	   CHECK: %su   EXCL-MATCH: '%s'u   INCL-MATCH: '%s'u   EXCLUDE: %su   PASS: %r(   R   R   R   R*   t   keysR+   R-   R.   R/   R0   R1   (   t   src_listR4   t   exclude_listt   fileR7   R)   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   filter_exclude_includeƒ   s0    
	c         C@  s¡  d „  } i  } xD|  j  D]9} zd  } | d k r[ t j t j j ƒ  d d d t ƒ} nO y t j t | ƒ d d ƒ} Wn- t	 k
 r© } t
 d | | j f ƒ w n Xx | D]… } t | ƒ j ƒ  } t j j t j j | | ƒ ƒ } t t j j t | ƒ ƒ ƒ } t t j j t | ƒ ƒ ƒ }	 | | | |	 ƒ q± WWd  | rQ| j ƒ  n  Xq Wg  }
 x> t | ƒ D]0 } | | } | j ƒ  |
 j | g  | f ƒ qiW|
 S(   Nc         S@  s1   | |  k r | g |  | <n |  | j  | ƒ d  S(   N(   R   (   R6   t   keyt   value(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _append¢   s    u   -t   modeR)   t   closefduJ   --files-from input file %s could not be opened for reading (%s), skipping.(   t
   files_fromt   Nonet   iot   opent   syst   stdint   filenoR*   R   t   IOErrorR   t   strerrorR   t   stripR   R   t   normpathR   R3   t   basenamet   closet   sortedt   sortR   (   R4   t
   local_pathRP   t   filelistt   fnamet   ft   et   lineR3   R^   t   resultRN   t   values(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _get_filelist_from_file¡   s4    	'!

c         @  sò  ‡  ‡ f d †  } ‡ ‡ f d †  } ‡ f d †  } t  ƒ  ‰ t ƒ  ‰  ˆ j r t j j t ˆ j ƒ ƒ r t j j t ˆ j ƒ ƒ d k r ˆ  j ˆ j ƒ n
 t	 d ƒ g  } t
 d t ƒ } t } t |  ƒ t t t f k ré |  g }  n  | d  k rˆ j } n  xo |  D]g }	 t |	 ƒ }
 |
 j d k s<t d |	 ƒ ‚ n  |
 j ƒ  rb| rbt d	 |	 ƒ ‚ n  | j |
 ƒ qWx& | D] }
 | | |
 ˆ  ƒ \ } } qzWt | ƒ d
 k r·t } n  t | ƒ \ } } | | ƒ } | ˆ  | ƒ | | | | f S(   Nc   	      @  sö  t  |  ƒ } d } t d | ƒ d } xÉ|  D]Á} | d 7} | d d k rd t d | | f ƒ n  | d k rv q- n  |  | d } y" t j t j t | ƒ ƒ ƒ } Wn. t k
 rÖ } | j t j k rÐ q- q× ‚  n X|  | j	 i | j
 d 6| j d	 6| j d
 6| j d 6| j d 6| j d 6| d 6ƒ | | j
 7} d ˆ j k r- ˆ  j | j | j | j | j
 ƒ } | d  k rÉy |  j | ƒ } Wn t k
 r q- n Xˆ  j | j | j | j | j
 | ƒ n  |  j | | j | j | | j
 ƒ q- q- W| S(   Ni    uY   Running stat() and reading/calculating MD5 values on %d files, this may take some time...i   iè  u   [%d/%d]t   -t	   full_namet   sizet   mtimet   devt   inodet   uidt   gidt   srt   md5(   t   lenR   R   t   stat_resultt   statR   t   OSErrort   errnot   ENOENTt   updatet   st_sizet   st_mtimet   st_devt   st_inot   st_uidt   st_gidt   sync_checksRt   RT   t   get_md5RZ   R=   t   record_hardlink(	   t   loc_listt   len_loc_listt
   total_sizet   countert   relative_fileRl   Rs   Rf   Rt   (   t   cacheR4   (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _fetch_local_list_infoÉ   sH    
"





$()c         @  s¿  t  d ƒ | j ƒ  d k r y t j ƒ  } t j ƒ  } Wn t k
 rW d } d } n Xi d d 6d d 6d d 6| d 6| d	 6d d
 6d d 6|  d <|  t f S| j ƒ  r| j ƒ  } | j ƒ  } ˆ rî t	 ˆ  j
 ƒ rî t ˆ  | ƒ } t } qNˆ  j rt | ƒ } n t | ƒ } t } n3 d } | j ƒ  } | g  | j ƒ  g f g } t } xd| D]\\ }	 }
 } |	 j | | d ƒ } x5| D]-} t j j |	 | ƒ } t j j t | ƒ ƒ sât j j t | ƒ ƒ r€t d | ƒ q€q€n  t j j t | ƒ ƒ rˆ  j st d | ƒ q€qn  t j j | | ƒ } t j j d k rbd j | j t j j ƒ ƒ } n  ˆ  j d k r€t | ƒ } n  | j d ƒ rœ| d } n  i | d 6|  | <q€WqUW|  | f S(   Nu    Compiling list of local files...Rk   i    Rl   iÿÿÿÿRm   Rn   Rq   Rr   Ro   Rp   R$   i   u"   Skipping over non regular file: %su   Skipping over symbolic link: %st   /t   normals   ./i   (   R   R^   R   t   geteuidt   getegidR   R.   R   R   Ru   RS   Rj   R*   t   follow_symlinksRF   RG   R3   t   replaceR   t   isfileR   t   existsR   R   R<   t   splitt   urlencoding_modeR
   R;   (   R…   t	   local_uriRŠ   Rq   Rr   t
   local_baseRb   Rc   t   single_fileR2   R   t   filest   rel_rootRe   Rl   R‰   (   R4   t   is_src(    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _get_filelist_localö   sj    


					!c         @  s»   ˆ  j  r· t ˆ  j ƒ d k r· |  j ƒ  t r= | j ƒ  } n | j ƒ  } xN | D]F } | j d d ƒ } | st qP n  |  j | d | | d | d ƒ qP W|  j	 ƒ  |  j
 ˆ  j  ƒ n  d  S(   Ni    Rp   Ro   Rn   Rm   (   t
   cache_fileRu   RS   t   mark_all_for_purget   PY3Ri   t
   itervaluest   gett   unmark_for_purget   purget   save(   RŠ   t
   local_listt   local_list_val_itert   f_infoRp   (   R4   (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _maintain_cache5  s    

i    u6   Cache file not found or empty, creating/populating it.RH   RL   s.   Expecting filename or directory instead of: %ss)   Use --recursive to upload a directory: %si   (   R   R   R   R   R   R’   R	   t   getsizet   loadR   R   R*   t   typet   listt   tupleR9   RT   t	   recursiveR   R   R   R   Ru   RM   (   t   argsR›   R®   R‹   Rœ   R¨   t
   local_urisR¥   R˜   t   argt   urit   list_for_uriRK   R‡   (    (   RŠ   R4   R›   sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyR   Ç   s<    -?		E
	c         @  s÷  ‡ f d †  ‰  t  ‡  ‡ f d † } t ƒ  ‰ g  } t d t ƒ } t |  ƒ t t t f k ri |  g }  n  | d  k r ˆ j	 } n  xI |  D]A } t
 | ƒ } | j d k s¼ t d | ƒ ‚ n  | j | ƒ qˆ Wd }	 | rGxü| D]` } | | d t  ƒ\ }
 } |	 | 7}	 x5 |
 D]- } |
 | | | <| j | |
 j | ƒ ƒ qWqà Wn‘xŽ| D]†} | j ƒ  } t j d | d	 d
 ƒ} t | ƒ d k r| \ } } d | k } | t
 | ƒ d | ƒ\ }
 } |	 | 7}	 x|
 D]4 } t j j |
 | d | ƒ rÑ|
 | | | <qÑqÑWqNt t j j t | j ƒ  ƒ ƒ ƒ } | sOt d | j ƒ  ƒ ‚ n  i | d 6| j ƒ  d 6| j ƒ  d 6} | rŒˆ  | | ƒ n  | | | <| j d ƒ } | r¾| j | | ƒ n  |	 | j d d ƒ 7}	 qNWt | ƒ \ } } | | |	 f S(   Nc         @  sÃ   t  ˆ  ƒ j |  ƒ } | j d ƒ s( d  S| j i t | d d ƒ d 6| d d j d ƒ d 6t | d d ƒ d 6ƒ y4 | d	 d } | j i | d 6ƒ t d
 | ƒ Wn t k
 r¾ n Xd  S(   Nt   headerss   content-lengthRm   t   etags   "'Rt   s   last-modifiedt	   timestamps   s3cmd-attrsu   retreived md5=%s from headers(	   R   t   object_infoR¡   R{   t   intR\   R   R   t   KeyError(   R²   t   remote_itemt   responseRt   (   R4   (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _get_remote_attribst  s    	c         @  s  t  d |  ƒ t j d ƒ } d } t t ƒ  ƒ } | j |  j ƒ  d |  j ƒ  d | d ˆ ƒ} |  j ƒ  } } |  } | d k rÇ | d d	 k rÇ | | j d	 ƒ d
  } t	 d |  j ƒ  | f ƒ }  n  t
 | ƒ }	 t d t ƒ }
 t } xŠ| d D]~} | d | k rc| d d d	 k rct t j j t | d ƒ ƒ ƒ } | j ƒ  } t } t d t ƒ }
 n | d |	 } |  j ƒ  | } | j | ƒ r t d ƒ qó n  i t | d ƒ d 6t | d ƒ d 6| d j d ƒ d 6| d d 6| d 6|  d 6d  d 6d  d 6|
 | <d |
 | d k r5ˆ  t	 | ƒ |
 | ƒ n  |
 | d } |
 j | | ƒ | t | d ƒ 7} | ró Pqó qó W|
 | f S(   Nu*   Retrieving list of remote files for %s ...s   \A\s*\Zi    t   prefixR®   t
   uri_paramsR$   iÿÿÿÿRŒ   i   u
   s3://%s/%sRH   R¬   t   Keyu(   Empty object name on S3 found, ignoring.t   SizeRm   t   LastModifiedR¶   t   ETags   "'Rt   t
   object_keyt   object_uri_strt   base_uriRo   Rp   Rk   (   R   t   ret   compileR   R   t   bucket_listt   buckett   objectt   rfindR   Ru   R   R*   R   R   R   R^   R   R²   R.   t   matchR   R¸   R   R\   RT   t
   record_md5(   t
   remote_uriR®   t   empty_fname_reR‡   t   s3R»   t   rem_base_originalt   rem_baset   remote_uri_originalt   rem_base_lent   rem_listt	   break_nowRÊ   RN   RÄ   Rt   (   R¼   R¾   (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _get_filelist_remote…  sT    $"
RH   RÐ   s    Expecting S3 URI instead of '%s'i    R®   s   \*|\?t   maxspliti   i   RŒ   RÄ   u3   Expecting S3 URI with a filename or --recursive: %sRÅ   RÃ   Rt   Rm   (   R.   R   R   R*   R«   R¬   R­   R9   RT   R®   R   R   R   RÍ   Rƒ   R²   RÆ   R”   Ru   t   globt   fnmatchR   R   R   R^   R   RÊ   R¡   RM   (   R¯   t   require_attribsR®   R¾   R×   t   remote_urist   remote_listR±   R²   R‡   t
   objectlistt   tmp_total_sizeRN   t   uri_strt   wildcard_split_resultR½   t   restt   need_recursionRº   Rt   RK   (    (   R¼   R4   R¾   sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyR   s  sb    B	
$
$
c         @  s
  d „  } ‡  ‡ f d †  } | t  k o0 | t  k s: t ‚ t d ƒ t ƒ  ‰  t d t  ƒ } g  } t d | | ƒ | | ƒ f ƒ x<|  j ƒ  D].} t d | ƒ | ˆ k r*ˆ  j rÚ t d | ƒ |  | =ˆ | =q n  y | |  ˆ | | | ƒ }	 WnD t t	 f k
 r<t d | ƒ t
 d	 | ƒ |  | =ˆ | =q n X|	 rbt d
 | ƒ |  | =ˆ | =q½y |  j | ƒ }
 Wn t k
 rŽd  }
 n X|
 d  k	 rû|
 ˆ j k rûˆ j |
 ƒ } t d | | f ƒ | j |  | | | |
 f ƒ |  | =ˆ | =q½ˆ j | |
 ƒ |  | | | <|  | =ˆ | =q y |  j | ƒ }
 Wn t k
 rVd  }
 n Xˆ j |
 ƒ } | d  k	 r­t d | | f ƒ | j |  | | | |
 f ƒ |  | =q ˆ j | |
 ƒ q Wx6 ˆ j ƒ  D]( } | |  k sì| | k rÎˆ | =qÎqÎW|  ˆ | | f S(   Nc         S@  s   |  r d p d S(   Nt   remotet   local(    (   t	   is_remote(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   __direction_str	  s    c   	      @  sÏ  t  } | |  k o | ˆ k sE t d | | |  k | ˆ k f ƒ t Sd ˆ  j k rÆ d ˆ | k rÆ d |  | k rÆ ˆ | d |  | d k rÃ t d | |  | d ˆ | d f ƒ t } qÃ qÆ n  d ˆ  j k } | r5| t  k rû d |  | d k s| t  k r5d ˆ | d k r5t } t d | ƒ q5n  | rË| rËy" |  j | ƒ } ˆ j | ƒ } Wn6 t t f k
 r›t d | ƒ t d | ƒ ‚  n X| | k rËt } t d	 | | | f ƒ qËn  | S(
   s@   Return True if src_list[file] matches dst_list[file], else FalseuE   %s: does not exist in one side or the other: src_list=%s, dst_list=%sRm   u'   xfer: %s (size mismatch: src=%s dst=%s)Rt   Rk   u   disabled md5 check for %su   IGNR: %s (disappeared)u   %s: file disappeared, ignoring.u&   XFER: %s (md5 mismatch: src=%s dst=%s)(	   R.   R   R*   R‚   R   Rƒ   RZ   Rx   R   (	   RJ   t   dst_lstt
   src_remotet
   dst_remoteRL   t   attribs_matcht   compare_md5t   src_md5t   dst_md5(   R4   t   dst_list(    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   _compare  s4    # '@u   Verifying attributes...RH   s)   Comparing filelists (direction: %s -> %s)u	   CHECK: %su   IGNR: %s (used --skip-existing)u   IGNR: %s (disappeared)u   %s: file disappeared, ignoring.u   IGNR: %s (transfer not needed)u   DST COPY src: %s -> %su   DST COPY dst: %s -> %s(   R*   R8   R   R   R   R   RI   t   skip_existingRZ   Rx   R   Rƒ   RT   t   by_md5t   find_md5_oneR   RÍ   (   RJ   Rï   Ré   Rê   Rç   Rð   t   update_listt
   copy_pairsR‰   t	   same_fileRt   t   dst1Re   (    (   R4   Rï   sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyR     sr    	'
	 	





(   i   i    ()   t
   __future__R    R   R   R   R   t	   BaseUtilsR   R   t   UtilsR   R   R	   R
   t
   ExceptionsR   R   t   loggingR   R   R   R   RW   RÙ   RÆ   Ry   RU   t   version_infoRŸ   t   __all__R   R   RF   RG   RM   Rj   R*   RT   R   R   R   (    (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/FileLists.pyt   <module>	   s4   "		(					&¬•