forked from pypi/warehouse
/
models.py
570 lines (483 loc) · 19 KB
/
models.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# Copyright 2013 Donald Stufft
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
import datetime
import os.path
import urlparse
import logging
from warehouse import db, models
from warehouse.packaging.tables import ReleaseDependencyKind
log = logging.getLogger(__name__)
class Model(models.Model):
get_project_count = db.scalar(
"SELECT COUNT(*) FROM packages"
)
get_download_count = db.scalar(
"SELECT SUM(downloads) FROM release_files",
default=0,
)
def get_recently_updated(self, num=10):
# We only consider releases made in the last 7 days, otherwise we have
# to do a Sequence Scan against the entire table and it takes 5+
# seconds to complete. This shouldn't be a big deal as it is highly
# unlikely we'll have a week without at least 10 releases.
query = \
""" SELECT *
FROM (
SELECT DISTINCT ON (name) name, version, summary, created
FROM releases
WHERE created >= now() - interval '7 days'
ORDER BY name, created DESC
) r
ORDER BY r.created DESC
LIMIT %(num)s
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, num=num)]
def get_releases_since(self, since):
query = \
""" SELECT name, version, created, summary
FROM releases
WHERE created > %(since)s
ORDER BY created DESC
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, since=since)]
def get_changed_since(self, since):
query = \
"""SELECT name, max(submitted_date) FROM journals
WHERE submitted_date > %(since)s
GROUP BY name
ORDER BY max(submitted_date) DESC
"""
with self.engine.connect() as conn:
return [r[0] for r in conn.execute(query, since=since)]
def all_projects(self):
query = "SELECT name FROM packages ORDER BY lower(name)"
with self.engine.connect() as conn:
return [r["name"] for r in conn.execute(query)]
def get_top_projects(self, num=None):
query = \
""" SELECT name, sum(downloads)
FROM release_files
GROUP BY name
ORDER BY sum(downloads) DESC
"""
if num:
query += "LIMIT %(limit)s"
with self.engine.connect() as conn:
return [tuple(r) for r in conn.execute(query, limit=num)]
def get_project(self, name):
query = \
""" SELECT name
FROM packages
WHERE normalized_name = lower(
regexp_replace(%(name)s, '_', '-', 'ig')
)
"""
with self.engine.connect() as conn:
return conn.execute(query, name=name).scalar()
def get_projects_for_user(self, username):
query = \
""" SELECT DISTINCT ON (lower(name)) name, summary
FROM (
SELECT package_name
FROM roles
WHERE user_name = %(username)s
) roles
INNER JOIN (
SELECT name, summary
FROM releases
ORDER BY _pypi_ordering DESC
) releases
ON (releases.name = roles.package_name)
ORDER BY lower(name)
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, username=username)]
def get_users_for_project(self, project):
query = \
""" SELECT DISTINCT ON (u.username) u.username, u.email
FROM (
SELECT username, email
FROM accounts_user
LEFT OUTER JOIN accounts_email ON (
accounts_email.user_id = accounts_user.id
)
) u
INNER JOIN roles ON (u.username = roles.user_name)
WHERE roles.package_name = %(project)s
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, project=project)]
def get_roles_for_project(self, project):
query = \
""" SELECT user_name, role_name
FROM roles
WHERE package_name = %(project)s
ORDER BY role_name, user_name
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, project=project)]
def get_roles_for_user(self, user):
query = \
""" SELECT package_name, role_name
FROM roles
WHERE user_name = %(user)s
ORDER BY package_name, role_name
"""
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, user=user)]
def get_hosting_mode(self, name):
query = "SELECT hosting_mode FROM packages WHERE name = %(project)s"
with self.engine.connect() as conn:
return conn.execute(query, project=name).scalar()
def get_release_urls(self, name):
query = \
""" SELECT version, home_page, download_url
FROM releases
WHERE name = %(project)s
ORDER BY version DESC
"""
with self.engine.connect() as conn:
return {
r["version"]: (r["home_page"], r["download_url"])
for r in conn.execute(query, project=name)
}
def get_external_urls(self, name):
query = \
""" SELECT DISTINCT ON (url) url
FROM description_urls
WHERE name = %(project)s
ORDER BY url
"""
with self.engine.connect() as conn:
return [r["url"] for r in conn.execute(query, project=name)]
def get_file_urls(self, name):
query = \
""" SELECT name, filename, python_version, md5_digest
FROM release_files
WHERE name = %(project)s
ORDER BY filename DESC
"""
with self.engine.connect() as conn:
results = conn.execute(query, project=name)
return [
{
"filename": r["filename"],
"url": urlparse.urljoin(
"/".join([
"../../packages",
r["python_version"],
r["name"][0],
r["name"],
r["filename"],
]),
"#md5={}".format(r["md5_digest"]),
),
}
for r in results
]
def get_project_for_filename(self, filename):
query = "SELECT name FROM release_files WHERE filename = %(filename)s"
with self.engine.connect() as conn:
return conn.execute(query, filename=filename).scalar()
def get_filename_md5(self, filename):
query = \
""" SELECT md5_digest
FROM release_files
WHERE filename = %(filename)s
"""
with self.engine.connect() as conn:
return conn.execute(query, filename=filename).scalar()
def get_last_serial(self, name=None):
if name is not None:
query = "SELECT MAX(id) FROM journals WHERE name = %(name)s"
else:
query = "SELECT MAX(id) FROM journals"
with self.engine.connect() as conn:
return conn.execute(query, name=name).scalar()
def get_projects_with_serial(self):
# return list of dict(name: max id)
query = "SELECT name, max(id) FROM journals GROUP BY name"
with self.engine.connect() as conn:
return dict(r for r in conn.execute(query))
def get_project_versions(self, project):
query = \
""" SELECT version
FROM releases
WHERE name = %(project)s
ORDER BY _pypi_ordering DESC
"""
with self.engine.connect() as conn:
return [r["version"] for r in conn.execute(query, project=project)]
def get_downloads(self, project, version):
query = \
""" SELECT
name, version, python_version, packagetype, comment_text,
filename, md5_digest, downloads, upload_time
FROM release_files
WHERE name = %(project)s AND version = %(version)s
ORDER BY packagetype, python_version, upload_time
"""
results = []
with self.engine.connect() as conn:
for r in conn.execute(query, project=project, version=version):
result = dict(r)
result["filepath"] = os.path.join(
self.app.config.paths.packages,
result["python_version"],
result["name"][0],
result["name"],
result["filename"],
)
if not os.path.exists(result["filepath"]):
log.error(
"%s missing for package %s %s",
result["filepath"],
result["name"],
result["version"])
continue
result["url"] = "/".join([
"/packages",
result["python_version"],
result["name"][0],
result["name"],
result["filename"],
])
result["size"] = os.path.getsize(result["filepath"])
if os.path.exists(result["filepath"] + ".asc"):
result["pgp_url"] = result["url"] + ".asc"
else:
result["pgp_url"] = None
results.append(result)
return results
def get_release(self, project, version):
query = \
""" SELECT
name, version, author, author_email, maintainer,
maintainer_email, home_page, license, summary, description,
keywords, platform, download_url, created
FROM releases
WHERE name = %(project)s AND version = %(version)s
ORDER BY _pypi_ordering DESC
LIMIT 1
"""
with self.engine.connect() as conn:
result = [
dict(r)
for r in conn.execute(query, project=project, version=version)
][0]
# Load dependency information
query = \
""" SELECT name, version, kind, specifier
FROM release_dependencies
WHERE name = %(project)s AND version = %(version)s
ORDER BY kind, specifier
"""
dependency_data = {}
with self.engine.connect() as conn:
for dependency in conn.execute(
query,
project=project,
version=version):
kind = ReleaseDependencyKind(dependency["kind"])
if kind in {
ReleaseDependencyKind.requires_dist,
ReleaseDependencyKind.provides_dist,
ReleaseDependencyKind.obsoletes_dist}:
value = dependency_data.setdefault(kind.name, [])
value.append(dependency["specifier"])
if kind is ReleaseDependencyKind.project_url:
value = dependency_data.setdefault(kind.name, {})
value.update(dict([dependency["specifier"].split(",", 1)]))
result.update(dependency_data)
return result
def get_releases(self, project):
# Get the release data
query = \
""" SELECT
name, version, author, author_email, maintainer,
maintainer_email, home_page, license, summary, keywords,
platform, download_url, created
FROM releases
WHERE name = %(project)s
ORDER BY _pypi_ordering DESC
"""
with self.engine.connect() as conn:
results = [dict(r) for r in conn.execute(query, project=project)]
return results
def get_full_latest_releases(self):
query = \
""" SELECT DISTINCT ON (name)
name, version, author, author_email, maintainer,
maintainer_email, home_page, license, summary, description,
keywords, platform, download_url, created
FROM releases
ORDER BY name, _pypi_ordering DESC
"""
with self.engine.connect() as conn:
results = [dict(r) for r in conn.execute(query)]
return results
def get_download_counts(self, project):
def _make_key(precision, datetime, key):
return "downloads:{}:{}:{}".format(
precision[0],
datetime.strftime(precision[1]),
key,
)
precisions = [
("hour", "%y-%m-%d-%H"),
("daily", "%y-%m-%d"),
]
# Get the current utc time
current = datetime.datetime.utcnow()
# Get the download count for the last 24 hours (roughly)
keys = [
_make_key(
precisions[0],
current - datetime.timedelta(hours=x),
project,
)
for x in range(25)
]
last_1 = sum(
[int(x) for x in self.redis.mget(*keys) if x is not None]
)
# Get the download count for the last 7 days (roughly)
keys = [
_make_key(
precisions[1],
current - datetime.timedelta(days=x),
project,
)
for x in range(8)
]
last_7 = sum(
[int(x) for x in self.redis.mget(*keys) if x is not None]
)
# Get the download count for the last month (roughly)
keys = [
_make_key(
precisions[1],
current - datetime.timedelta(days=x),
project,
)
for x in range(31)
]
last_30 = sum(
[int(x) for x in self.redis.mget(*keys) if x is not None]
)
return {
"last_day": last_1,
"last_week": last_7,
"last_month": last_30,
}
def get_classifiers(self, project, version):
query = \
""" SELECT classifier
FROM release_classifiers
INNER JOIN trove_classifiers ON (
release_classifiers.trove_id = trove_classifiers.id
)
WHERE name = %(project)s AND version = %(version)s
ORDER BY classifier
"""
with self.engine.connect() as conn:
return [
r["classifier"]
for r in conn.execute(query, project=project, version=version)
]
def get_classifier_ids(self, classifiers):
placeholders = ', '.join(['%s'] * len(classifiers))
query = \
"""SELECT classifier, id
FROM trove_classifiers
WHERE classifier IN (%s)
""" % placeholders
with self.engine.connect() as conn:
return dict((r['classifier'], r['id'])
for r in conn.execute(query, *classifiers))
def search_by_classifier(self, selected_classifiers):
# Note: selected_classifiers is a list of ids from trove_classifiers
if not selected_classifiers:
return []
# generate trove id -> level mapping
trove = {}
query = "SELECT * FROM trove_classifiers"
with self.engine.connect() as conn:
for id, classifier, l2, l3, l4, l5 in conn.execute(query):
if id == l2:
trove[id] = 2
elif id == l3:
trove[id] = 3
elif id == l4:
trove[id] = 4
else:
trove[id] = 5
# compute a statement to produce all packages selected
query = "SELECT name, version FROM releases"
for c in selected_classifiers:
level = trove[c]
query = \
""" SELECT DISTINCT a.name, a.version
FROM (%s) a, release_classifiers rc, trove_classifiers t
WHERE a.name=rc.name
AND a.version=rc.version
AND rc.trove_id=t.id
AND t.l%d=%d
""" % (query, level, c)
releases = []
with self.engine.connect() as conn:
for name, version in conn.execute(query):
releases.append((name.decode('utf-8'), version))
return releases
def get_documentation_url(self, project):
path_parts = [
self.app.config.paths.documentation,
project,
"index.html",
]
if os.path.exists(os.path.join(*path_parts)):
return urlparse.urljoin(
self.app.config.urls.documentation,
project
) + "/"
def get_bugtrack_url(self, project):
query = "SELECT bugtrack_url FROM packages WHERE name = %(project)s"
with self.engine.connect() as conn:
return conn.execute(query, project=project).scalar()
#
# Mirroring support
#
def get_changelog(self, since):
query = '''SELECT name, version, submitted_date, action, id
FROM journals
WHERE journals.submitted_date > %(since)s
ORDER BY submitted_date DESC
'''
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, since=since)]
def get_last_changelog_serial(self):
with self.engine.connect() as conn:
return conn.execute('SELECT max(id) FROM journals').scalar()
def get_changelog_serial(self, since):
query = '''SELECT name, version, submitted_date, action, id
FROM journals
WHERE journals.id > %(since)s
ORDER BY submitted_date DESC
'''
with self.engine.connect() as conn:
return [dict(r) for r in conn.execute(query, since=since)]