This repository has been archived by the owner on May 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
rpc.py
executable file
·406 lines (360 loc) · 15.2 KB
/
rpc.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import os
import sys
import xmlrpclib
import traceback
import datetime
import logging
import re
import time
import json
from cStringIO import StringIO
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from collections import defaultdict
from contextlib import contextmanager
from perfmetrics import metric
from perfmetrics import metricmethod
from perfmetrics import set_statsd_client
from perfmetrics import statsd_client
import redis
# local imports
import config
from store import dependency
from fncache import RedisLru
from dogadapter import dogstatsd
root = os.path.dirname(os.path.abspath(__file__))
conf = config.Config(os.path.join(root, "config.ini"))
redis_kwargs = {
'socket_connect_timeout': 0.1,
'socket_timeout': 0.05,
}
if conf.cache_redis_url is None:
cache_redis = None
else:
cache_redis = redis.StrictRedis.from_url(conf.cache_redis_url, **redis_kwargs)
# Note: slice object is to cut off the instance of Store that would be passed along
package_tag_lru = RedisLru(cache_redis, expires=86400, tag="pkg~%s", arg_index=1, slice_obj=slice(1, None))
cache_by_pkg = package_tag_lru.decorator
if conf.xmlrpc_redis_url is None:
xmlrpc_redis = None
else:
xmlrpc_redis = redis.StrictRedis.from_url(conf.xmlrpc_redis_url, **redis_kwargs)
STATSD_URI = "statsd://127.0.0.1:8125?prefix=%s" % (conf.database_name)
set_statsd_client(STATSD_URI)
statsd_reporter = statsd_client()
def log_xmlrpc_request(remote_addr, user_agent, data):
if conf.xmlrpc_request_log_file:
try:
with open(conf.xmlrpc_request_log_file, 'a') as f:
params, method = xmlrpclib.loads(data)
dogstatsd.increment('xmlrpc.request', tags=['method:{}'.format(method)])
record = json.dumps({
'timestamp': datetime.datetime.utcnow().isoformat(),
'remote_addr': remote_addr,
'user_agent': user_agent,
'method': method,
'params': params,
'type': 'request',
})
f.write(record + '\n')
except Exception:
pass
def log_xmlrpc_response(remote_addr, user_agent, data, response_size):
if conf.xmlrpc_request_log_file:
try:
with open(conf.xmlrpc_request_log_file, 'a') as f:
params, method = xmlrpclib.loads(data)
dogstatsd.increment('xmlrpc.response', tags=['method:{}'.format(method)])
dogstatsd.histogram('xmlrpc.response.size', response_size, tags=['method:{}'.format(method)])
record = json.dumps({
'timestamp': datetime.datetime.utcnow().isoformat(),
'remote_addr': remote_addr,
'user_agent': user_agent,
'method': method,
'params': params,
'response_size': response_size,
'type': 'response',
})
f.write(record + '\n')
except Exception:
pass
def log_xmlrpc_throttle(remote_addr, enforced):
if conf.xmlrpc_request_log_file:
try:
with open(conf.xmlrpc_request_log_file, 'a') as f:
dogstatsd.increment('xmlrpc.throttled', tags=['remote_addr:{}'.format(remote_addr), 'enforced:{}'.format(enforced)])
record = json.dumps({
'timestamp': datetime.datetime.utcnow().isoformat(),
'remote_addr': remote_addr,
'method': 'throttled',
'throttle_enforced': enforced,
})
f.write(record + '\n')
except Exception:
pass
@contextmanager
def throttle_concurrent(remote_addr):
throttled = False
try:
if xmlrpc_redis:
dogstatsd.increment('xmlrpc.rate-limit.invoke')
statsd_reporter.incr('rpc-rl.invoke')
pipeline = xmlrpc_redis.pipeline()
pipeline.incr(remote_addr)
pipeline.expire(remote_addr, 60)
current = pipeline.execute()[0]
if current >= conf.xmlrpc_concurrent_requests:
dogstatsd.increment('xmlrpc.rate-limit.over')
statsd_reporter.incr('rpc-rl.over')
log_xmlrpc_throttle(remote_addr, conf.xmlrpc_enforce)
if conf.xmlrpc_enforce:
dogstatsd.increment('xmlrpc.rate-limit.enforce')
statsd_reporter.incr('rpc-rl.enforce')
throttled = True
except Exception:
dogstatsd.increment('xmlrpc.rate-limit.context.before.error')
statsd_reporter.incr('rpc-rl.context.before.error')
pass
yield throttled
try:
if xmlrpc_redis:
xmlrpc_redis.decr(remote_addr)
except Exception:
dogstatsd.increment('xmlrpc.rate-limit.context.after.error')
statsd_reporter.incr('rpc-rl.context.after.error')
pass
class RequestHandler(SimpleXMLRPCDispatcher):
"""A request dispatcher for the PyPI XML-RPC API."""
def __init__(self):
SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8')
self.register_function(list_packages)
self.register_function(package_releases)
self.register_function(release_urls)
self.register_function(release_urls, name='package_urls') # Deprecated
self.register_function(release_data)
self.register_function(release_data, name='package_data') # Deprecated
self.register_function(search)
self.register_function(browse)
self.register_function(updated_releases)
self.register_function(changelog)
self.register_function(changelog_last_serial)
self.register_function(changelog_since_serial)
self.register_function(changed_packages)
self.register_function(post_cheesecake_for_release)
self.register_function(release_downloads)
self.register_function(package_roles)
self.register_function(user_packages)
self.register_function(package_hosting_mode)
self.register_function(top_packages)
self.register_function(list_packages_with_serial)
self.register_introspection_functions()
self.register_multicall_functions()
@dogstatsd.timed('xmlrpc.call')
@metricmethod
def __call__(self, webui_obj):
webui_obj.handler.send_response(200, 'OK')
webui_obj.handler.send_header('Content-type', 'text/xml')
webui_obj.handler.send_header('charset', 'UTF-8' )
webui_obj.handler.end_headers()
try:
length = int(webui_obj.env['CONTENT_LENGTH'])
assert length < 10*1024*1024, 'request content way too big'
data = webui_obj.handler.rfile.read(length)
# This should be thread-safe, as the store is really a singleton
self.store = webui_obj.store
except Exception, e:
# report as a fault to caller rather than propogating up to generic
# exception handler
response = xmlrpclib.dumps(
xmlrpclib.Fault(1, repr(e)),
encoding=self.encoding,
allow_none=self.allow_none
)
else:
user_agent = webui_obj.env.get('HTTP_USER_AGENT', None)
log_xmlrpc_request(webui_obj.remote_addr, user_agent, data)
with throttle_concurrent(webui_obj.remote_addr) as is_throttled:
if is_throttled:
response = xmlrpclib.dumps(
xmlrpclib.Fault(429, 'max concurrent requests exceeded'),
encoding=self.encoding,
allow_none=self.allow_none
)
else:
# errors here are handled by _marshaled_dispatch
response = self._marshaled_dispatch(data)
# remove non-printable ASCII control codes from the response
response = re.sub('([\x00-\x08]|[\x0b-\x0c]|[\x0e-\x1f])+', '', response)
log_xmlrpc_response(webui_obj.remote_addr, user_agent, data, len(response))
webui_obj.handler.wfile.write(response)
@dogstatsd.timed('xmlrpc.dispatch')
@metricmethod
def _dispatch(self, method, params):
if not method.startswith('system.'):
# Add store to all of our own methods
params = (self.store,)+tuple(params)
return SimpleXMLRPCDispatcher._dispatch(self, method, params)
@dogstatsd.timed('xmlrpc.multicall')
@metricmethod
def system_multicall(self, call_list):
if len(call_list) > 100:
raise Fault, "multicall too large"
return SimpleXMLRPCDispatcher.system_multicall(self, call_list)
@dogstatsd.timed('xmlrpc.function', tags=['function:package_hosting_mode'])
@metric
def package_hosting_mode(store, package_name):
"""Returns the hosting mode for a given package."""
return store.get_package_hosting_mode(package_name)
@dogstatsd.timed('xmlrpc.function', tags=['function:release_downloads'])
@metric
@cache_by_pkg
def release_downloads(store, package_name, version):
'''Return download count for given release.'''
return store.get_release_downloads(package_name, version)
@dogstatsd.timed('xmlrpc.function', tags=['function:package_roles'])
@metric
@cache_by_pkg
def package_roles(store, package_name):
'''Return associated users and package roles.'''
result = store.get_package_roles(package_name)
return [tuple(fields.values())for fields in result]
@dogstatsd.timed('xmlrpc.function', tags=['function:user_packages'])
@metric
def user_packages(store, user):
'''Return associated packages for user.'''
result = store.get_user_packages(user)
return [tuple(fields.values()) for fields in result]
@dogstatsd.timed('xmlrpc.function', tags=['function:list_packages'])
@metric
def list_packages(store):
result = store.get_packages()
return [row['name'] for row in result]
@dogstatsd.timed('xmlrpc.function', tags=['function:list_packages_with_serial'])
@metric
def list_packages_with_serial(store):
return store.get_packages_with_serial()
@dogstatsd.timed('xmlrpc.function', tags=['function:package_releases'])
@metric
@cache_by_pkg
def package_releases(store, package_name, show_hidden=False):
if show_hidden:
hidden = None
else:
hidden = False
result = store.get_package_releases(package_name, hidden=hidden)
return [row['version'] for row in result]
@dogstatsd.timed('xmlrpc.function', tags=['function:release_urls'])
@metric
def release_urls(store, package_name, version):
result = []
for file in store.list_files(package_name, version):
info = file.as_dict()
info['url'] = store.gen_file_url(info['python_version'],
package_name, info['filename'], path=info['path'])
info['digests'] = {'md5': file['md5_digest'], 'sha256': file['sha256_digest']}
result.append(info)
# TODO do something with release_urls when there is something to do
#info = store.get_package(package_name, version)
#if info['download_url']:
# result.append({'url': info['download_url']})
return result
package_urls = release_urls # "deprecated"
@dogstatsd.timed('xmlrpc.function', tags=['function:release_data'])
@metric
@cache_by_pkg
def release_data(store, package_name, version):
info = store.get_package(package_name, version)
if not info:
return {}
info = info.as_dict()
if "description_html" in info:
del info['description_html']
dependencies = defaultdict(list)
for kind, specifier in store.get_release_dependencies(package_name, version):
dependencies[dependency.by_val[kind]].append(specifier)
info.update(dependencies)
classifiers = [r[0] for r in store.get_release_classifiers(package_name,
version)]
info['classifiers' ] = classifiers
info['package_url'] = 'http://pypi.python.org/pypi/%s' % package_name
info['release_url'] = 'http://pypi.python.org/pypi/%s/%s' % (package_name,
version)
info['docs_url'] = store.docs_url(package_name)
try:
info['downloads'] = store.download_counts(package_name)
except redis.exceptions.ConnectionError as conn_fail:
info['downloads'] = {'last_month': 0, 'last_week': 0, 'last_day': 0}
return info
package_data = release_data # "deprecated"
@dogstatsd.timed('xmlrpc.function', tags=['function:search'])
@metric
def search(store, spec, operator='and'):
spec['_pypi_hidden'] = 'FALSE'
return [row.as_dict() for row in store.search_packages(spec, operator)]
@dogstatsd.timed('xmlrpc.function', tags=['function:browse'])
@metric
def browse(store, categories):
if not isinstance(categories, list):
raise TypeError, "Parameter categories must be a list"
classifier_ids = store.get_classifier_ids(categories)
if len(classifier_ids) != len(categories):
for c in categories:
if c not in classifier_ids:
raise ValueError, 'Unknown category "%s"' % c
ids = classifier_ids.values()
packages, tally = store.browse(ids)
return [(name, version) for name, version, desc in packages]
@dogstatsd.timed('xmlrpc.function', tags=['function:updated_releases'])
@metric
def updated_releases(store, since):
result = store.updated_releases(since)
return [(row['name'], row['version']) for row in result]
@dogstatsd.timed('xmlrpc.function', tags=['function:changelog_last_serial'])
@metric
def changelog_last_serial(store):
"return the last changelog event's serial"
return store.changelog_last_serial()
@dogstatsd.timed('xmlrpc.function', tags=['function:changelog'])
@metric
def changelog(store, since, with_ids=False):
result = []
for row in store.changelog(since, full=False):
if isinstance(row['submitted_date'], str):
d = datetime.datetime.strptime(row['submitted_date'],
'%Y-%m-%d %H:%M:%S').timetuple()
else:
d = row['submitted_date'].timetuple()
t = (row['name'],row['version'], int(time.mktime(d)), row['action'])
if with_ids:
t += (row['id'], )
result.append(t)
return result
@dogstatsd.timed('xmlrpc.function', tags=['function:changelog_since_serial'])
@metric
def changelog_since_serial(store, since_serial):
'return the changes since the nominated event serial (id)'
result = []
for row in store.changelog_since_serial(since_serial, full=False):
if isinstance(row['submitted_date'], str):
d = datetime.datetime.strptime(row['submitted_date'],
'%Y-%m-%d %H:%M:%S').timetuple()
else:
d = row['submitted_date'].timetuple()
result.append((row['name'],row['version'], int(time.mktime(d)),
row['action'], row['id']))
return result
@dogstatsd.timed('xmlrpc.function', tags=['function:changed_packages'])
@metric
def changed_packages(store, since):
return store.changed_packages(since)
@dogstatsd.timed('xmlrpc.function', tags=['function:post_cheesecake_for_release'])
@metric
def post_cheesecake_for_release(store, name, version, score_data, password):
if password != store.config.cheesecake_password:
raise ValuError("Bad password.")
store.save_cheesecake_score(name, version, score_data)
store.commit()
@dogstatsd.timed('xmlrpc.function', tags=['function:top_packages'])
@metric
def top_packages(store, num=None):
return store.top_packages(num=num)
handle_request = RequestHandler()