ó
Òbc           @`  sÅ  d  d l  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	 d  d l
 m Z d  d l m Z y e Wn e k
 rŸ e Z n Xd  d l Z d  d l Z d  d l m Z m Z m Z m Z g  Z e e d „ Z e j d ƒ d „  Z e j d	 ƒ e j e j Z  e! e  ƒ Z" d
 „  Z# e j d ƒ d „  Z$ e j d ƒ e j% d d ƒ d d d „ Z& e j d ƒ e j% d d ƒ d d d „ Z' e j d ƒ d „  Z( e j d ƒ d „  Z) e j d ƒ d d e d „ Z+ e j d ƒ d d d „ Z, e j d  ƒ d d e d! „ Z- e j d" ƒ d d d# „ Z. e j d$ ƒ d d% „ Z/ e j d& ƒ d e d' „ Z0 e j d( ƒ d) „  Z1 e j d* ƒ d+ „  Z2 e3 d, „ Z4 e j d- ƒ d. „  Z5 e j d/ ƒ d0 „  Z6 e j d1 ƒ d2 „  Z7 e j d3 ƒ d4 „  Z8 e j d5 ƒ d6 „  Z9 e j d7 ƒ y d  d l: Z: d8 „  Z; Wn& e< k
 rpd  d l= Z= d9 „  Z; n Xe j d: ƒ y d  d l> Z> d; „  Z? Wn e< k
 r³d< „  Z? n Xe j d= ƒ d S(>   i    (   t   absolute_importt   divisionN(   t   md5(   t   debug(   t   base_urlencode_stringt   base_replace_nonprintablest   base_unicodiset   base_deunicodisec         C`  s˜   | r t  |  ƒ p t |  ƒ }  | rŠ d d d d g } d } x) |  d k rg |  d }  | j d ƒ } q? W| rz t  |  ƒ pƒ t |  ƒ | f S|  d f Sd  S(	   Nt   Kt   Mt   Gt   Tt    i   i   i    (   t   floatt   intt   pop(   t   sizet   human_readablet   floating_pointt   coeffst   coeff(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt
   formatSize'   s    
"R   c         C`  s3   i  } x& |  D] } | d | | d j  ƒ  <q W| S(   sL   
    Header keys are always in lowercase in python2 but not in python3.
    i   i    (   t   lower(   t   listt   retvalt   tuple(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   convertHeaderTupleListToDict5   s    R   c         C`  sE   d } x8 |  d k r@ | t  t j d t d ƒ 7} |  d 8}  q	 W| S(   NR   i    i   (   t
   _rnd_charst   randomt   randintt   _rnd_chars_len(   t   lenR   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   rndstrA   s
    R    c         C`  s   t  j d ƒ } d } xt | d k r‹ |  t | ƒ } y | | ƒ PWn8 t k
 r} } | j t j k r~ t  j | ƒ ‚  q~ n X| d 8} q Wt  j | ƒ | S(   Ni?   i   i    i   (   t   ost   umaskR    t   OSErrort   errnot   EEXIST(   t   prefixt	   randcharst
   createfunct	   old_umaskt   triest   dirnamet   e(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   mktmpsomethingJ   s    

R-   t   TMPs   /tmps   /tmpdir-i
   c         C`  s   t  |  | t j ƒ S(   N(   R-   R!   t   mkdir(   R&   R'   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   mktmpdir]   s    R0   s	   /tmpfile-i   c         C`  s   d „  } t  |  | | ƒ S(   Nc         S`  s)   t  j t  j t |  ƒ t  j t  j Bƒ ƒ S(   N(   R!   t   closet   opent   deunicodiset   O_CREATt   O_EXCL(   t   filename(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   <lambda>c   s    (   R-   (   R&   R'   R(   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt	   mktmpfileb   s    	R8   c         C`  sd   t  ƒ  } t t |  ƒ d ƒ : } x0 t rS | j d ƒ } | sC Pn  | j | ƒ q$ WWd  QX| j ƒ  S(   Nt   rbi    i   i €  (   R   R2   R3   t   Truet   readt   updatet	   hexdigest(   R6   t   ht   fpt   data(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   hash_file_md5h   s    		RA   c         C`  s  |  j  t j ƒ } g  } xB | r\ t j j t t j j | ƒ ƒ ƒ r\ | j | j ƒ  ƒ q Wx¯ | r| j | j ƒ  ƒ t j j | ƒ } y% t	 d | ƒ t j
 t | ƒ ƒ Wq` t t f k
 rã } t	 d | | j f ƒ t St k
 r
} t	 d | | f ƒ t SXq` Wt S(   s‘   
    mkdir_with_parents(dst_dir)

    Create directory 'dir_name' with all parent directories

    Returns True on success, False otherwise.
    s	   mkdir(%s)s(   Can not make directory '%s' (Reason: %s)(   t   splitR!   t   sept   patht   isdirR3   t   joint   appendR   R   R/   R#   t   IOErrort   strerrort   Falset	   ExceptionR:   (   t   dir_namet   pathmemberst	   tmp_stackt   cur_dirR,   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   mkdir_with_parentsu   s"    .		RP   t   replacec         C`  s.   | s t  j j ƒ  j } n  t |  | | | ƒ S(   N(   t   S3t   Configt   encodingR   (   t   stringRT   t   errorst   silent(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt	   unicodise   s    RX   c         C`  s   t  |  | | t ƒ S(   s.   
    Alias to silent version of unicodise
    (   RX   R:   (   RU   RT   RV   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   unicodise_s—   s    RY   c         C`  s.   | s t  j j ƒ  j } n  t |  | | | ƒ S(   N(   RR   RS   RT   R   (   RU   RT   RV   RW   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyR3   Ÿ   s    R3   c         C`  s   t  |  | | t ƒ S(   s0   
    Alias to silent version of deunicodise
    (   R3   R:   (   RU   RT   RV   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   deunicodise_s¦   s    RZ   c         C`  s"   t  t |  | ƒ | ƒ j d d ƒ S(   ss   
    Convert 'string' to Unicode according to current encoding
    and replace all invalid characters with '?'
    u   ï¿½t   ?(   RX   R3   RQ   (   RU   RT   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   unicodise_safe®   s    R\   c         C`  s1   | d  k r! t j j ƒ  j } n  t |  | | ƒ S(   N(   t   NoneRR   RS   t   urlencoding_modeR   (   RU   R^   t   unicode_output(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   urlencode_string¹   s    R`   c         C`  s%   t  j j ƒ  j d k } t |  | ƒ S(   s   
    replace_nonprintables(string)

    Replaces all non-printable characters 'ch' in 'string'
    where ord(ch) <= 26 with ^@, ^A, ... ^Z
    t	   fixbucket(   RR   RS   R^   R   (   RU   t   warning_message(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   replace_nonprintablesÀ   s    Rc   c         C`  sX  t  |  t ƒ r |  St  |  t ƒ s4 t  |  t j ƒ rG t t j |  ƒ ƒ St |  d ƒ ro t t j |  j ƒ  ƒ ƒ St |  d ƒ r‘ t |  j d ƒ ƒ St  |  t	 ƒ s¾ t  |  t
 ƒ s¾ t  |  t ƒ r>y5 |  j d ƒ rè t j ƒ  t |  d ƒ St |  ƒ SWq>t k
 r:y t j |  ƒ SWq;t k
 r6} t d | ƒ q;Xq>Xn  t j j d |  ƒ ‚ d S(	   s­   Convert time specified in a variety of forms into UNIX epoch time.
    Accepts datetime.datetime, int, anything that has a strftime() method, and standard time 9-tuples
    t	   timetuplet   strftimes   %st   +i   s&   Failed to parse date with strptime: %ss}   Unable to convert %r to an epoch time. Pass an epoch time. Try `date -d 'now + 1 year' +%%s` (shell) or time.mktime (Python).N(   t
   isinstanceR   R   t   timet   struct_timet   mktimet   hasattrRd   Re   t   strt   unicodet   bytest
   startswitht
   ValueErrort   strptimeR   RR   t
   Exceptionst   ParameterError(   t   tt   ex(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   time_to_epochÌ   s(    !-c         C`  sé  | rP t  j d |  t  j ƒ } | r— t j j d |  | j ƒ  d f ƒ ‚ q— nG t  j d |  t  j ƒ } | r— t j j d |  | j ƒ  d f ƒ ‚ n  t |  ƒ d k  rÂ t j j d |  ƒ ‚ n  t |  ƒ d k rí t j j d	 |  ƒ ‚ n  | råt |  ƒ d
 k rt j j d |  ƒ ‚ n  t  j d |  t  j ƒ rOt j j d |  ƒ ‚ n  t  j d |  t  j ƒ r€t j j d |  ƒ ‚ n  t  j d |  t  j ƒ s±t j j d |  ƒ ‚ n  t  j d |  t  j ƒ såt j j d |  ƒ ‚ qån  t S(   Ns   ([^a-z0-9\.-])s™   Bucket name '%s' contains disallowed character '%s'. The only supported ones are: lowercase us-ascii letters (a-z), digits (0-9), dot (.) and hyphen (-).i    s   ([^A-Za-z0-9\._-])s¤   Bucket name '%s' contains disallowed character '%s'. The only supported ones are: us-ascii letters (a-z, A-Z), digits (0-9), dot (.), hyphen (-) and underscore (_).i   s0   Bucket name '%s' is too short (min 3 characters)iÿ   s1   Bucket name '%s' is too long (max 255 characters)i?   s0   Bucket name '%s' is too long (max 63 characters)s   -\.sE   Bucket name '%s' must not contain sequence '-.' for DNS compatibilitys   \.\.sE   Bucket name '%s' must not contain sequence '..' for DNS compatibilitys	   ^[0-9a-z]s4   Bucket name '%s' must start with a letter or a digits	   [0-9a-z]$s2   Bucket name '%s' must end with a letter or a digit(	   t   ret   searcht   UNICODERR   Rr   Rs   t   groupsR   R:   (   t   buckett
   dns_strictt   invalid(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   check_bucket_nameî   s.    ,)R~   c         C`  s3   y t  |  d t ƒSWn t j j k
 r. t SXd  S(   NR|   (   R~   R:   RR   Rr   Rs   RJ   (   R{   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt    check_bucket_name_dns_conformity
  s    R   c         C`  s   d |  k r t  St | ƒ S(   sk   
    Check whether either the host_bucket support buckets and
    either bucket name is dns compatible
    s
   %(bucket)s(   RJ   R   (   t   bucket_hostt   bucket_name(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   check_bucket_name_dns_support  s    R‚   c         C`  s€   d t  j j ƒ  j k r" |  t f St  j j ƒ  j j ƒ  i d d 6} t j | |  t j ƒ } | sm |  t f S| j d ƒ t	 f S(   sÎ   
    bucket, success = getBucketFromHostname(hostname)

    Only works for hostnames derived from bucket names
    using Config.host_bucket pattern.

    Returns bucket name and a boolean success flag.
    s
   %(bucket)ss   (?P<bucket>.*)R{   i   (
   RR   RS   t   host_bucketRJ   R   Rw   t   matchRy   t   groupR:   (   t   hostnamet   patternt   m(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   getBucketFromHostname  s    	
#
R‰   c         C`  s!   t  j j ƒ  j j ƒ  i |  d 6S(   NR{   (   RR   RS   Rƒ   R   (   R{   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   getHostnameFromBucket3  s    RŠ   c         C`  s”   t  ƒ  } | } |  d k r} | j | ƒ x_ | d k ry | j t | | ƒ ƒ } | sY Pn  | j | ƒ | t | ƒ 8} q+ Wn | j |  ƒ | j ƒ  S(   NR   i    (   R   t   seekR;   t   minR<   R   R=   (   t   buffert   mfilet   offsett
   chunk_sizet
   send_chunkt   md5_hasht	   size_leftR@   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   calculateChecksum8  s    	R”   c         C`  s   t  t j |  ƒ j ƒ S(   s>   returns a username from the password databse for the given uid(   RY   t   pwdt   getpwuidt   pw_name(   t   uid(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   getpwuid_usernameM  s    c         C`  s   t  t j ƒ  ƒ S(   N(   RY   t   getpasst   getuser(   R˜   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyR™   R  s    R™   c         C`  s   t  t j |  ƒ j ƒ S(   s<   returns a groupname from the group databse for the given gid(   RY   t   grpt   getgrgidt   gr_name(   t   gid(    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   getgrgid_grpnameX  s    c         C`  s   d S(   Nu   nobody(    (   RŸ   (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyR    \  s    R    (@   t
   __future__R    R   R!   Rh   Rw   RU   t
   string_modR   R$   t   hashlibR   t   loggingR   Rm   t	   NameErrorRl   t	   S3.ConfigRR   t   S3.Exceptionst   S3.BaseUtilsR   R   R   R   t   __all__RJ   R   RG   R   t   ascii_letterst   digitsR   R   R   R    R-   t   getenvR0   R8   RA   RP   R]   RX   RY   R3   RZ   R\   R`   Rc   Rv   R:   R~   R   R‚   R‰   RŠ   R”   R•   R™   t   ImportErrorRš   Rœ   R    (    (    (    sH   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Utils.pyt   <module>	   s”   
"				
				"						