
bc           @@  sb  d  d l  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 Z d  d l	 Z	 d  d l
 Z
 d  d l m Z m Z d d l m Z y d  d l Z d  d l Z Wn; e k
 r e j j d  e j j   e j e  n Xy d  d l Z Wn  e k
 r.d  d l j Z n Xy, d  d l m Z m Z m Z m Z m Z  Wn9 e k
 rd  d l m Z m Z m Z m Z m Z  n Xd d l! m" Z" d d	 l# m# Z# d d
 l$ m% Z% m& Z& m' Z' m( Z( m) Z) y e* Wn e+ k
 r e, Z* n Xd   Z- d   Z. d   Z/ d e0 f d     YZ1 d e0 f d     YZ d e0 f d     YZ2 d S(   i    (   t   absolute_importN(   t   debugt   warningi   (   t	   EX_OSFILEu:  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ImportError trying to import dateutil.parser and dateutil.tz.
Please install the python dateutil module:
$ sudo apt-get install python-dateutil
  or
$ sudo yum install python-dateutil
  or
$ pip install python-dateutil
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(   t   NoOptionErrort   NoSectionErrort   MissingSectionHeaderErrort   ParsingErrort   ConfigParser(   t   Progress(   t
   SortedDict(   t   s3_quotet   getTreeFromXmlt   getDictFromTreet   base_unicodiset   dateRFC822toPythonc         C@  sL   t  |   t k r" |  j   d k St  |   t k rD |  t k rD t St Sd S(   ss   Check to see if a string is true, yes, on, or 1

    value may be a str, or unicode.

    Return True if it is
    t   truet   yest   ont   1N(   R   R   R   R   (   t   typet   unicodet   lowert   boolt   Truet   False(   t   value(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   is_bool_trueI   s
    c         C@  sL   t  |   t k r" |  j   d k St  |   t k rD |  t k rD t St Sd S(   st   Check to see if a string is false, no, off, or 0

    value may be a str, or unicode.

    Return True if it is
    t   falset   not   offt   0N(   R   R   R   R   (   R   R   R   R   R   R   (   R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   is_bool_falseX   s
    c         C@  s   t  |   p t |   S(   s)   Check a string value to see if it is bool(   R   R    (   R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   is_boolg   s    t   Configc        	   B@  sB  e  Z d0 Z g  Z i  Z d  Z d  Z d  Z e	 Z
 d0 Z d0 Z d Z d Z d  Z d Z d Z e j Z e j j   Z e j Z d1 Z d2 Z e Z e Z  e Z! e" d e	  Z# e Z$ e Z% d0 Z& e Z' e Z( d  Z) e Z* e Z+ d Z, d	 Z- d0 Z. g  Z/ g  Z0 d  Z1 d
 Z2 e Z3 e Z4 d  Z5 e	 Z6 d d d d d d d d d g	 Z7 e Z8 e Z9 e Z: d Z; d Z< d e d <e Z= d  Z> d  Z? d Z@ d ZA e	 ZB d  ZC d  ZD d  ZE e	 ZF e	 ZG d ZH d ZI e	 ZJ e	 ZK d  ZL e	 ZM d ZN d3 ZO d ZP d d g ZQ g  ZR g  ZS i  ZT i  ZU eV jW   pd ZX d  ZY d  ZZ e Z[ d  Z\ e Z] d! Z^ e Z_ e Z` e	 Za d" Zb d  Zc d# Zd g  Ze g  Zf d  Zg d  Zh g  Zi d  Zj d  Zk d  Zl e Zm d$ Zn e Zo e Zp d  Zq d  Zr e Zs e Zt e Zu d% Zv e Zw e	 Zx d& Zy d0 d0 d0 d0 d'  Zz d0 d0 d0 d0 d(  Z{ d)   Z| d*   Z} d+   Z~ d,   Z d-   Z d.   Z d/   Z RS(4   u    u   s3.amazonaws.comu   %(bucket)s.s3.amazonaws.comu   sdb.amazonaws.comu   cloudfront.amazonaws.comi@   i   t   ignore_casei   u   Standardi8  u   unameu   uidu   gnameu   gidu   atimeu   mtimeu   ctimeu   modeu   md5iu@   [sync] Remove remote S3 objects when local file has been deletedt   delete_removedu}   %(gpg_command)s -c --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)su}   %(gpg_command)s -d --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)su   USu   binary/octet-streami   i'  t   sizet   md5s   UTF-8u   normali,  u
   index.htmlu8   http://%(bucket)s.s3-website-%(location)s.amazonaws.com/i    id   i   c         C@  s+   |  j  d  k r$ t j |   |  _  n  |  j  S(   N(   t	   _instancet   Nonet   objectt   __new__(   t   selft
   configfilet
   access_keyt
   secret_keyt   access_token(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR*      s    c         C@  s  | ry |  j  |  Wn< t k
 rU d t j k sE d t j k rV |  j   qV n X| rw | rw | |  _ | |  _ n  | r | |  _ t |  _	 n  t
 |  j  d k rkt j d  p t j d  } t j d  p t j d  } t j d  p t j d	  } | r^| st d
   n  t |  |  _ t |  |  _ | rht |  _	 t |  |  _ qhqk|  j   n  |  j r|  j t k rt d  n  |  j r|  j t k rt d   qn  d  S(   Nt   AWS_CREDENTIAL_FILEt   AWS_PROFILEi    t   AWS_ACCESS_KEYt   AWS_ACCESS_KEY_IDt   AWS_SECRET_KEYt   AWS_SECRET_ACCESS_KEYt   AWS_SESSION_TOKENt   AWS_SECURITY_TOKENsR   AWS_ACCESS_KEY environment variable is used but AWS_SECRET_KEY variable is missings   Cannot have server_side_encryption (S3 SSE) and KMS_key set (S3 KMS). KMS encryption will be used. Please set server_side_encryption to FalsesF   KMS encryption requires signature v4. Please set signature_v2 to False(   t   read_config_filet   IOErrort   ost   environt   aws_credential_fileR-   R.   R/   R   t   _access_token_refresht   lent   getenvt
   ValueErrorR   t   role_configt   kms_keyt   server_side_encryptionR   R   t   signature_v2t	   Exception(   R+   R,   R-   R.   R/   t   env_access_keyt   env_secret_keyt   env_access_token(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   __init__   s<    			c      
   C@  s!  yt  j j d  } | rd t t j    } i d d 6d d 6| d 6| d 6} t  j j d	  } | r t |   } | j   } Wd
 QXd | d <| | d <n  d j g  | j   D]( \ } } d | t	 | d t
 f ^ q  }	 d }
 t  j j d  d k r6t  j j d  } | s)|  j } n  d | }
 n  t j d |
 d d  } | j d d |	  | j   } | j   } | j d k rt |  d k rt |  } t |  } | j d k r| d d } n3 | j d k r| d  d } n t d! | j   t   j d" | d#  t   j d$ | d%  t   j d& | d'  t t | d(   } | t j d) d*  |  _ t j j t j j     |  _! qt  nkt j" d d+ d d  } | j d, d-  | j   } | j   } | j d k r	t |  d k r	| j d, d. | j# d/   | j   } | j d k r t | j    } t$ j% |  } t   j d" t | d#   t   j d$ t | d%   t   j d& t | d0   t t | d(   } | t j d) d*  |  _ t t | d1   |  _! qt  n t  Wn
   n Xd
 S(2   sL   
        Get credentials from IAM authentication and STS AssumeRole
        t   AWS_ROLE_ARNs   role-session-%st
   AssumeRolet   Actions
   2011-06-15t   Versiont   RoleArnt   RoleSessionNamet   AWS_WEB_IDENTITY_TOKEN_FILENt   AssumeRoleWithWebIdentityt   WebIdentityTokent   &s   %s=%st   unicode_outputs   sts.amazonaws.comt   AWS_STS_REGIONAL_ENDPOINTSt   regionalt
   AWS_REGIONs   sts.%s.amazonaws.comt   hostt   timeouti   t   POSTs   /?i   i   t   AssumeRoleResponset   AssumeRoleResultt   Credentialst!   AssumeRoleWithWebIdentityResponset   AssumeRoleWithWebIdentityResults.   Unexpected XML message from STS server: <%s />R-   t   AccessKeyIdR.   t   SecretAccessKeyR/   t   SessionTokent
   Expirationt   minutesi   s   169.254.169.254t   GETs+   /latest/meta-data/iam/security-credentials/s-   /latest/meta-data/iam/security-credentials/%ss   utf-8t   Tokent   LastUpdated(&   R:   R;   t   gett   intt   timet   opent   readt   joint   itemsR   R   t   bucket_locationt   httplibt   HTTPSConnectiont   requestt   getresponset   statusR>   R   R   t   tagR9   R"   t   update_optionR   R   t   datetimet	   timedeltat   _access_token_expirationt   nowt   dateutilt   tzt   tzutct   _access_token_last_updatet   HTTPConnectiont   decodet   jsont   loads(   R+   t   role_arnt   role_session_namet   paramst   web_identity_token_filet   ft   web_identity_tokent   kt   vt   encoded_paramst   sts_endpointt   regiont   connt   respt   resp_contentt   treet   result_dictt   credst
   expirationt   files(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRA   )  s    

;	!!		!	
c         C@  s   |  j  r t j j t j j    } |  j rX | |  j k  rX |  j rX |  j | k rX d  Sy |  j   Wq t	 k
 r t
 d  q Xn  d  S(   Ns   Could not refresh role(   R=   Rw   Rz   R{   R|   R}   Ry   R~   RA   RE   R   (   R+   Rz   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   role_refresh}  s    			c         @  se  yt  j j d    t  j j d  } | rN t  j j |  rN t |    n t  j j    sd d  St    t d    t	 j
   d d t |  d d   } | j   } Wd  QXyR y  j t	 j |   Wn1 t k
 rd | }  j t	 j |   n XWn/ t k
 r7} t d   t |  f   n Xt t  j j d	 d
   } t d |  t    f d  } | | d d  } | rt   j d t |   n  | | d d  } | rt   j d t |   n  | | d d  t  }	 |	 rt   j d t |	   n  WnH t k
 r>}
 t d |
 j    n# t k
 r`}
 t d |    n Xd  S(   Ns   ~/.aws/credentialsR0   s   Reading AWS credentials from %st   rt   encodings   UTF-8u
   [default]
s*   Error reading aws_credential_file (%s): %sR1   t   defaults   Using AWS profile '%s'c         @  s   d  } y  j |  |  } Wn t k
 r } | rP t d | j | j    n  | r y/ | } d }   j |  |  } t d |  Wq t k
 r } q Xq n X| r t d | |     n  | S(   NsL   Couldn't find key '%s' for the AWS Profile '%s' in the credentials file '%s'R   s   Legacy configuratin key '%s' used, please use the standardized config format as described here: https://aws.amazon.com/blogs/security/a-new-and-standardized-way-to-manage-credentials-in-the-aws-sdks/sW   Found the configuration option '%s' for the AWS Profile '%s' in the credentials file %s(   R(   Rh   R   R   t   optiont   sectionR   (   t   profilet   keyt
   legacy_keyt   print_warningt   resultt   e(   R<   t   config(    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   get_key  s*    t   aws_access_key_idt   AWSAccessKeyIdR-   t   aws_secret_access_keyt   AWSSecretKeyR.   t   aws_session_tokenR/   s&   Errno %d accessing credentials file %ss;   Couldn't find AWS Profile '%s' in the credentials file '%s'(   R:   t   patht
   expanduserR;   Rh   t   isfileR   t   PyConfigParserR   t   ioRk   t   getattrRl   t   readfpt   StringIOR   R   R@   t   strR   R"   Rv   R(   R   R9   R   t   errnoR   (   R+   t   credential_file_from_envt   fpt   config_stringt   excR   R   t   profile_access_keyt   profile_secret_keyt   profile_access_tokenR   (    (   R<   R   sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR<     s^    	
 					
c         C@  s   g  } xr t  |   D]d } t t t |   } | j d  s | t d  t d  t t  f k rj q n  | j |  q W| S(   Nt   _u   stringi*   (   t   dirR   R   R"   t
   startswithR   t   append(   R+   t   retvalR   t   option_type(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   option_list  s    		c         C@  s  t  |  } xN |  j   D]@ } | j |  } | d  k	 rI | j   } n  |  j | |  q W| j d  r |  j d | j d   n  | j d  r xT | j d  j d  D]7 } | j d d  \ } } | j   |  j | j   <q Wn  |  j j	 |  d  S(   Nt
   acl_publict   add_headerst   ,t   :i   (
   R   R   Rh   R(   t   stripRv   t   splitt   extra_headerst   _parsed_filesR   (   R+   R,   t   cpR   t   _optionR   R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR8     s     c         C@  s   t  |  j d |   d  S(   Nu   default(   t   ConfigDumpert   dump(   R+   t   stream(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   dump_config  s    c         C@  s  | d  k r d  St |  j d  rB |  j | t j | d   S| d k r y t |  } Wqt k
 r y6 y t j	 | } Wn t
 k
 r t j | } n XWq t k
 r t d |   q XqXn| d k r| j d  s | j d  rd } n- | j d	  s | j d
  r)d } n d } y* | rLt | d   | >pUt |  } Wqt k
 rt d | | f   qXn*t t t |   t t  k st t |  d  k rt |  rt |  rt } qt |  rt } qt d | | f   n t t t |   t d  k riy t |  } Wqt k
 ret d | | f   qXnG | d k r| j d  r| d } q| j d  r| d } qn  t t | |  d  S(   Nt   $i   t	   verbositys)   Config: verbosity level '%s' is not validt	   limitrateR   t   Ki
   t   mt   Mi   i    isF   Config: value of option %s must have suffix m, k, or nothing, not '%s's8   Config: value of option '%s' must be Yes or No, not '%s'i*   s9   Config: value of option '%s' must be an integer, not '%s't	   host_baset   host_buckett   cloudfront_hosts   http://i   s   https://i   (   R   R   R   (   R(   R   R   Rv   R:   R?   Ri   R@   t   loggingt   _levelNamest   AttributeErrort   _nameToLevelt   KeyErrort   endswithRE   R   R   R"   R   R!   R   R    R   t   setattr(   R+   R   R   t   shift(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRv     sV    		*!!		!Ni   i   i   (   t   __name__t
   __module__R(   R'   R   t   _docR-   R.   R/   R   R=   Ry   R~   R   R   RB   t   simpledb_hostR   R   t   WARNINGR   t   syst   stdoutt   isattyt   progress_meterR	   t
   ProgressCRt   progress_classt
   send_chunkt
   recv_chunkR   t   list_md5t   long_listingt   human_readable_sizesR
   R   t   forceRC   t   enablet   get_continuet   put_continuet	   upload_idt   skip_existingt	   recursivet   restore_dayst   restore_priorityR   t
   acl_grantst   acl_revokest
   proxy_hostt
   proxy_portt   encryptt   dry_runt   add_encoding_extst   preserve_attrst   preserve_attrs_listR$   t   delete_aftert   delete_after_fetcht
   max_deletet   limitt   delay_updatest   gpg_passphraset   gpg_commandt   gpg_encryptt   gpg_decryptt	   use_httpst   ca_certs_filet   ssl_client_key_filet   ssl_client_cert_filet   check_ssl_certificatet   check_ssl_hostnameRo   t   default_mime_typet   guess_mime_typet   use_mime_magict	   mime_typet   enable_multipartt   multipart_chunk_size_mbt   multipart_copy_chunk_size_mbt   multipart_max_chunkst   sync_checkst   excludet   includet   debug_excludet   debug_includet   localet   getpreferredencodingR   t   urlencoding_modet   log_target_prefixt   reduced_redundancyt   storage_classt   follow_symlinkst   socket_timeoutt   invalidate_on_cft   invalidate_default_index_on_cft#   invalidate_default_index_root_on_cft   website_indext   website_errort   website_endpointt   additional_destinationst
   files_fromt
   cache_fileR   t   remove_headerst   expiry_dayst   expiry_datet   expiry_prefixRD   R   t   requester_payst   stop_on_errort   content_dispositiont   content_typet   statst   use_http_expectt   signurl_use_httpst   throttle_maxt   public_url_use_httpst   connection_poolingt   connection_max_ageR*   RI   RA   R   R<   R   R8   R   Rv   (    (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR"   l   s   			
+	T		a			R   c           B@  s>   e  Z g  d   Z g  d  Z d   Z d   Z d d  Z RS(   c         C@  s   i  |  _  |  j | |  d  S(   N(   t   cfgt
   parse_file(   R+   t   filet   sections(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRI   R  s    	c      
   C@  s  t  d |  t |  t g   k r2 | g } n  t } t j d  } t j d  } t j d  } t j d  } t j d  } t j | d d |  j d d	  o}	 xe|	 D]]}
 | j |
  s | j |
  r q n  | j |
  } | r$| j	   d
 } | | k pt
 |  d
 k } q n  | j |
  } | r| r| j   } | j | d  rv| d d d !| d <n  |  j | d | d  | d d k rd | d d  t
 | d  d | d d f } n
 | d } t  d | d | f  q n  t d | |
 f  q WWd  QXd  S(   Ns   ConfigParser: Reading file '%s's   ^\s*#.*s   ^\s*$s   ^\[([^\]]+)\]s$   ^\s*(?P<key>\w+)\s*=\s*(?P<value>.*)s   ^"(.*)"\s*$R   R   s   UTF-8i    R   i   iR   R-   R.   R  s   %s...%d_chars...%si   i   s   ConfigParser: %s->%ss!   Ignoring invalid line in '%s': %s(   R-   R.   R  (   R   R   R   t   ret   compileR   Rk   Rh   t   matcht   groupsR>   t	   groupdictt   __setitem__R   (   R+   R@  RA  t   in_our_sectiont	   r_commentt   r_emptyt	   r_sectiont   r_datat   r_quotesR   t   linet
   is_sectionR   t   is_datat   datat   print_value(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR?  V  s<    '4
c         C@  s   |  j  | S(   N(   R>  (   R+   t   name(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   __getitem__w  s    c         C@  s   | |  j  | <d  S(   N(   R>  (   R+   RS  R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRG  z  s    c         C@  s   | |  j  k r |  j  | S| S(   N(   R>  (   R+   RS  R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRh   }  s    N(   R   R   RI   R?  RT  RG  R(   Rh   (    (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR   Q  s
   !		R   c           B@  s   e  Z d    Z d   Z RS(   c         C@  s   | |  _  d  S(   N(   R   (   R+   R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyRI     s    c         C@  s   |  j  j d |  x | j   D] } t | |  } | d k r t | t  r y6 y t j | } Wn t k
 r t j	 | } n XWq t
 k
 r q Xq n  |  j  j d | | f  q! Wd  S(   Nu   [%s]
R   u   %s = %s
(   R   t   writeR   R   t
   isinstanceRi   R   R   R   t   _levelToNameR   (   R+   R   R   R   R   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR     s    
(   R   R   RI   R   (    (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyR     s   	(3   t
   __future__R    R   Rw   R  RB  R:   R   R   R   Rj   R   R   t	   ExitCodesR   t   dateutil.parserR{   t   dateutil.tzt   ImportErrort   stderrRU  t   flusht   exitRp   t   http.clientt   clientt   configparserR   R   R   R   R   R   t    R	   R
   t	   BaseUtilsR   R   R   R   R   R   t	   NameErrorR   R   R    R!   R)   R"   R   (    (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Config.pyt   <module>	   sR   	
,,(
			 1