U
    |’“e"$  ã                   @   s  d Z dgZddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZ ddlmZmZmZ dd	lmZ zddlZd
ZW n ek
r¶   dZY nX zddlZW n ek
rà   edƒ Y nX G dd„ deƒZdd„ Ze dkr
eƒ  dS )a  
Buildozer remote
================

.. warning::

    This is an experimental tool and not widely used. It might not fit for you.

Pack and send the source code to a remote SSH server, bundle buildozer with it,
and start the build on the remote.
You need paramiko to make it work.
ÚBuildozerRemoteé    N)Ú	BuildozerÚBuildozerCommandExceptionÚBuildozerExceptionÚ__version__)ÚstdoutÚstdinÚexit)Úselect)ÚjoinÚ
expanduserÚrealpathÚexistsÚsplitext)ÚmakedirsÚwalkÚgetcwd)ÚConfigParserTFz&Paramiko missing: pip install paramikoc                   @   sn   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r   c                 C   sò  |r~|d   d¡sq~| d¡}|dkr.d| _q |dkrD| d¡| _q |dkr^|  ¡  tdƒ q |dkr td t¡ƒ tdƒ q |  	¡  t
|ƒdk rž|  ¡  d S |d }d	 |¡}| j |¡sÐ|  d
 |¡¡ d S | j |dd¡ | _}| j |dd¡| _| j |dd¡ | _}| j |dd¡ | _}| j |dd¡| _|sP|  d |¡¡ d S |sj|  d |¡¡ d S |s„|  d |¡¡ d S d| _|  ¡  |  d¡ |  ¡  |  ¡  z6|  ¡  |  ¡  |  |dd … ¡ | jtƒ dd W 5 |  ¡  X d S )Nr   ú-)z-vú	--verboseé   )z-pz	--profile)z-hz--helpz	--versionzBuildozer (remote) {0}z	remote:{}z.Unknown remote "{}", must be configured first.ÚhostÚ ÚportZ22ÚuserZbuild_directoryÚidentityzMissing "host = " for {}zMissing "user = " for {}z#Missing "build_directory = " for {}ÚremotezPrepare source code to syncé   Úget)Úmode)Ú
startswithÚpopÚ	log_levelZconfig_profileÚusager	   ÚprintÚformatr   Z_merge_config_profileÚlenÚconfigÚhas_sectionÚerrorr   Úremote_hostÚremote_portÚremote_userÚremote_build_dirÚremote_identityZ
targetnameZcheck_build_layoutÚinfoZ_copy_application_sourcesÚ_ssh_connectÚ
_ssh_closeÚ_ensure_buildozerÚ_sync_application_sourcesÚ_do_remote_commandsÚ	_ssh_syncr   )ÚselfÚargsÚargZremote_nameZremote_sectionr*   r,   r-   © r9   ú</tmp/pip-unpacked-wheel-ohd2lbr1/buildozer/scripts/remote.pyÚrun_command%   sŒ    



ÿ  ÿ
  ÿ  ÿ
  ÿ
  ÿ
zBuildozerRemote.run_commandc                 C   s~   |   d | j¡¡ t ¡  | _}| t ¡ ¡ | ¡  i }| j	rNt
| j	ƒ|d< |j| jf| jt| jƒdœ|—Ž | ¡ | _d S )NzConnecting to {}Zkey_filename)Úusernamer   )r/   r%   r*   ÚparamikoZ	SSHClientÚ_ssh_clientZset_missing_host_key_policyZAutoAddPolicyZload_system_host_keysr.   r   Úconnectr,   Úintr+   Z	open_sftpÚ_sftp_client)r6   ÚclientÚkwargsr9   r9   r:   r0   j   s    ÿÿzBuildozerRemote._ssh_connectc                 C   s"   |   d¡ | j ¡  | j ¡  d S )NzClosing remote connection)ÚdebugrA   Úcloser>   )r6   r9   r9   r:   r1   v   s    

zBuildozerRemote._ssh_closec                 C   sR   | j }| d¡}t|| j| jƒ| _|  d | j¡¡ |  | j¡ |  t	d ¡ d S )NÚ.zRemote build directory: {}r   )
rA   Ú	normalizer   r-   Zpackage_full_namerD   r%   Ú
_ssh_mkdirr5   Ú__path__)r6   ÚsÚroot_dirr9   r9   r:   r2   {   s    
ÿz!BuildozerRemote._ensure_buildozerc                 C   sn   |   d¡ |  | j¡ |   d¡ tƒ }| d¡ | ddd¡ t| jdƒ}| j 	|d¡}| 
|¡ | ¡  d S )NzSynchronize application sourceszCreate custom buildozer.speczbuildozer.specZappz
source.dirÚwb)r/   r5   Zapp_dirr   ÚreadÚsetr   r-   rA   ÚopenÚwriterE   )r6   r'   ÚfnÚfdr9   r9   r:   r3   „   s    



z)BuildozerRemote._sync_application_sourcesc                 C   s<   |   d¡ d | j| jdkr dndd |¡¡}|  |¡ d S )NzExecute remote buildozerz–source ~/.profile;cd {0};env PYTHONPATH={0}:$PYTHONPATH python -c "import buildozer, sys;buildozer.Buildozer().run_command(sys.argv[1:])" {1} {2} 2>&1r   r   r   ú )r/   r%   r-   r"   r   Ú_ssh_command)r6   r7   Úcmdr9   r9   r:   r4   “   s    
ÿø
z#BuildozerRemote._do_remote_commandsc                 G   sz   t |Ž }|  d |¡¡ z| j |¡ W nL tk
rt   z| j |¡ W n& tk
rn   |  d |¡¡ ‚ Y nX Y nX d S )NzCreate remote directory {}z$Unable to create remote directory {})r   rD   r%   rA   ÚmkdirÚIOErrorÚstatr)   )r6   r7   Ú	directoryr9   r9   r:   rH   ¡   s    zBuildozerRemote._ssh_mkdirÚputc                 C   s  |   d |¡¡ tt|ƒƒ}| d¡}|dkr†t|dƒ}t| jdƒ}t|ƒsTt|ƒ | j	j
|dD ]}| j	 t||ƒt||ƒ¡ qbd S t|ƒD ]„\}}}	|  | j||d d … ¡ |	D ]Z}
t|
ƒd dkrÌq¶t||
ƒ}t| j||d d … |
ƒ}|   d ||¡¡ | j	 ||¡ q¶qŽd S )	NzSyncing {} directoryú/r   Úbin)Úpathr   )z.pyoz.pycz.swpzSync {} -> {})rD   r%   r   r   Úrfindr   r-   r   r   rA   Úlistdirr   r   rH   r   rZ   )r6   rY   r   Z
base_stripZ	local_dirZ
remote_dirÚ_fileÚrootÚdirsÚfilesrQ   Ú
local_fileZremote_filer9   r9   r:   r5   ®   s,    

ÿ
zBuildozerRemote._ssh_syncc                 C   sJ   |   d |¡¡ | j ¡ }| ¡ }z| |¡ |  |¡ W 5 | ¡  X d S )NzExecute remote command {})rD   r%   r>   Zget_transportZopen_sessionrE   Zexec_commandÚ_interactive_shell)r6   ÚcommandÚ	transportZchannelr9   r9   r:   rT   Å   s    

zBuildozerRemote._ssh_commandc                 C   s   t r|  |¡ n
|  |¡ d S )N)Úhas_termiosÚ_posix_shellÚ_windows_shell)r6   Úchanr9   r9   r:   re   Ï   s    z"BuildozerRemote._interactive_shellc                 C   sÊ   t  t¡}z¨| d¡ t|tgg g ƒ\}}}||kr„z8| d¡}t|ƒdkrXt	dƒ W q°t
 |¡ t
 ¡  W n tjk
r‚   Y nX t|krt d¡}t|ƒdkr¤q°| |¡ qW 5 t  tt j|¡ X d S )Ng        é€   r   z
*** EOF
r   )ÚtermiosÚ	tcgetattrr   Ú	tcsetattrÚ	TCSADRAINÚ
settimeoutr
   Úrecvr&   r$   r   rP   ÚflushÚsocketÚtimeoutrM   Úsendall)r6   rk   ZoldttyÚrÚwÚeÚxr9   r9   r:   ri   Õ   s(    




zBuildozerRemote._posix_shellc                 C   sl   dd l }t d¡ dd„ }|j||fd}| ¡  z t d¡}|sDqP| |¡ q4W n tk
rf   Y nX d S )Nr   zALine-buffered terminal emulation. Press F6 or ^Z to send EOF.

c                 S   s:   |   d¡}|s"t d¡ t ¡  q6t |¡ t ¡  q d S )Né   z
*** EOF ***

)rr   r   rP   rs   )ÚsockÚdatar9   r9   r:   Úwriteallô   s    


z0BuildozerRemote._windows_shell.<locals>.writeall)Útargetr7   r   )	Ú	threadingr   rP   ÚThreadÚstartr   rM   ÚsendÚEOFError)r6   rk   r€   r~   ÚwriterÚdr9   r9   r:   rj   ï   s    


zBuildozerRemote._windows_shellN)rZ   )Ú__name__Ú
__module__Ú__qualname__r;   r0   r1   r2   r3   r4   rH   r5   rT   re   ri   rj   r9   r9   r9   r:   r   $   s   E	

c               
   C   sd   zt ƒ  tjdd … ¡ W nD tk
r.   Y n2 tk
r^ }  ztƒ  d|  ¡ W 5 d } ~ X Y nX d S )Nr   z%s)r   r;   ÚsysÚargvr   r   r   r)   )r)   r9   r9   r:   Úmain  s    rŒ   Ú__main__)!Ú__doc__Ú__all__rt   rŠ   Z	buildozerr   r   r   r   r   r   r	   r
   Úos.pathr   r   r   r   r   Úosr   r   r   Úconfigparserr   rm   rh   ÚImportErrorr=   r$   r   rŒ   r‡   r9   r9   r9   r:   Ú<module>   s.   
 i	
