-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmirrorlib.py
215 lines (175 loc) · 7.23 KB
/
mirrorlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Open Source Initiative OSI - The MIT License (MIT):Licensing
#
# The MIT License (MIT)
# Copyright (c) 2012 Ken Cochrane ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
""" This is a simple library that you can use to find out the status of
PyPI mirrors. It is based on the information that is found at
http://www.python.org/dev/peps/pep-0381/ and
http://pypi.python.org/mirrors
Updated for PEP 449
http://www.python.org/dev/peps/pep-0449/
"""
import datetime
import urllib2
import time
import operator
MIRROR_URL_FORMAT = "{0}://{1}/last-modified"
MASTER_URL_FORMAT = "https://{0}/daytime"
MASTER_SERVER = "pypi.python.org"
STATUSES = {'GREEN':'Green',
'YELLOW':'Yellow',
'RED':'Red'}
def ping_mirror(mirror_url):
""" Given a mirror_url it will ping it and return the contents and
the response time """
try:
start = time.time()
res = urllib2.urlopen(mirror_url)
stop = time.time()
response_time = round((stop - start) * 1000, 2)
return res.read().strip(), response_time
except Exception:
return None, None
def parse_date(date_str):
""" parse the date the get back from the mirror """
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, other_date=None, offset=None, sign="ago"):
""" This function prints the difference between two python datetime objects
in a more human readable form
"""
if other_date:
dt = abs(now - other_date)
delta_d, offset = dt.days, dt.seconds
if now < other_date:
sign = "ahead"
elif offset:
delta_d, offset = divmod(offset, 60 * 60 * 24)
else:
raise ValueError("Must supply other_date or offset (from now)")
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:
fmt = "{s:d} seconds {ago}"
return fmt.format(d=delta_d, h=delta_h, m=delta_m, s=delta_s, ago=sign)
def mirror_status_desc(how_old):
""" Get the status description of the mirror """
if how_old < datetime.timedelta(minutes=60):
return STATUSES.get('GREEN')
elif how_old < datetime.timedelta(days=1):
return STATUSES.get('YELLOW')
else:
return STATUSES.get('RED')
def ping_master_pypi_server(master_url_format=MASTER_URL_FORMAT):
""" Ping the master Pypi server, it is a little different
then the other servers. """
# a.pypi.python.org is the master server treat it differently
m_url = master_url_format.format(MASTER_SERVER)
res, res_time = ping_mirror(m_url)
return MASTER_SERVER, res, res_time
def mirror_statuses(mirror_url_format=MIRROR_URL_FORMAT,
mirrors=None,
ping_master_mirror=True):
""" get the data we need from the mirrors and return a list of
dictionaries with information about each mirror
``mirror_url_format`` - Change the url format from the standard one
``mirrors`` - provided the list if mirrors to check.
The list needs to contain a tuple, (protocal, domain) for example:
[('http, 'pypi.example.com'), ('https', 'pypi2.example.com')]
``ping_master_mirror`` - Do you want to include the status of the master
server in the results. Defaults to True.
"""
if not mirrors:
return []
# scan the mirrors and collect data
ping_results = []
for protocol, ml in mirrors:
m_url = mirror_url_format.format(protocol, ml)
res, res_time = ping_mirror(m_url)
ping_results.append((ml, res, res_time))
if ping_master_mirror:
# pypi.python.org is the master server treat it differently
master_results = ping_master_pypi_server()
ping_results.insert(0, master_results)
now = datetime.datetime.utcnow()
results = []
for ml, res, res_time in ping_results:
if res:
last_update = parse_date(res)
time_diff = abs(now - last_update)
status = mirror_status_desc(time_diff)
time_diff_human = humanize_date_difference(now, last_update)
results.append({'mirror': ml,
'last_update': last_update,
'time_now': now,
'time_diff': time_diff,
'time_diff_human': time_diff_human,
'response_time': res_time,
'status': status}
)
else:
results.append({'mirror': ml,
'last_update': "Unavailable",
'time_now': now,
'time_diff_human': "Unavailable",
'time_diff': 'Unavailable',
'response_time': "Unavailable",
'status': 'Unavailable'}
)
return results
def is_master_alive():
""" Check if the Master server is alive """
server, response, res_time = ping_master_pypi_server()
if response is None:
return False
return True
def find_out_of_date_mirrors(mirrors=None):
""" Find the mirrors that are out of date """
results = mirror_statuses(mirrors=mirrors)
bad_mirrors = []
for r in results:
if r.get('status') == STATUSES.get('RED'):
bad_mirrors.append(r)
return bad_mirrors
def __find_mirror_sort(sort_field, mirrors=None, reverse=False):
""" Find the first mirror that is sorted by sort_field """
results = mirror_statuses(mirrors=mirrors, ping_master_mirror=False)
new_list = sorted(results, key=operator.itemgetter(sort_field), reverse=reverse)
return new_list[0]
def find_fastest_mirror(mirrors=None):
""" Find the fastest mirror (via response time), might not be up to date """
return __find_mirror_sort('response_time', mirrors=mirrors)
def find_freshest_mirror(mirrors=None):
""" Find the freshest mirror (via last updated) """
return __find_mirror_sort('time_diff', mirrors=mirrors)