
    Je                        d Z ddlZddlZddlmZ ddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZ  ed	      Z ed
      Z ed      Z ed      Z ed      Z ed      Z ed      Z  ejB                  e"      Z# G d de$      Z% G d de      Z&e"dk(  rddl'm(Z(  e(       Z) e&d      Z*e*jV                  jY                  d        e*jZ                  jY                  d        e*j\                  jY                  d        e*j_                          e*j`                  jY                  e)jb                         e)je                          yy)zKite installation functions.    N)
gettempdir)urlretrieve)QThreadSignal)_)to_text_string)check_if_kite_installedWINDOWS_URL	LINUX_URLMAC_URLz	No statuszDownloading script installerzDownloading installer
InstallingzInstallation finishedzInstallation errored	Cancelledc                       e Zd ZdZy)"KiteInstallationCancelledExceptionz Kite installation was cancelled.N)__name__
__module____qualname____doc__     X/usr/lib/python3/dist-packages/spyder/plugins/completion/providers/kite/utils/install.pyr   r   )   s    *r   r   c                        e Zd ZdZ ee      Z eee      Z ee      Z	 fdZ
efdZd Zd Zd Zd Zd Zd	 Zd
 Zd Zd Z xZS )KiteInstallationThreadz2Thread to handle the installation process of kite.c                 
   t         t        |           t        | _        d| _        t        j                  dk(  rt        | _	        d| _
        y t        j                  dk(  rt        | _	        d| _
        y t        | _	        d| _
        y )NFntzkiteSetup.exedarwinzKite.dmgzkite_installer.sh)superr   __init__	NO_STATUSstatus	cancelledosnamer
   _download_url_installer_namesysplatformr   r   )selfparent	__class__s     r   r   zKiteInstallationThread.__init__?   sf    $d4677d?!,D#2D \\X%!(D#-D !*D#6D r   c                 \    || _         | j                  j                  | j                          y)zSet the installation status.N)r    sig_installation_statusemit)r(   r    s     r   _change_installation_statusz2KiteInstallationThread._change_installation_statusM   s!    $$))$++6r   c                     d}|dkD  r||z  }| j                   r
t               | j                  j                  ||       y )Nr   )r!   r   sig_download_progressr-   )r(   block_number	read_size
total_sizeprogresss        r   _progress_reporterz)KiteInstallationThread._progress_reporterR   s>    >#i/H>>466&&++HjAr   c                 2   t               }t        j                  || j                        }t        j
                  j                  d      r| j                  t               n| j                  t               t        | j                  || j                        S )z.Download the installer or installation script.linuxr    )
reporthook)r   ospjoinr%   r&   r'   
startswithr.   DOWNLOADING_SCRIPTDOWNLOADING_INSTALLERr   r$   r5   )r(   temp_dirpaths      r   _download_installer_or_scriptz4KiteInstallationThread._download_installer_or_script[   sw    <xx$"6"67<<""7+,,4F,G,,4I,J..0 	0r   c                 h    | j                  t               |ddg}t        j                  |d       y)zInstallation on Windows.r8   --plugin-launch-with-copilot--channel=spyderTshellNr.   
INSTALLING
subprocess
check_call)r(   installer_pathinstall_commands      r   _execute_windows_installationz4KiteInstallationThread._execute_windows_installationi   s5    ((
(;*  	oT:r   c                     | j                  t               ddd|gg dg dg dg}|D ]  }t        j                  |        y)	zInstallation on MacOS.r8   hdiutilattachz	-nobrowse)cpz-rz/Volumes/Kite/Kite.appz/Applications/)rO   detachz/Volumes/Kite/)openz-az/Applications/Kite.appz--argsrC   rD   NrG   )r(   rK   install_commandscommands       r   _execute_mac_installationz0KiteInstallationThread._execute_mac_installationr   sQ    ((
(;+~>D3!	

 ( 	+G!!'*	+r   c                 .   | j                  t               t        j                  |      }t        j                  ||j
                  t        j                  z         dj                  |      }t        j                  |t        j                  t        j                  d      }| j                  s|j                  j                         }t        |d      }|dk(  r|j!                         nt#        j$                  d|      rf|j'                  d	      d
   j)                         }|j'                  d      d   }|j'                  d      d   }| j*                  j-                  ||       | j                  s| j                  r
t/               |j                  j1                          |j3                         }	|	rt        j4                  |	|      |dg}
g d}| j                  t6               t        j8                  |
       t        j                  |d       y)zInstallation on Linux.r8   z{} --downloadT)stdoutstderrrF   zutf-8 NzDownload: (\d+)/(\d+):/r      z	--install)z~/.local/share/kite/kitedrC   rD   rE   )r.   r>   r"   statchmodst_modeS_IEXECformatrI   PopenPIPESTDOUTr!   rX   readliner   pollrematchsplitstripr0   r-   r   closewaitCalledProcessErrorrH   rJ   )r(   rK   	stat_filedownload_commanddownload_processr4   download_progresscurrent_progressr3   return_coderL   run_commands               r   _execute_linux_installationz2KiteInstallationThread._execute_linux_installation   s   ((0E(FGGN+	
!2!2T\\!AB*11.A%++??$$	 ..'..779H%h8H2~"2"7"7"9"Exx0(;$,NN3$7$;$A$A$C!#4#:#:3#?#B .44S9!<
**//0@*M .. >>466%%'&++-//=MNN);7 
 	((
(;o.D1r   c                 0   t         j                  dk(  r| j                  |       n6t        j                  dk(  r| j                  |       n| j                  |       	 t        j                  |       | j                  t               y# t        $ r Y "w xY w)zExecute the installer.r   r   r8   N)r"   r#   rM   r&   r'   rV   rw   remove	Exceptionr.   FINISHED)r(   rK   s     r   _execute_installer_or_scriptz3KiteInstallationThread._execute_installer_or_script   s{    77d?..~>\\X%**>:,,^<	IIn% 	(((9  		s   B	 		BBc                    t               \  }}|r| j                  t               y	 | j                         \  }}| j	                  |       y# t
        $ r | j                  t               Y yt        $ rq}| j                  t               t        j                  dj                  t        |                   | j                  j                  t        |             Y d}~yd}~ww xY w)zExecute the installation task.r8   zInstallation error: {0}N)r	   r.   r{   rA   r|   r   	CANCELLEDrz   ERROREDloggerdebugrc   r   sig_error_msgr-   )r(   is_kite_installedinstallation_pathr@   http_responseerrors         r   runzKiteInstallationThread.run   s    /F/H,,,,H,= 		?&*&H&H&J#m11$7 	 6 C00	0B 	  ?000@-44^E5JKM""''u(=>>?s   $A C'.C'6A'C""C'c                 F    | j                         r| j                          yy)zInstall Kite.N)rn   startr(   s    r   installzKiteInstallationThread.install   s     99;JJL r   c                 2    | j                   t        t        fv S )z3Return if the installation was cancelled or failed.)r    r   r~   r   s    r   cancelled_or_erroredz+KiteInstallationThread.cancelled_or_errored   s    {{w	222r   )r   r   r   r   r   strr,   intr0   r   r   r   r.   r5   rA   rM   rV   rw   r|   r   r   r   __classcell__)r*   s   @r   r   r   .   sj    <
 %Sk
 #3, 3KM7 2; 7
B0;+"'2R:$3r   r   __main__)qapplicationc                     t        |       S Nprintr8   s    r   <lambda>r      s
    uV} r   c                     t        |       S r   r   )r   s    r   r   r      s
    eEl r   c                 8    t        dj                  | |            S )Nz{0}/{1})r   rc   )r4   totals     r   r   r      s    i&6&6x&G H r   )3r   loggingr"   os.pathr@   r:   ri   r_   rI   r&   tempfiler   urllib.requestr   qtpy.QtCorer   r   spyder.config.baser   spyder.py3compatr   5spyder.plugins.completion.providers.kite.utils.statusr	   r
   r   r   r   r=   r>   rH   r{   r   r~   	getLoggerr   r   rz   r   r   spyder.utils.qthelpersr   appinstall_managerr,   connectr   r0   r   finishedquitexec_r   r   r   <module>r      sL   #  	  	   
  & ( ! +> > kN	56 12 |_
$%
"
#kN				8	$	 	
g3W g3T z3
.C,T2O++33$&!!))"$))11HJ$$SXX.IIK r   