U ä€C^ê2ã@snddlZddlmZddlZddlmZmZddlmZddl Z e   d¡Z Gdd„deƒZ Gd d „d eƒZ dS) éN)Úquoteé)ÚAbstractFileSystemÚAbstractBufferedFile©Úinfer_storage_optionsÚwebhdfscsôeZdZdZdZdZd8‡fdd„ Zd d „Zd9d d„Zd:dd„Z e dd„ƒZ e dd„ƒZ e dd„ƒZdd„Zd;dd„Zdd„Zdd„Zd d!„Zdd0d1„Zd2d3„Zd?d4d5„Zd6d7„Z‡ZS)@ÚWebHDFSaF Interface to HDFS over HTTP Three auth mechanisms are supported: insecure: no auth is done, and the user is assumed to be whoever they say they are (parameter `user`), or a predefined value such as "dr.who" if not given spnego: when kerberos authentication is enabled, auth is negotiated by requests_kerberos https://github.com/requests/requests-kerberos . This establishes a session based on existing kinit login and/or specified principal/password; paraneters are passed with ``kerb_kwargs`` token: uses an existing Hadoop delegation token from another secured service. Indeed, this client can also generate such tokens when not insecure. Note that tokens expire, but can be renewed (by a previously specified user) and may allow for proxying. z/tmp)rZwebHDFSé–ÃFNc  s¶|jr dStƒjf| Ždj||d�|_||_|p4i|_i|_|pDi|_|dk rr|dk s`|dk rht dƒ‚||jd<|dk r„||jd<|dk r–||jd<|rª|dk rªt dƒ‚|  ¡dS) as Parameters ---------- host: str Name-node address port: int Port for webHDFS kerberos: bool Whether to authenticate with kerberos for this connection token: str or None If given, use this token on every call to authenticate. A user and user-proxy may be encoded in the token and should not be also given user: str or None If given, assert the user name to connect with proxy_to: str or None If given, the user has the authority to proxy, and this value is the user in who's name actions are taken kerb_kwargs: dict Any extra arguments for HTTPKerberosAuth, see https://github.com/requests/requests-kerberos/blob/master/requests_kerberos/kerberos_.py data_proxy: dict, callable or None If given, map data-node addresses. This can be necessary if the HDFS cluster is behind a proxy, running on Docker or otherwise has a mismatch between the host-names given by the name-node and the address by which to refer to them from the client. If a dict, maps host names `host->data_proxy[host]`; if a callable, full URLs are passed, and function must conform to `url->data_proxy(url)`. kwargs Nzhttp://{host}:{port}/webhdfs/v1)ÚhostÚportz_If passing a delegation token, must not set user or proxy_to, as these are encoded in the tokenZ delegationz user.nameZdoaszJIf using Kerberos auth, do not specify the user, this is handled by kinit.) Ú_cachedÚsuperÚ__init__ÚformatÚurlÚkerbÚ kerb_kwargsÚparsÚproxyÚ ValueErrorÚ_connect) Úselfr r ZkerberosÚtokenÚuserZproxy_torZ data_proxyÚkwargs©Ú __class__©úB/tmp/pip-install-6_kvzl1k/fsspec/fsspec/implementations/webhdfs.pyr$s.+  ÿ    ÿzWebHDFS.__init__cCs0t ¡|_|jr,ddlm}|f|jŽ|j_dS)Nr)ÚHTTPKerberosAuth)ÚrequestsÚSessionÚsessionrZrequests_kerberosr rÚauth)rr rrrrjs  zWebHDFS._connectÚgetTc Ksœ|jt|p dƒ}| ¡}| |j¡| ¡|d<t |||¡|jj | ¡||||d�} | j dkrlt |ƒ‚| j dkr‚t |p~dƒ‚| j dkr�t ‚|   ¡| S)NÚÚop)ÚmethodrÚparamsÚdataÚallow_redirectsi”i“i‘)rrÚcopyÚupdaterÚupperÚloggerÚdebugr#ÚrequestÚ status_codeÚFileNotFoundErrorÚPermissionErrorÚraise_for_status) rr'r(Úpathr*ÚredirectrrÚargsÚoutrrrÚ_callqs(  û    z WebHDFS._callÚrbc Ks$|p|j}t|||||j|||d�S)a^ Parameters ---------- path: str File location mode: str 'rb', 'wb', etc. block_size: int Client buffer size for read-ahead or write buffer autocommit: bool If False, writes to temporary file that only gets put in final location upon commit replication: int Number of copies of file on the cluster, write mode only permissions: str or int posix permissions, write mode only kwargs Returns ------- WebHDFile instance )ÚmodeÚ block_sizeÚtempdirÚ autocommitÚ replicationÚ permissions)Ú blocksizeÚ WebHDFiler>)rr6r<r=r?r@rArrrrÚ_open‡s! øz WebHDFS._opencCs |d ¡|d<|d|d<|S)NÚtypeÚlengthÚsize)Úlower)ÚinforrrÚ _process_info´s zWebHDFS._process_infocCs t|ƒdS)Nr6r)Úclsr6rrrÚ_strip_protocolºszWebHDFS._strip_protocolcCs:t|ƒ}| dd¡| dd¡d|kr6| d¡|d<|S)Nr6ÚprotocolÚusernamer)rÚpop)Zurlpathr9rrrÚ_get_kwargs_from_urls¾s   zWebHDFS._get_kwargs_from_urlscCs,|jd|d�}| ¡d}||d<| |¡S)NZ GETFILESTATUS©r6Ú FileStatusÚname)r:ÚjsonrJ)rr6r9rIrrrrIÇs z WebHDFS.infocCsv|jd|d�}| ¡dd}|D](}| |¡| d¡d|d|d<q"|r`t|dd „d �Std d „|DƒƒSdS) NZ LISTSTATUSrQZ FileStatusesrRú/Z pathSuffixrScSs|dS)NrSr)ÚirrrÚÔózWebHDFS.ls..)Úkeycss|]}|dVqdS)rSNr)Ú.0rIrrrÚ ÖszWebHDFS.ls..)r:rTrJÚrstripÚsorted)rr6Zdetailr9ZinfosrIrrrÚlsÍs z WebHDFS.lscCs|jd|d�}| ¡dS)z8Total numbers of files, directories and bytes under pathZGETCONTENTSUMMARYrQZContentSummary©r:rT)rr6r9rrrÚcontent_summaryØszWebHDFS.content_summarycCs@|jd|dd�}| |jd¡}|j |¡}| ¡| ¡dS)z/Checksum info of file, giving method and resultZGETFILECHECKSUMF)r6r7ÚLocationZ FileChecksum)r:Ú _apply_proxyÚheadersr#r%r5rT)rr6r9ÚlocationÚout2rrrÚukeyÝs  z WebHDFS.ukeycCs| d¡}| ¡dS)zGet user's home directoryZGETHOMEDIRECTORYÚPathr_)rr9rrrÚhome_directoryås zWebHDFS.home_directorycCsB|r|jd|d�}n | d¡}| ¡d}|dkr:tdƒ‚|dS)zÒRetrieve token which can give the same authority to other uses Parameters ---------- renewer: str or None User who may use this token; if None, will be current user ZGETDELEGATIONTOKEN)ÚrenewerÚTokenNz1No token available for this user/security contextZ urlString)r:rTr)rrir9ÚtrrrÚget_delegation_tokenês  zWebHDFS.get_delegation_tokencCs|jdd|d�}| ¡dS)z/Make token live longer. Returns new expiry timeZRENEWDELEGATIONTOKENÚput©r(rÚlongr_)rrr9rrrÚrenew_delegation_tokenûszWebHDFS.renew_delegation_tokencCs|jdd|d�dS)z Stop the token from being usefulZCANCELDELEGATIONTOKENrmrnN©r:)rrrrrÚcancel_delegation_tokenszWebHDFS.cancel_delegation_tokencCs|jdd||d�dS)aSet the permission at path Parameters ---------- path: str location to set (file or directory) mod: str or int posix epresentation or permission, give as oct string, e.g, '777' or 0o777 Z SETPERMISSIONrm)r(r6Z permissionNrq)rr6ÚmodrrrÚchmods z WebHDFS.chmodcCs>i}|dk r||d<|dk r$||d<|jdd|dœ|—ŽdS)zChange owning user and/or groupNÚownerÚgroupÚSETOWNERrm©r(r6)rwrq)rr6rurvrrrrÚchowns z WebHDFS.chowncCs|jd|d|d�dS)a9 Set file replication factor Parameters ---------- path: str File location (not for directories) replication: int Number of copies of file on the cluster. Should be smaller than number of data nodes; normally 3 on most systems. ZSETREPLICATIONrm)r6r(r@Nrq)rr6r@rrrÚset_replications zWebHDFS.set_replicationcKs|jdd|d�dS)NZMKDIRSrmrxrq)rr6rrrrÚmkdir(sz WebHDFS.mkdircCs(|dkr| |¡rt|ƒ‚| |¡dS)NF)ÚexistsÚFileExistsErrorr{)rr6Úexist_okrrrÚmakedirs+szWebHDFS.makedirscKs|jdd||d�dS)NZRENAMErm)r(r6Z destinationrq)rÚpath1Úpath2rrrrÚmv0sz WebHDFS.mvcKs|jdd||rdndd�dS)NÚDELETEÚdeleteÚtrueÚfalse)r(r6Ú recursiverq)rr6r‡rrrrÚrm3s  üz WebHDFS.rmcCsH|jrt|jƒr| |¡}n(|jrD|j ¡D]\}}| ||d¡}q,|S)Né)rÚcallableÚitemsÚreplace)rrdÚkÚvrrrrb;s  zWebHDFS._apply_proxy)r FNNNNN)r%NNT)r;NTNN)F)N)NN)F)F) Ú__name__Ú __module__Ú __qualname__Ú__doc__r>rMrrr:rDÚ staticmethodrJÚ classmethodrLrPrIr^r`rfrhrlrprrrtryrzr{rr‚rˆrbÚ __classcell__rrrrr sR÷F ù -        r csJeZdZdZ‡fdd„Zddd„Zdd„Zd d „Zd d „Zd d„Z ‡Z S)rCz"A file living in HDFS over webHDFSc sžtƒj||f|Ž| ¡}| dd¡dkr6| dd¡| dd¡dkrR| dd¡| dd¡|_| d¡}| dd¡dkrš|j|_d |t t   ¡ƒg¡|_dS)NrAr@iÿr>r?FrU) rrr,r%rOrAr6ÚtargetÚjoinÚstrÚuuidZuuid4)rÚfsr6rr>rrrrHs   zWebHDFile.__init__FcCs&|jjj|j|j ¡d�}| ¡dS)zÕ Write one part of a multi-block file upload Parameters ========== final: bool This is the last block, so should complete file, if self.autocommit is True. )r*T)ršr#ÚpostrdÚbufferÚgetvaluer5)rÚfinalr9rrrÚ _upload_chunkUs zWebHDFile._upload_chunkcCsœd|jkrd\}}n$d\}}|j |j¡r8|j |j¡|jj|||jfddi|j—Ž}|j |jd¡}d|jkrŠ|jj   |¡}|  ¡|  dd ¡|_ d S) z Create remote file/upload Úa)ÚAPPENDÚPOST)ÚCREATEÚPUTr7FraÚwr£r¡N)r<ršr|r6rˆr:rrbrcr#rmr5rŒrd)rr'r(r9rdrerrrÚ_initiate_uploadbs    zWebHDFile._initiate_uploadcCsJ|jjd|j|||dd�}| ¡|jd}|jj |j |¡¡}|jS)NZOPENF)r6ÚoffsetrFr7ra) ršr:r6r5rcr#r%rbÚcontent)rÚstartÚendr9rdrerrrÚ _fetch_rangessÿ zWebHDFile._fetch_rangecCs|j |j|j¡dS©N)ršr‚r6r–©rrrrÚcommit|szWebHDFile.commitcCs|j |j¡dSr¬)ršrˆr6r­rrrÚdiscardszWebHDFile.discard)F) r�r�r‘r’rrŸr¦r«r®r¯r•rrrrrCEs  rC)r!Ú urllib.parserr™ÚspecrrÚutilsrÚloggingÚ getLoggerr/r rCrrrrÚs   :