Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

build the list of mirrors dynamically #1

Merged
merged 4 commits into from

2 participants

Florent Xicluna Ken Cochrane
Florent Xicluna

I pushed few changes:

  • build the list of mirrors using "last.pypi.python.org"
  • remove external dependency on "requests"
  • use UTC time to not depend on the host timezone
Ken Cochrane
Owner

Looks good, thanks.

Ken Cochrane kencochrane merged commit 70e766f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 52 additions and 42 deletions.
  1. +52 −42 pypi_mirrors.py
94 pypi_mirrors.py
View
@@ -1,15 +1,14 @@
#!/usr/bin/env python
-import requests
import datetime
+import socket
+import urllib2
-mirrors = ['b.pypi.python.org',
- 'c.pypi.python.org',
- 'd.pypi.python.org',
- 'e.pypi.python.org',
- 'f.pypi.python.org',
- 'g.pypi.python.org']
-mirror_url = "http://{0}/last-modified"
+# Add non-official mirrors here
+MIRRORS = [
+ # 'pypi.crate.io',
+]
+MIRROR_URL = "http://{0}/last-modified"
#TODO: replace with a template system and nice html/css
page = """<html><head><title>PyPI Mirror Status</title></head><body>
@@ -19,7 +18,7 @@
{body}
<hr>
-Page Last updated at {date_now} <br />
+Page last updated at {date_now} <br />
Built with:
<a href='https://github.com/kencochrane/pypi-mirrors'>pypi-mirrors</a>
</body>
@@ -27,16 +26,35 @@
"""
+def get_mirrors():
+ # http://pypi.python.org/mirrors
+ res = socket.gethostbyname_ex('last.pypi.python.org')[0]
+ last, dot, suffix = res.partition('.')
+ assert suffix == 'pypi.python.org'
+ mirrors = []
+ for l in range(ord('a'), ord(last) + 1):
+ mirrors.append('{0:c}.{1}'.format(l, suffix))
+ mirrors.extend(MIRRORS)
+ return mirrors
+
+
def ping_mirror(mirror_url):
- res = requests.get(mirror_url)
- if res.ok:
- return res.content.strip()
- return None
+ try:
+ res = urllib2.urlopen(mirror_url)
+ return res.read().strip()
+ except Exception:
+ return None
def parse_date(date_str):
""" parse the date the get back from the mirror """
- return datetime.datetime.strptime(date_str, '%Y%m%dT%H:%M:%S')
+ if len(date_str) == 17:
+ # Used on official mirrors
+ date_fmt = '%Y%m%dT%H:%M:%S'
+ else:
+ # Canonical ISO-8601 format (compliant with PEP 381)
+ date_fmt = '%Y-%m-%dT%H:%M:%S'
+ return datetime.datetime.strptime(date_str, date_fmt)
def humanize_date_difference(now, otherdate=None, offset=None):
@@ -50,35 +68,29 @@ def humanize_date_difference(now, otherdate=None, offset=None):
"""
if otherdate:
dt = now - otherdate
- offset = dt.seconds + (dt.days * 60 * 60 * 24)
- if offset:
- delta_s = offset % 60
- offset /= 60
- delta_m = offset % 60
- offset /= 60
- delta_h = offset % 24
- offset /= 24
- delta_d = offset
+ delta_d, offset = dt.days, dt.seconds
+ elif offset:
+ delta_d, offset = divmod(offset, 60 * 60 * 24)
else:
raise ValueError("Must supply otherdate or offset (from now)")
-
- if delta_d > 0:
- return "%d days, %d hours, %d minutes ago" % (delta_d,
- delta_h,
- delta_m)
- if delta_h > 0:
- return "%d hours, %d minutes ago" % (delta_h, delta_m)
- if delta_m > 0:
- return "%d minutes, %d seconds ago" % (delta_m, delta_s)
+ offset, delta_s = divmod(offset, 60)
+ delta_h, delta_m = divmod(offset, 60)
+
+ if delta_d:
+ fmt = "{d:d} days, {h:d} hours, {m:d} minutes ago"
+ elif delta_h:
+ fmt = "{h:d} hours, {m:d} minutes ago"
+ elif delta_m:
+ fmt = "{m:d} minutes, {s:d} seconds ago"
else:
- return "%d seconds ago" % delta_s
+ fmt = "{s:d} seconds ago"
+ return fmt.format(d=delta_d, h=delta_h, m=delta_m, s=delta_s)
-def gather_data():
+def gather_data(now, mirror_url=MIRROR_URL):
""" get the data we need put in dict """
- now = datetime.datetime.now()
results = []
- for ml in mirrors:
+ for ml in get_mirrors():
m_url = mirror_url.format(ml)
res = ping_mirror(m_url)
@@ -98,15 +110,13 @@ def gather_data():
def generate_page(format='html'):
- now = datetime.datetime.now()
- data = gather_data()
+ now = datetime.datetime.utcnow()
+ data = gather_data(now)
body = "<table border='1' width='50%'>"
body += "<tr><th>Mirror</th><th>Last update</th><th>Age</th></tr>"
- row = "<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>"
+ row = "<tr><td>{mirror}</td><td>{last_update}</td><td>{how_old}</td></tr>"
for d in data:
- body += row.format(d.get("mirror", "n/a"),
- d.get("last_update", "Unavailable"),
- d.get("how_old", "Unavailable"))
+ body += row.format(**d)
body += "</table>"
page_out = page.format(body=body, date_now=now)
print page_out
Something went wrong with that request. Please try again.