From 550ef2eeeb58a3899b612bf5f895b2ae962086a9 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 21 Sep 2010 18:58:34 -0500 Subject: Allow generated mirrorlist to take status info into account By using the mirror score we calculate, we can sort the mirrors in the generated mirrorlist for people. Signed-off-by: Dan McGee --- mirrors/views.py | 26 +++++++++++++++++++++----- templates/mirrors/index.html | 4 +++- templates/mirrors/mirrorlist.txt | 10 +++++----- templates/mirrors/mirrorlist_status.txt | 13 +++++++++++++ 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 templates/mirrors/mirrorlist_status.txt diff --git a/mirrors/views.py b/mirrors/views.py index 17f65cc..71c896d 100644 --- a/mirrors/views.py +++ b/mirrors/views.py @@ -9,10 +9,12 @@ from .models import Mirror, MirrorUrl, MirrorProtocol from .utils import get_mirror_statuses, get_mirror_errors import datetime +from operator import attrgetter class MirrorlistForm(forms.Form): country = forms.MultipleChoiceField(required=False) protocol = forms.MultipleChoiceField(required=False) + use_mirror_status = forms.BooleanField(required=False) def __init__(self, *args, **kwargs): super(MirrorlistForm, self).__init__(*args, **kwargs) @@ -32,13 +34,14 @@ def generate_mirrorlist(request): if form.is_valid(): countries = form.cleaned_data['country'] protocols = form.cleaned_data['protocol'] - return find_mirrors(request, countries, protocols) + use_status = form.cleaned_data['use_mirror_status'] + return find_mirrors(request, countries, protocols, use_status) else: form = MirrorlistForm() return direct_to_template(request, 'mirrors/index.html', {'mirrorlist_form': form}) -def find_mirrors(request, countries=None, protocols=None): +def find_mirrors(request, countries=None, protocols=None, use_status=False): if not protocols: protocols = MirrorProtocol.objects.exclude( protocol__iexact='rsync').values_list('protocol', flat=True) @@ -48,12 +51,25 @@ def find_mirrors(request, countries=None, protocols=None): ) if countries and 'all' not in countries: qset = qset.filter(mirror__country__in=countries) - qset = qset.order_by('mirror__country', 'mirror__name', 'url') - return direct_to_template(request, 'mirrors/mirrorlist.txt', { - 'mirror_urls': qset, + if not use_status: + urls = qset.order_by('mirror__country', 'mirror__name', 'url') + template = 'mirrors/mirrorlist.txt' + else: + scores = dict([(u.id, u.score) for u in get_mirror_statuses()]) + urls = [] + for u in qset: + u.score = scores[u.id] + if u.score and u.score < 100.0: + urls.append(u) + urls = sorted(urls, key=attrgetter('score')) + template = 'mirrors/mirrorlist_status.txt' + + return direct_to_template(request, template, { + 'mirror_urls': urls, }, mimetype='text/plain') + def status(request): bad_timedelta = datetime.timedelta(days=3) urls = get_mirror_statuses() diff --git a/templates/mirrors/index.html b/templates/mirrors/index.html index 8c193ba..1d620b1 100644 --- a/templates/mirrors/index.html +++ b/templates/mirrors/index.html @@ -31,7 +31,9 @@ pacman mirrorlist based on geography and desired protocol(s). Simply replace the contents of - /etc/pacman.d/mirrorlist with your generated list.

+ /etc/pacman.d/mirrorlist with your generated list. + Additionally, the mirror status data can be incorporated into the generated + mirror list and used to pre-order the mirrors.

{{ mirrorlist_form.as_p }} diff --git a/templates/mirrors/mirrorlist.txt b/templates/mirrors/mirrorlist.txt index 9d5b691..2eedbd7 100644 --- a/templates/mirrors/mirrorlist.txt +++ b/templates/mirrors/mirrorlist.txt @@ -3,11 +3,11 @@ Yes, ugly templates are ugly, but in order to keep line breaks where we want them, sacrifices have to be made. If editing this template, it is easiest to forget about where line breaks are happening until you are done getting the content right, and then go back later to fix it all up. -{% endcomment %}{% autoescape off %}# -# Arch Linux repository mirrorlist -# Generated on {% now "Y-m-d" %} -#{% for mirror_url in mirror_urls %}{% ifchanged %} +{% endcomment %}{% autoescape off %}## +## Arch Linux repository mirrorlist +## Generated on {% now "Y-m-d" %} +##{% for mirror_url in mirror_urls %}{% ifchanged %} -# {{ mirror_url.mirror.country }}{% endifchanged %} +## {{ mirror_url.mirror.country }}{% endifchanged %} #Server = {{ mirror_url.url}}$repo/os/$arch{% endfor %} {% endautoescape %} diff --git a/templates/mirrors/mirrorlist_status.txt b/templates/mirrors/mirrorlist_status.txt new file mode 100644 index 0000000..e2fbc1e --- /dev/null +++ b/templates/mirrors/mirrorlist_status.txt @@ -0,0 +1,13 @@ +{% comment %} +Yes, ugly templates are ugly, but in order to keep line breaks where we want +them, sacrifices have to be made. If editing this template, it is easiest to +forget about where line breaks are happening until you are done getting the +content right, and then go back later to fix it all up. +{% endcomment %}{% autoescape off %}## +## Arch Linux repository mirrorlist +## Sorted by mirror score from mirror status page +## Generated on {% now "Y-m-d" %} +{% for mirror_url in mirror_urls %} +## Score: {{ mirror_url.score|floatformat:1|default:'unknown' }}, {{ mirror_url.mirror.country }} +#Server = {{ mirror_url.url}}$repo/os/$arch{% endfor %} +{% endautoescape %} -- cgit v1.2.3-24-g4f1b