o f~d@s~dZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z Gddde Z ddZ d d Zd d d ZdS)z( Implementation for SFTP upload method. N) dputhelperc@s8eZdZdZddZddZddZdd Zd d Zd S) ProcessAsChannelAdapterz' Wrap a process as a paramiko Channel. cCs&t\|_}tj|||dd|_dS)Nr)stdinstdoutbufsize)socket socketpair _ProcessAsChannelAdapter__socket subprocessPopen_ProcessAsChannelAdapter__proc)selfargv subproc_sockr$/usr/share/dput/dput/methods/sftp.py__init__s z ProcessAsChannelAdapter.__init__cCsdS)z' Return a simple name for the adapter. rrrrrrget_name sz ProcessAsChannelAdapter.get_namecCs |j|S)z+ Send a number of bytes to the subprocess. )r send)rdatarrrr$s zProcessAsChannelAdapter.sendc CsXz|j|WStjy+}z|jdtjtjtjtj fvr&WYd}~dSd}~ww)z0 Receive a number of bytes from the subprocess. rN) r recvrerrorargserrnoEPIPE ECONNRESET ECONNABORTEDEBADF)r num_byteserrrr(szProcessAsChannelAdapter.recvcCs"|j|j|jdS)z' Close and wait for process to finish. N)r closer terminatewaitrrrrr#4s  zProcessAsChannelAdapter.closeN) __name__ __module__ __qualname____doc__rrrrr#rrrrrs rcCs"dddddddd|d |d d |d gS) z3 Gather a command line for connection to a server. sshz-oForwardX11 noz-oForwardAgent noz-oPermitLocalCommand noz-oClearAllForwardings yesz -oProtocol 2z&-oNoHostAuthenticationForLocalhost yesz-pz-lz-sz--sftpr)loginfqdnportrrrget_ssh_command_line;sr/c Cs*t|d}|r0zt|j}Wnty%d}|r#tjd|Ynwtj ||tj|d}d|t t t ddf}z/|j|ddd } |d } | sSn|| qKWd n1scwY|||Wnty} z||| d } ~ wwWd d S1swYd S) z Upload a single file. rbz(D: Determining size of file '%s' failed )ptype progressfsizez%s.tmp.%.9f.%d.%driw)rTiN)openosstatst_size ExceptionsysrwriterFileWithProgresstimegetpidrandomrandintfilereadrenameremove) sftp_client local_path remote_pathdebugprogressfileobjr4tmp_pathremote_fileobjrr"rrr copy_fileIsP       "rNc Cszddl}Wnty#}ztjdtdWYd}~nd}~wwd|vr1|dd\}} nd} |r9|dkr>td}| d sGd |}zt t ||| } |j | } Wntyv}ztjd |tdWYd}~nd}~wwzX|D]N} tj| \} }tj||}tjd |tjz t| | |||Wnty}ztjd |tdWYd}~nd}~wwtjdqzW| dS| w)z Upload the files via SFTP. Requires paramiko for SFTP protocol, but uses the ssh binary for setting up the connection so we get proper prompts for authentication, unknown hosts and other stuff. rNz5E: paramiko must be installed to use sftp transport. :22*USER/z%s/z'%s E: Error connecting to remote host. z Uploading %s: z %s E: Error uploading file. zdone. )paramiko.sftp_clientr:r;rr<exitrsplitr7getenvendswithrr/rF SFTPClientpathsplitjoinflushrNr#)r-r,incomingfilesrIcompressrJparamikor"r.channelrFrGpath_to_package base_filenamerHrrruploadksX      rf)r)r)rr7os.pathr@r;rr r>helperrobjectrr/rNrfrrrrs $"