o U7e@sLddlZz ddlmZmZWneyddlmZmZYnwddlZddlZddlZddlZddl Z ddl Z zddl m Z WneyOddlm Z YnwddlZGdddejZedkre jZee jedZeZeZedej j!ej j"dj!efZ#ee$ej j%&e#eeZ'e'(dSdS) N)EmptyQueue)urlopenc@sfeZdZdZGdddejZdddZddZdd d Z d dZ dddZ  dddZ ddZ dS) MirrorTestz@Determines the best mirrors by perfoming ping and download test.c@s"eZdZdZd ddZddZdS) zMirrorTest.PingWorkerzUse the command line command ping to determine the server's response time. Using multiple threads allows to run several test simultaneously.rrrcCs@||_||_||_||_||_||_td|_t j |dS)Nz^rtt .* = [\.\d]+/([\.\d]+)/.*) bordersmodparentidjobsresultsrecompile match_result threadingThread__init__)selfr rr r r r r?/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.pyrs zMirrorTest.PingWorker.__init__cCs0d}|js|jjrz5|jd}|j}|jd|tj ddddd|gtj tj dd j } | }|s9nt|j|}q2WntyLYdS|jd |Ytjtjd 7_|jtjtj|j|j|r|jt|d ||gtj|js|jjs dSdSdSdS) NFz Pinging %s...pingz-qz-c 2z-W 1z-i 0.5T)stdoutstderruniversal_newlinesz Skipping %srr)r emptyr runningisSetgethostname report_action subprocessPopenPIPESTDOUTrreadlinerfindallrrrcompleted_lockacquire completedreport_progresstodor r rappendfloatrelease)rresultmirrorhostcommandolinerrrrun#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)rrractionprogresseventbest test_filethreadsrr*Lockr(lenr,mirrorsEventr)rrDr@r>rrrrrCs    zMirrorTest.__init__cCs||_|jr |jdSdSN)r<r>set)rtextrrrr!TszMirrorTest.report_actionrdrcCsP||d||d|d|d|d||f|_|jr&|jdSdS)zTSubclasses should override this method to receive progress status updatesrrN)r=r>rG)rcurrentmaxr r rrrr+Ys  zMirrorTest.report_progressc Cs|jdddd}t|j}|dkr3|dd|jtd|dg|dd|jtd|dg|jdd |Dd tjtjfd }|D] \}}t d |j |fqF|sXdS|ddj }t d ||S)N)r?)r)rLr r rrcSsg|]}|dqS)rPr).0rrrr jsz,MirrorTest.run_full_test..rNr)r r zmirror: %s - time: %szand the winner is: %s) run_ping_testrCrDr-randomrandintrun_download_testrr,printr )r results_pingsizerthwinnerrrr run_full_testbs$ ""  zMirrorTest.run_full_testrc Cs|dkr|j}t}|D]}||q g}tdD]}t||||||} |j| | q|jD]} | q4| |d|S)zPerforms 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.Nr) rDrputrangerr:rAr-startjoinsort) rrDrLr r r mrir\rrrrUws       zMirrorTest.run_ping_testc sfdd}|dkr j}g}|D]'}jsn||}|dkr)|||g||dt|d|q||d|S)zPerforms 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|djf}d|t}zt|dddt|WSYdS)Nz%s/%srzDownloading %s...rP)timeouti) get_repo_urlsr@r!timerread)r1urlrcrrrtest_download_speeds z9MirrorTest.run_download_test..test_download_speedNrrrT)rDrrr-r+indexrCre) rrDrLr r rnrrf download_timerrmrrXs     zMirrorTest.run_download_testcCs||_|jdS)z/Complete test exercise, set self.best when doneN)r_r?rclearrmrrrr5s zMirrorTest.runrF)rIr)NNrr)r6r7r8r9rrr:rr!r+r_rUrXr5rrrrrs -   r__main__zdpkg --print-architecturez!dists/%s/%s/binary-%s/Packages.gz))rqueuerr ImportErrorrjrr"osaptsources.distro aptsourcesaptsources.sourceslisturllib.requestrurllib2rVrrr6distro get_distro get_sources sourceslist SourcesListpopenpiperkstriparchrErrGsource_templatename componentsr@list mirror_setvaluesappr_rrrrsL