Ticket #14891: sortsites3.diff

File sortsites3.diff, 2.6 KB (added by jmroot (Joshua Root), 16 years ago)
  • src/port1.0/portfetch.tcl

     
    301301    }
    302302}
    303303
     304# sorts fetch_urls in order of ping time
     305proc sortsites {args} {
     306    global fetch_urls fallback_mirror_site
     307
     308    set fallback_mirror_list [mirror_sites $fallback_mirror_site {} {}]
     309
     310    foreach {url_var distfile} $fetch_urls {
     311        global portfetch::$url_var
     312                if {![info exists $url_var]} {
     313                        ui_error [format [msgcat::mc "No defined site for tag: %s, using master_sites"] $url_var]
     314                        set url_var master_sites
     315                        global portfetch::$url_var
     316                }
     317                set urllist [set $url_var]
     318                set hosts {}
     319                set hostregex {[a-zA-Z]+://([a-zA-Z0-9\.\-_]+)}
     320
     321        foreach site $urllist {
     322            regexp $hostregex $site -> host
     323           
     324            if { [info exists seen($host)] } {
     325                continue
     326            }
     327            foreach fallback $fallback_mirror_list {
     328                if {[string match [append fallback *] $site]} {
     329                    # don't bother pinging fallback mirrors
     330                    set seen($host) yes
     331                    # and make them sort to the very end of the list
     332                    set pingtimes($host) 20000
     333                    break
     334                }
     335            }
     336            if { ![info exists seen($host)] } {
     337                set seen($host) yes
     338                lappend hosts $host
     339                ui_debug "Pinging $host..."
     340                set fds($host) [open "|ping -noq -c3 -t3 $host | grep round-trip | cut -d / -f 5"]
     341            }
     342        }
     343       
     344        foreach host $hosts {
     345            set len [gets $fds($host) pingtimes($host)]
     346            if { [catch { close $fds($host) }] || ![string is double -strict $pingtimes($host)] } {
     347                # ping failed, so put it last in the list (but before the fallback mirrors)
     348                set pingtimes($host) 10000
     349            }
     350            ui_debug "$host ping time is $pingtimes($host)"
     351        }
     352       
     353        set pinglist {}
     354        foreach site $urllist {
     355            regexp $hostregex $site -> host
     356            lappend pinglist [ list $site $pingtimes($host) ]
     357        }
     358
     359        set pinglist [ lsort -real -index 1 $pinglist ]
     360
     361        set $url_var {}
     362        foreach pair $pinglist {
     363            lappend $url_var [lindex $pair 0]
     364        }
     365    }
     366}
     367
    304368# Perform the full checksites/checkpatchfiles/checkdistfiles sequence.
    305369# This method is used by distcheck target.
    306370proc checkfiles {args} {
     
    310374        checksites
    311375        checkpatchfiles
    312376        checkdistfiles
     377        sortsites
    313378}
    314379
    315380