
    Je)                     T   d dl Z d dlZd dlmZ d dlZd dlZd dlmZm	Z	 d dl
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 d dlmZ d dlmZ d dlmZmZ  e j:                  e      Z ed	      Z  ed
      Z! ed      Z" G d de#      Z$ G d de#      Z% G d de      Z& G d de      Z'y)    N)QObjectSignal)ConnectionError	HTTPErrorSSLError)__version__)_is_stable_version
is_pynsistrunning_in_mac_app)is_anaconda)get_spyder_conda_channel)check_versionis_module_installedzeUnable to connect to the Spyder update service.<br><br>Make sure your connection is working properly.z}HTTP error {status_code} when checking for updates.<br><br>Make sure your connection is working properly,and try again later.zSSL certificate verification failed while checking for Spyder updates.<br><br>Please contact your network administrator for assistance.c                       e Zd ZdZy) UpdateDownloadCancelledExceptionz/Download for installer to update was cancelled.N__name__
__module____qualname____doc__     8/usr/lib/python3/dist-packages/spyder/workers/updates.pyr   r   /   s    9r   r   c                       e Zd ZdZy)UpdateDownloadIncompleteErrorz$Error occured while downloading fileNr   r   r   r   r   r   4   s    .r   r   c                   2    e Zd ZdZ e       ZddZd Zd Zy)WorkerUpdatesz
    Worker that checks for releases using either the Anaconda
    default channels or the Github Releases page without
    blocking the Spyder user interface, in case of connection
    issues.
    Nc                     t        j                  |        || _        d | _        d | _        || _        || _        |st        | _        y || _        y )N)	r   __init___parenterrorlatest_releasestartupreleasesr   version)selfparentr$   r&   r%   s        r   r    zWorkerUpdates.__init__B   sE    
" &DL"DLr   c                 |   d| j                   v s| j                  sd| j                  fS t        | j                         r&| j                  D cg c]  }t        |      s| }}n2| j                  D cg c]  }t        |      r|| j                   v r| }}|d   }t	        | j                   |d      |fS c c}w c c}w )a	  
        Check if there is an update available.

        It takes as parameters the current version of Spyder and a list of
        valid cleaned releases in chronological order.
        Example: ['2.3.2', '2.3.3' ...] or with github ['2.3.4', '2.3.3' ...]
        devF<)r&   r%   r#   r
   r   )r'   rr%   r#   s       r   check_update_availablez$WorkerUpdates.check_update_availableO   s     DLL 4..// T\\*#'==Ia4Ea4HIHI#'== Ja03qDLL7H  JH J ""dllNC@  	  JJs   B4B42"B9c                 H   d| _         t        | _        d}d}t               s
t	               rd| _        n;t               r*t               \  }}||y|dk(  r|| _        n|dz   | _        n|| _        	 t        j                  d| j
                          t        j                  | j
                        }|j                          |j                         }t               s
t	               rX| j                  |D cg c]  }|d   j                  d	d
       c}| _        t!        t#        | j                              | _        nat               r=| j
                  |k7  r.| j                  <|d   j                  d      }|r&|d   g| _        n| j                  |d   d   g| _        | j%                         }	|	\  | _         | _        | j>                  r|#|| _         	 | jB                  jE                          yyc c}w # t&        $ r'}
t(        }t        j                  |
d       Y d}
~
bd}
~
wt*        $ r'}
t,        }t        j                  |
d       Y d}
~
d}
~
wt.        $ rA}
t0        j3                  j4                        }t        j                  |
d       Y d}
~
d}
~
wt6        $ rr}
t9        j:                         }|j                  dd      j                  dd      }t=        d      j3                  |      }t        j                  |
d       Y d}
~
Qd}
~
ww xY w# tF        $ r Y yw xY w)zMain method of the worker.FNz!https://pypi.org/pypi/spyder/jsonz7https://api.github.com/repos/spyder-ide/spyder/releasespypiz/channeldata.jsonzChecking for updates from tag_namev packagesspyderr&   infoT
stack_info)status_code
<br> &nbsp;z^It was not possible to check for Spyder updates due to the following error:<br><br><tt>{}</tt>)$update_availabler   r#   r   r   urlr   r   loggerdebugrequestsgetraise_for_statusjsonr%   replacelistreversedr.   r   SSL_ERROR_MSGr   CONNECT_ERROR_MSGr   HTTP_ERROR_MSGformatr9   	Exception	traceback
format_excr	   r$   r"   	sig_readyemitRuntimeError)r'   	error_msgpypi_urlchannelchannel_urlpagedataitemspyder_dataresulterrr"   formatted_errors                r   startzWorkerUpdates.starth   s    %)	6<-/5DH]#;#= G[+"5F"#&)<<DH*	/LL5dhhZ@A<<)D!!#99;D|13==(FJ%>BZ(00b9%DM %)$--)@$ADM488x#7==("&z"2"6"6x"@K")4Y)?(@==(%)&\)%<$=DM002F9?6D!4#6. !6"DJ##% "7K%  	/%ILLL.. 	/)ILLL.. 	/&--$:J:J-KILLL.. 
	/((*E#mmD&9AA#xPO
 f_%  LLL..
	/"   sc   /BG3 4G.B,G3 L .G3 3	L<HL*IL7JL A'LL	L! L!)r3   N)	r   r   r   r   r   rP   r    r.   r^   r   r   r   r   r   9   s      I# 2Ir   r   c                   N    e Zd ZdZ ee      Z	  eee      Z	 d Z	d Z
d Zd Zy)WorkerDownloadInstallerz{
    Worker that donwloads standalone installers for Windows
    and MacOS without blocking the Spyder user interface.
    c                 t    t        j                  |        || _        || _        d | _        d| _        d | _        y )NF)r   r    r!   latest_release_versionr"   	cancelledinstaller_path)r'   r(   rb   s      r   r    z WorkerDownloadInstaller.__init__   s5    &<#
"r   c                     d}|dkD  r||z  }| j                   r
t               | j                  j                  ||       y)z'Calculate download progress and notify.r   N)rc   r   sig_download_progressrQ   )r'   block_number	read_size
total_sizeprogresss        r   _progress_reporterz*WorkerDownloadInstaller._progress_reporter   s>    >#i/H>>244""''*=r   c           	      B   t        j                         }t        d      xs t        d      }t        j                  dk(  rdj                  |rdnd      }ndj                  |rdnd	      }d
| }t        j                  |dd      }t        j                  |d       t        j                  || j                        }t        j                  |d       t        j                  |      D ]B  }|t        | j                  fvst        j                  ||      }t        j                  |       D t        j                  ||      }	|	| _        t        j                  |	      r,t        j!                  |	 d       | j#                  ddd       yt        j%                  d| d|	        t'        j(                  |d      5 }
t+        |	d      5 }d}d}d}d}d|
j,                  v rt/        |
j,                  d         }| j#                  |||       |
j1                  |      D ]9  }|t3        |      z  }|j5                  |       |dz  }| j#                  |||       ; |dk\  r||k  rt7        d| d| d      ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z7Donwload latest Spyder standalone installer executable.numpypandasntzSpyder_64bit_{}.exefulllitezSpyder{}.dmgr3   z-Litez>https://github.com/spyder-ide/spyder/releases/latest/download/r5   updatesT)exist_okz already downloaded   NzDownloading installer from z to )streamwbi    r+   r   zcontent-length)
chunk_sizez$Download incomplete: retrieved only z out of z bytes.)tempfile
gettempdirr   osnamerL   ospjoinmakedirsrb   listdirr   removerd   isfiler@   r6   rk   rA   rB   rC   openheadersintiter_contentlenwriter   )r'   tmpdiris_full_installerr{   r?   dir_pathinstaller_dir_pathfiler   rd   r-   frw   size	size_read	chunk_numchunks                    r   _download_installerz+WorkerDownloadInstaller._download_installer   s   $$&09 ;0: 	77d?(//:K5;=D "((/@gND&"88FHi8
Ht, XXd113
&6JJx( 	"DK)D)DEE(D1		&!	"
 "4d;,::n%KK>**=>?##Aq!,23%tN;KLM\\#d+ 	qnd+ q%
		#qyy0qyy)9:;D''	:tD^^z^B IEU+IGGENNI++Iz4H	I 19T!17>$+XdV7< %	 	 	 	s%   ?JB,J	8J	J	JJc                    d}	 | j                          || _        	 | j$                  j'                  | j                         y# t        $ r. | j                  rt        j                  | j                         Y yt
        $ r'}t        }t        j                  |d       Y d}~d}~wt        $ r'}t        }t        j                  |d       Y d}~d}~wt        $ rr}t        j                         }|j                  dd      j                  dd      }t        d      j!                  |      }t        j                  |d       Y d}~5d}~ww xY w# t(        $ r Y yw xY w)	z2Main method of the WorkerDownloadInstaller worker.NTr7   r:   r;   r<   r=   z\It was not possible to download the installer due to the following error:<br><br><tt>{}</tt>)r   r   rd   rz   r   r   rI   r@   rA   r   rJ   rM   rN   rO   rF   r	   rL   r"   rP   rQ   rR   )r'   rS   r\   r"   r]   s        r   r^   zWorkerDownloadInstaller.start  s)   		/$$&, 
	NN 3 34/ 0 	""		$--. 	/%ILLL.. 	/)ILLL.. 
	/((*E#mmD&9AA#xPO
 f_%  LLL..
	/  		sG   A %E 4E8E B""E.CEA'E		E	EEN)r   r   r   r   r   strrP   r   rf   r    rk   r   r^   r   r   r   r`   r`      s@    
 sI #3,	#>7rr   r`   )(loggingrz   os.pathpathr|   rx   rN   qtpy.QtCorer   r   rB   requests.exceptionsr   r   r   r5   r   spyder.config.baser	   r
   r   r   spyder.config.utilsr   spyder.utils.condar   spyder.utils.programsr   r   	getLoggerr   r@   rJ   rK   rI   rM   r   r   r   r`   r   r   r   <module>r      s     	    (  D D : : + 7 D 
		8	$= 
  H	y 	
	I 	
xG xvDg Dr   