o UÀ7eã@sLddlZz ddlmZmZWneyddlmZmZYnwddlZddlZddlZddlZddl Z ddl Z zddl m Z WneyOddlm Z YnwddlZGdd„dejƒZedkr¤e j ¡Ze e j ¡¡e d¡Ze ¡ ¡Ze ¡Ze ¡dej j!ej j"dj!efZ#ee$ej j% &¡ƒe#e ¡eƒZ'e' (¡dSdS) éN)ÚEmptyÚQueue)Úurlopenc@sfeZdZdZGdd„dejƒZddd„Zdd„Zdd d „Z d d„Z ddd„Z  ddd„Z dd„Z dS)Ú MirrorTestz@Determines the best mirrors by perfoming ping and download test.c@s"eZdZdZd dd„Zdd„ZdS) zMirrorTest.PingWorkerz¢Use the command line command ping to determine the server's response time. Using multiple threads allows to run several test simultaneously.©ré©rrcCs@||_||_||_||_||_||_t d¡|_t j   |¡dS)Nz^rtt .* = [\.\d]+/([\.\d]+)/.*) ÚbordersÚmodÚparentÚidÚjobsÚresultsÚreÚcompileÚ match_resultÚ threadingÚThreadÚ__init__)Úselfr rr r r r ©rú?/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.pyrs zMirrorTest.PingWorker.__init__cCs0d}|j ¡s’|jj ¡r”z5|j d¡}|j}|j d|¡tj ddddd|gtj tj dd j } |  ¡}|s9nt |j|¡}q2WntyLYdS|j d |¡Ytj ¡tjd 7_|j tjtj|j|j¡|r€|j t|d ƒ||g¡tj ¡|j ¡s–|jj ¡s dSdSdSdS) NFz Pinging %s...Úpingz-qz-c 2z-W 1z-i 0.5T)ÚstdoutÚstderrÚuniversal_newlinesz Skipping %srr)r Úemptyr ÚrunningÚisSetÚgetÚhostnameÚ report_actionÚ subprocessÚPopenÚPIPEÚSTDOUTrÚreadlinerÚfindallrrrÚcompleted_lockÚacquireÚ completedÚreport_progressÚtodor r rÚappendÚfloatÚrelease)rÚresultÚmirrorÚhostÚcommandoÚlinerrrÚrun#sF ýýü   ý &äzMirrorTest.PingWorker.runN)rr)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr5rrrrÚ PingWorkers  r:NcCsltj |¡d|_d|_||_d|_||_g|_dt _ t  ¡t _ t |ƒt _||_|s1t ¡|_dS||_dS)NÚ)rrgr)rrrÚactionÚprogressÚeventÚbestÚ test_fileÚthreadsrr*ÚLockr(Úlenr,ÚmirrorsÚEventr)rrDr@r>rrrrrCs    zMirrorTest.__init__cCs||_|jr |j ¡dSdS©N)r<r>Úset)rÚtextrrrr!TsÿzMirrorTest.report_action©rédrcCsP||d||d|d|d|d||f|_|jr&|j ¡dSdS)zTSubclasses should override this method to receive progress status updatesrrN)r=r>rG)rÚcurrentÚmaxr r rrrr+Ys  þÿzMirrorTest.report_progressc CsÎ|jdddd}t|jƒ}|dkr3| dd|jt d|d¡g¡| dd|jt d|d¡g¡|jdd „|Dƒd tjtjfd }|D] \}}t d |j |fƒqF|sXdS|ddj }t d |ƒ|S)Né)rçà?)ré)rLr r érrcSsg|]}|d‘qS)rPr)Ú.0ÚrrrrÚ jsz,MirrorTest.run_full_test..©rNr)r r zmirror: %s - time: %szand the winner is: %s) Ú run_ping_testrCrDr-ÚrandomÚrandintÚrun_download_testrr,Úprintr )rÚ results_pingÚsizerÚtÚhÚwinnerrrrÚ run_full_testbs$ ""ÿþ  zMirrorTest.run_full_testrc CsŠ|dkr|j}tƒ}|D]}| |¡q g}tdƒD]}t ||||||¡} |j | ¡|  ¡q|jD]} |   ¡q4|  ¡|d|…S)zõPerforms ping tests of the given mirrors and returns the best results (specified by max). Mod and borders could be used to tweak the reported result if the download test is only a part of a whole series of tests.Nér) rDrÚputÚrangerr:rAr-ÚstartÚjoinÚsort) rrDrLr r r ÚmrÚir\rrrrUws       zMirrorTest.run_ping_testc s†‡fdd„}|dkr ˆj}g}|D]'}ˆj ¡sn||ƒ}|dkr)| ||g¡ˆ | |¡dt|ƒd|¡q| ¡|d|…S)zùPerforms download tests of the given mirrors and returns the best results (specified by max). Mod and borders could be used to tweak the reported result if the download test is only a part of a whole series of tests.csZd| ¡dˆjf}ˆ d|¡t ¡}zt|dd d¡t ¡|WSYdS)Nz%s/%srzDownloading %s...rP)Útimeouti)Ú get_repo_urlsr@r!ÚtimerÚread)r1Úurlrc©rrrÚtest_download_speed”s ÿz9MirrorTest.run_download_test..test_download_speedNrrrT)rDrrr-r+ÚindexrCre) rrDrLr r rnrrfÚ download_timerrmrrXŽs     zMirrorTest.run_download_testcCs| ¡|_|j ¡dS)z/Complete test exercise, set self.best when doneN)r_r?rÚclearrmrrrr5¬s zMirrorTest.runrF)rIr)NNrr)r6r7r8r9rrr:rr!r+r_rUrXr5rrrrrs -   ÿ rÚ__main__zdpkg --print-architecturez!dists/%s/%s/binary-%s/Packages.gz))rÚqueuerrÚ ImportErrorrjrr"ÚosÚaptsources.distroÚ aptsourcesÚaptsources.sourceslistÚurllib.requestrÚurllib2rVrrr6ÚdistroÚ get_distroÚ get_sourcesÚ sourceslistÚ SourcesListÚpopenÚpiperkÚstripÚarchrErrGÚsource_templateÚnameÚ componentsr@ÚlistÚ mirror_setÚvaluesÚappr_rrrrÚsL ÿ ÿ    þÿ ÿ ó