ó õùPc@sÑdZddlmZddlZddlZddlZddlZddlZddlZddl Tddl m Z ddl m Z ddlmZd„Zd efd „ƒYZd efd „ƒYZdS( s Client-mode SFTP support. iÿÿÿÿ(thexlifyN(t*(tSFTPAttributes(t SSHException(tSFTPFilecCsOy|jdƒSWn7tk rJy|jdƒSWqKtk rF|SXnXdS(sÑ decode a string as ascii or utf8 if possible (as required by the sftp protocol). if neither works, just return a byte string because the server probably doesn't know the filename's encoding. tasciisutf-8N(tencodet UnicodeErrortdecode(ts((s/..\python\site-packages\paramiko\sftp_client.pyt _to_unicode%s  t SFTPClientcBsXeZdZd„Zd„ZeeƒZd„Zd„Zd„Zdd„Z dd„Z d d d „Z e Z d „Z e Zd „Zdd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd$ed„Zd$d„Z d„Z!d„Z"d$d „Z#d!„Z$d"„Z%d#„Z&RS(%s SFTP client object. C{SFTPClient} is used to open an sftp session across an open ssh L{Transport} and do remote file operations. cCsètj|ƒ||_t|_d|_tjƒ|_d|_ t j ƒ|_ t|ƒtkrž|jjƒ}tj|jƒdƒ|_|jƒ|_ny|jƒ}Wntk rÏ}tdƒ‚nX|jtd|ƒdS(s± Create an SFTP client from an existing L{Channel}. The channel should already have requested the C{"sftp"} subsystem. An alternate way to create an SFTP client context is by using L{from_transport}. @param sock: an open L{Channel} using the C{"sftp"} subsystem @type sock: L{Channel} @raise SSHException: if there's an exception while negotiating sftp is.sftpsEOF during negotiations*Opened sftp connection (server version %d)N(tBaseSFTPt__init__tsocktFalset ultra_debugtrequest_numbert threadingtLockt_locktNonet_cwdtweakreftWeakValueDictionaryt _expectingttypetChannelt get_transporttutilt get_loggertget_log_channeltloggert get_hexdumpt _send_versiontEOFErrorRt_logtINFO(tselfRt transporttserver_versiontx((s/..\python\site-packages\paramiko\sftp_client.pyR :s      cCs3|jƒ}|dkrdS|jdƒ||ƒS(sB Create an SFTP client channel from an open L{Transport}. @param t: an open L{Transport} which is already authenticated @type t: L{Transport} @return: a new L{SFTPClient} object, referring to an sftp session (channel) across the transport @rtype: L{SFTPClient} tsftpN(t open_sessionRtinvoke_subsystem(tclstttchan((s/..\python\site-packages\paramiko\sftp_client.pytfrom_transport\s   cGs“t|tƒrYx}|D]<}tt|ƒj|d||jjƒgt|ƒŒqWn6tt|ƒj|d||jjƒgt|ƒŒdS(Ns [chan %s] (t isinstancetlisttsuperR R$Rtget_name(R&tleveltmsgtargstm((s/..\python\site-packages\paramiko\sftp_client.pyR$ms =cCs!|jtdƒ|jjƒdS(sY Close the SFTP session and its underlying channel. @since: 1.4 ssftp session closed.N(R$R%Rtclose(R&((s/..\python\site-packages\paramiko\sftp_client.pyR9tscCs|jS(sú Return the underlying L{Channel} object for this SFTP session. This might be useful for doing things like setting a timeout on the channel. @return: the SSH channel @rtype: L{Channel} @since: 1.7.1 (R(R&((s/..\python\site-packages\paramiko\sftp_client.pyt get_channel}s t.cCs#g|j|ƒD]}|j^qS(s Return a list containing the names of the entries in the given C{path}. The list is in arbitrary order. It does not include the special entries C{'.'} and C{'..'} even if they are present in the folder. This method is meant to mirror C{os.listdir} as closely as possible. For a list of full L{SFTPAttributes} objects, see L{listdir_attr}. @param path: path to list (defaults to C{'.'}) @type path: str @return: list of filenames @rtype: list of str (t listdir_attrtfilename(R&tpathtf((s/..\python\site-packages\paramiko\sftp_client.pytlistdir‰s c Cs[|j|ƒ}|jtd|ƒ|jt|ƒ\}}|tkrVtdƒ‚n|jƒ}g}xÜtrFy|jt |ƒ\}}Wnt k r£}PnX|t kr¿tdƒ‚n|j ƒ}xut |ƒD]g}t|jƒƒ} t|jƒƒ} tj|| | ƒ} | dkrØ| dkrØ|j| ƒqØqØWqkW|jt|ƒ|S(sÝ Return a list containing L{SFTPAttributes} objects corresponding to files in the given C{path}. The list is in arbitrary order. It does not include the special entries C{'.'} and C{'..'} even if they are present in the folder. The returned L{SFTPAttributes} objects will each have an additional field: C{longname}, which may contain a formatted string of the file's attributes, in unix format. The content of this string will probably depend on the SFTP server implementation. @param path: path to list (defaults to C{'.'}) @type path: str @return: list of attributes @rtype: list of L{SFTPAttributes} @since: 1.2 s listdir(%r)sExpected handlesExpected name responseR;s..(t _adjust_cwdR$tDEBUGt_requestt CMD_OPENDIRt CMD_HANDLEt SFTPErrort get_stringtTruet CMD_READDIRR#tCMD_NAMEtget_inttrangeR Rt _from_msgtappendt CMD_CLOSE( R&R>R.R6thandletfilelisttetcounttiR=tlongnametattr((s/..\python\site-packages\paramiko\sftp_client.pyR<˜s.     triÿÿÿÿc Cs`|j|ƒ}|jtd||fƒd}d|ksGd|krT|tO}nd|ksxd|ksxd|kr…|tO}nd|kr¢|ttBO}nd|kr¿|ttBO}nd|krÜ|ttBO}nt ƒ}|j t |||ƒ\}}|t krt dƒ‚n|jƒ}|jtd ||t|ƒfƒt||||ƒS( sO Open a file on the remote server. The arguments are the same as for python's built-in C{file} (aka C{open}). A file-like object is returned, which closely mimics the behavior of a normal python file object. The mode indicates how the file is to be opened: C{'r'} for reading, C{'w'} for writing (truncating an existing file), C{'a'} for appending, C{'r+'} for reading/writing, C{'w+'} for reading/writing (truncating an existing file), C{'a+'} for reading/appending. The python C{'b'} flag is ignored, since SSH treats all files as binary. The C{'U'} flag is supported in a compatible way. Since 1.5.2, an C{'x'} flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to python's file flags, but is commonly known as the C{O_EXCL} flag in posix. The file will be buffered in standard python style by default, but can be altered with the C{bufsize} parameter. C{0} turns off buffering, C{1} uses line buffering, and any number greater than 1 (C{>1}) uses that specific buffer size. @param filename: name of the file to open @type filename: str @param mode: mode (python-style) to open in @type mode: str @param bufsize: desired buffering (-1 = default buffer size) @type bufsize: int @return: a file object representing the open file @rtype: SFTPFile @raise IOError: if the file could not be opened. s open(%r, %r)iRWt+twtaR)sExpected handlesopen(%r, %r) -> %s(RAR$RBtSFTP_FLAG_READtSFTP_FLAG_WRITEtSFTP_FLAG_CREATEtSFTP_FLAG_TRUNCtSFTP_FLAG_APPENDtSFTP_FLAG_EXCLRRCtCMD_OPENRERFRGRR( R&R=tmodetbufsizetimodet attrblockR.R6RP((s/..\python\site-packages\paramiko\sftp_client.pytopenÄs(# $       #cCs7|j|ƒ}|jtd|ƒ|jt|ƒdS(s- Remove the file at the given path. This only works on files; for removing folders (directories), use L{rmdir}. @param path: path (absolute or relative) of the file to remove @type path: str @raise IOError: if the path refers to a folder (directory) s remove(%r)N(RAR$RBRCt CMD_REMOVE(R&R>((s/..\python\site-packages\paramiko\sftp_client.pytremoveÿs cCsO|j|ƒ}|j|ƒ}|jtd||fƒ|jt||ƒdS(sP Rename a file or folder from C{oldpath} to C{newpath}. @param oldpath: existing name of the file or folder @type oldpath: str @param newpath: new name for the file or folder @type newpath: str @raise IOError: if C{newpath} is a folder, or something else goes wrong srename(%r, %r)N(RAR$RBRCt CMD_RENAME(R&toldpathtnewpath((s/..\python\site-packages\paramiko\sftp_client.pytrenames iÿcCsR|j|ƒ}|jtd||fƒtƒ}||_|jt||ƒdS(s™ Create a folder (directory) named C{path} with numeric mode C{mode}. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. @param path: name of the folder to create @type path: str @param mode: permissions (posix-style) for the newly-created folder @type mode: int s mkdir(%r, %r)N(RAR$RBRtst_modeRCt CMD_MKDIR(R&R>RbRV((s/..\python\site-packages\paramiko\sftp_client.pytmkdir s   cCs7|j|ƒ}|jtd|ƒ|jt|ƒdS(s} Remove the folder named C{path}. @param path: name of the folder to remove @type path: str s rmdir(%r)N(RAR$RBRCt CMD_RMDIR(R&R>((s/..\python\site-packages\paramiko\sftp_client.pytrmdir1scCsc|j|ƒ}|jtd|ƒ|jt|ƒ\}}|tkrVtdƒ‚ntj|ƒS(s3 Retrieve information about a file on the remote system. The return value is an object whose attributes correspond to the attributes of python's C{stat} structure as returned by C{os.stat}, except that it contains fewer fields. An SFTP server may return as much or as little info as it wants, so the results may vary from server to server. Unlike a python C{stat} object, the result may not be accessed as a tuple. This is mostly due to the author's slack factor. The fields supported are: C{st_mode}, C{st_size}, C{st_uid}, C{st_gid}, C{st_atime}, and C{st_mtime}. @param path: the filename to stat @type path: str @return: an object containing attributes about the given file @rtype: SFTPAttributes sstat(%r)sExpected attributes( RAR$RBRCtCMD_STATt CMD_ATTRSRFRRM(R&R>R.R6((s/..\python\site-packages\paramiko\sftp_client.pytstat<s  cCsc|j|ƒ}|jtd|ƒ|jt|ƒ\}}|tkrVtdƒ‚ntj|ƒS(sd Retrieve information about a file on the remote system, without following symbolic links (shortcuts). This otherwise behaves exactly the same as L{stat}. @param path: the filename to stat @type path: str @return: an object containing attributes about the given file @rtype: SFTPAttributes s lstat(%r)sExpected attributes( RAR$RBRCt CMD_LSTATRsRFRRM(R&R>R.R6((s/..\python\site-packages\paramiko\sftp_client.pytlstatVs  cCsd|j|ƒ}|jtd||fƒt|ƒtkrM|jdƒ}n|jt||ƒdS(sÿ Create a symbolic link (shortcut) of the C{source} path at C{destination}. @param source: path of the original file @type source: str @param dest: path of the newly created symlink @type dest: str ssymlink(%r, %r)sutf-8N(RAR$RBRtunicodeRRCt CMD_SYMLINK(R&tsourcetdest((s/..\python\site-packages\paramiko\sftp_client.pytsymlinkhs cCsR|j|ƒ}|jtd||fƒtƒ}||_|jt||ƒdS(sA Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's C{os.chmod} function. @param path: path of the file to change the permissions of @type path: str @param mode: new permissions @type mode: int s chmod(%r, %r)N(RAR$RBRRmRCt CMD_SETSTAT(R&R>RbRV((s/..\python\site-packages\paramiko\sftp_client.pytchmodxs   cCs_|j|ƒ}|jtd|||fƒtƒ}|||_|_|jt||ƒdS(sØ Change the owner (C{uid}) and group (C{gid}) of a file. As with python's C{os.chown} function, you must pass both arguments, so if you only want to change one, use L{stat} first to retrieve the current owner and group. @param path: path of the file to change the owner and group of @type path: str @param uid: new owner's uid @type uid: int @param gid: new group id @type gid: int schown(%r, %r, %r)N(RAR$RBRtst_uidtst_gidRCR|(R&R>tuidtgidRV((s/..\python\site-packages\paramiko\sftp_client.pytchown‰s  cCs‚|j|ƒ}|dkr6tjƒtjƒf}n|jtd||fƒtƒ}|\|_|_|jt ||ƒdS(sÉ Set the access and modified times of the file specified by C{path}. If C{times} is C{None}, then the file's access and modified times are set to the current time. Otherwise, C{times} must be a 2-tuple of numbers, of the form C{(atime, mtime)}, which is used to set the access and modified times, respectively. This bizarre API is mimicked from python for the sake of consistency -- I apologize. @param path: path of the file to modify @type path: str @param times: C{None} or a tuple of (access time, modified time) in standard internet epoch time (seconds since 01 January 1970 GMT) @type times: tuple(int) s utime(%r, %r)N( RARttimeR$RBRtst_atimetst_mtimeRCR|(R&R>ttimesRV((s/..\python\site-packages\paramiko\sftp_client.pytutimes  cCsR|j|ƒ}|jtd||fƒtƒ}||_|jt||ƒdS(s\ Change the size of the file specified by C{path}. This usually extends or shrinks the size of the file, just like the C{truncate()} method on python file objects. @param path: path of the file to modify @type path: str @param size: the new size of the file @type size: int or long struncate(%r, %r)N(RAR$RBRtst_sizeRCR|(R&R>tsizeRV((s/..\python\site-packages\paramiko\sftp_client.pyttruncate´s   cCs¡|j|ƒ}|jtd|ƒ|jt|ƒ\}}|tkrVtdƒ‚n|jƒ}|dkrrdS|dkr‘td|ƒ‚nt |j ƒƒS(s5 Return the target of a symbolic link (shortcut). You can use L{symlink} to create these. The result may be either an absolute or relative pathname. @param path: path of the symbolic link file @type path: str @return: target path @rtype: str s readlink(%r)sExpected name responseiisReadlink returned %d resultsN( RAR$RBRCt CMD_READLINKRJRFRKRR RG(R&R>R.R6RS((s/..\python\site-packages\paramiko\sftp_client.pytreadlinkÅs     cCs‘|j|ƒ}|jtd|ƒ|jt|ƒ\}}|tkrVtdƒ‚n|jƒ}|dkrtd|ƒ‚nt|j ƒƒS(sÐ Return the normalized path (on the server) of a given path. This can be used to quickly resolve symbolic links or determine what the server is considering to be the "current folder" (by passing C{'.'} as C{path}). @param path: path to be normalized @type path: str @return: normalized form of the given path @rtype: str @raise IOError: if the path can't be resolved on the server s normalize(%r)sExpected name responseisRealpath returned %d results( RAR$RBRCt CMD_REALPATHRJRFRKR RG(R&R>R.R6RS((s/..\python\site-packages\paramiko\sftp_client.pyt normalizeÜs   cCs~|dkrd|_dStj|j|ƒjƒs_ttjdtj tjƒ|fƒ‚n|j |ƒj dƒ|_dS(sE Change the "current directory" of this SFTP session. Since SFTP doesn't really have the concept of a current working directory, this is emulated by paramiko. Once you use this method to set a working directory, all operations on this SFTPClient object will be relative to that path. You can pass in C{None} to stop using a current working directory. @param path: new current working directory @type path: str @raise IOError: if the requested path doesn't exist on the server @since: 1.4 Ns%s: %ssutf-8( RRRttS_ISDIRRmRFterrnotENOTDIRtoststrerrorRŽR(R&R>((s/..\python\site-packages\paramiko\sftp_client.pytchdirôs   +cCs|jS(s9 Return the "current working directory" for this SFTP session, as emulated by paramiko. If no directory has been set with L{chdir}, this method will return C{None}. @return: the current working directory on the server, or C{None} @rtype: str @since: 1.4 (R(R&((s/..\python\site-packages\paramiko\sftp_client.pytgetcwd s c Cs%tj|ƒj}t|dƒ}z¦|j|dƒ}|jtƒd}zoxhtr¶|jdƒ} t| ƒdkrzPn|j| ƒ|t| ƒ7}|dk rO|||ƒqOqOWWd|j ƒXWd|j ƒX|r|j|ƒ} | j|kr!t d| j|fƒ‚q!n t ƒ} | S(s½ Copy a local file (C{localpath}) to the SFTP server as C{remotepath}. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. The SFTP operations use pipelining for speed. @param localpath: the local file to copy @type localpath: str @param remotepath: the destination path on the SFTP server @type remotepath: str @param callback: optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) @type callback: function(int, int) @param confirm: whether to do a stat() on the file afterwards to confirm the file size (since 1.7.7) @type confirm: bool @return: an object containing attributes about the given file (since 1.7.4) @rtype: SFTPAttributes @since: 1.4 trbtwbii€Nssize mismatch in put! %d != %d( R’RtRˆtfilet set_pipelinedRHtreadtlentwriteRR9tIOErrorR( R&t localpatht remotepathtcallbacktconfirmt file_sizetfltfrR‰tdataR ((s/..\python\site-packages\paramiko\sftp_client.pytputs.      c Cs|j|dƒ}|j|ƒj}|jƒz–t|dƒ}zud}xhtr³|jdƒ}t|ƒdkrwPn|j|ƒ|t|ƒ7}|dk rL|||ƒqLqLWWd|j ƒXWd|j ƒXt j|ƒ} | j|kr t d| j|fƒ‚ndS(s‡ Copy a remote file (C{remotepath}) from the SFTP server to the local host as C{localpath}. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. @param remotepath: the remote file to copy @type remotepath: str @param localpath: the destination path on the local host @type localpath: str @param callback: optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) @type callback: function(int, int) @since: 1.4 R–R—ii€Nssize mismatch in get! %d != %d( R˜RtRˆtprefetchRHRšR›RœRR9R’R( R&RŸRžR R¤R¢R£R‰R¥R ((s/..\python\site-packages\paramiko\sftp_client.pytgetMs(     cGs(|jtdƒ||Œ}|j|ƒS(N(t_async_requestRRt_read_response(R&R.targtnum((s/..\python\site-packages\paramiko\sftp_client.pyRCyscGs/|jjƒz tƒ}|j|jƒx²|D]ª}t|ƒtkrX|j|ƒq0t|ƒtkrz|j|ƒq0t|ƒt krœ|j |ƒq0t|ƒt kr¾|j |ƒq0t d|t|ƒfƒ‚q0W|j}||j|<|j|t |ƒƒ|jd7_Wd|jjƒX|S(Nsunknown type for %r type %ri(RtacquiretMessagetadd_intRRtinttlongt add_int64tstrt add_stringRt_packt ExceptionRt _send_packettrelease(R&tfileobjR.R«R6titemR¬((s/..\python\site-packages\paramiko\sftp_client.pyR©}s(      cCs(x!tr#y|jƒ\}}Wn,tk rM}tdt|ƒfƒ‚nXt|ƒ}|jƒ}||jkr¢|jt d|fƒ|dkrPqqn|j|}|j|=||krë|t krá|j |ƒn||fS|t dƒk r|j||ƒn|dkrPqqWdS(NsServer connection dropped: %ssUnexpected response #%d(NN(RHt _read_packetR#RR³R®RKRR$RBRt CMD_STATUSt_convert_statusRt_async_response(R&twaitforR.R¥RRR6R¬R¹((s/..\python\site-packages\paramiko\sftp_client.pyRª–s.          cCs4x-||jjƒkr/|jƒ|jƒqWdS(N(RtvaluesRªt_check_exception(R&R¹((s/..\python\site-packages\paramiko\sftp_client.pyt_finish_responses³s cCs•|jƒ}|jƒ}|tkr(dS|tkrCt|ƒ‚nN|tkrdttj|ƒ‚n-|t kr…ttj |ƒ‚n t|ƒ‚dS(sU Raises EOFError or IOError on error status; otherwise does nothing. N( RKRGtSFTP_OKtSFTP_EOFR#tSFTP_NO_SUCH_FILERRtENOENTtSFTP_PERMISSION_DENIEDtEACCES(R&R6tcodettext((s/..\python\site-packages\paramiko\sftp_client.pyR½¸s      cCs†t|ƒtkr$|jdƒ}n|jdkr7|St|ƒdkr]|ddkr]|S|jdkrw|j|S|jd|S(sr Return an adjusted path if we're emulating a "current working directory" for the server. sutf-8it/N(RRwRRRR›(R&R>((s/..\python\site-packages\paramiko\sftp_client.pyRAÊs" N('t__name__t __module__t__doc__R R0t classmethodR$R9R:R@R<RfR˜RhtunlinkRlRoRqRtRvR{R}R‚R‡RŠRŒRŽR”R•RRHR¦R¨RCR©RªRÂR½RA(((s/..\python\site-packages\paramiko\sftp_client.pyR 4sF "     ,9              5 ,     tSFTPcBseZdZRS(s6an alias for L{SFTPClient} for backwards compatability(RÌRÍRÎ(((s/..\python\site-packages\paramiko\sftp_client.pyRÑÛs(RÎtbinasciiRRR’RtRRƒRt paramiko.sftptparamiko.sftp_attrRtparamiko.ssh_exceptionRtparamiko.sftp_fileRR R R RÑ(((s/..\python\site-packages\paramiko\sftp_client.pyts         ÿÿ©