forked from pypi/legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
executable file
·2836 lines (2450 loc) · 105 KB
/
store.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
''' Implements a store of disutils PKG-INFO entries, keyed off name, version.
'''
import sys, os, re, time, hashlib, random, types, math, stat, errno
import logging, string, datetime, calendar, binascii, urllib, urllib2, cgi
import posixpath
from collections import defaultdict
import cPickle as pickle
try:
import psycopg2
except ImportError:
pass
try:
import sqlite3
sqlite3_cursor = sqlite3.Cursor
except ImportError:
sqlite3_cursor = type(None)
from defusedxml import ElementTree
import trove, openid2rp
from mini_pkg_resources import safe_name
# csrf modules
import hmac
from base64 import b64encode
import openid.store.sqlstore
import oauth
import requests
import urlparse
import time
from functools import wraps
import itertools
import readme_renderer.rst
import fs.errors
from pyblake2 import blake2b
import tasks
import packaging.version
try:
import psycopg2
OperationalError = psycopg2.OperationalError
IntegrityError = psycopg2.IntegrityError
except ImportError:
class OperationalError(Exception):
pass
class PreviouslyUsedFilename(Exception):
pass
class LockedException(Exception):
pass
# we import both the old and new (PEP 386) methods of handling versions since
# some version strings are not compatible with the new method and we can fall
# back on the old version
from distutils.version import LooseVersion
from verlib import NormalizedVersion, suggest_normalized_version
def enumerate(sequence):
return [(i, sequence[i]) for i in range(len(sequence))]
PRECISIONS = [
("hour", "%y-%m-%d-%H"),
("daily", "%y-%m-%d"),
]
def make_key(precision, datetime, key):
return "downloads:%s:%s:%s" % (
precision[0], datetime.strftime(precision[1]), key)
chars = string.ascii_letters + string.digits
dist_file_types = [
('sdist', 'Source'),
('bdist_dumb', '"dumb" binary'),
('bdist_rpm', 'RPM'),
('bdist_wininst', 'MS Windows installer'),
('bdist_msi', 'MS Windows MSI installer'),
('bdist_egg', 'Python Egg'),
('bdist_dmg', 'OS X Disk Image'),
('bdist_wheel', 'Python Wheel'),
]
dist_file_types_d = dict(dist_file_types)
# This could have been done with Postgres ENUMs, however
# a) they are not extensible, and
# b) they are not supported in other databases
class dependency:
requires = 1
provides = 2
obsoletes = 3
requires_dist = 4
provides_dist = 5
obsoletes_dist = 6
requires_external = 7
project_url = 8
by_val = {}
for k,v in dependency.__dict__.items():
if not isinstance(v, int):
continue
dependency.by_val[v] = k
keep_conn = False
connection = None
keep_trove = True
def normalize_package_name(n):
"Return lower-cased version of safe_name of n."
return safe_name(n).lower()
def normalize_version_number(v):
parsed = packaging.version.parse(v)
if isinstance(parsed, packaging.version.Version):
# We need to normalize the version, however we can't simply use the
# str() of the parsed version because we want to remove all of the
# trailing zeros for this.
parts = parsed.base_version.split("!")
parts[-1] = ".".join(reversed(list(itertools.dropwhile(lambda x: int(x) == 0, reversed(parts[-1].split("."))))))
fixed_base = "!".join(parts)
# Now that we have the base_version, we need to add the rest of our
# version pieces.
return fixed_base + str(parsed)[len(parsed.base_version):]
else:
return str(parsed)
class ResultRow:
'''Turn a tuple of row values into something that may be looked up by
both column index and name.
Also, convert any unicode values coming out of the database into UTF-8
encoded 8-bit strings.
'''
def __init__(self, cols, info=None):
self.cols = cols
self.cols_d = {}
for i, col in enumerate(cols):
self.cols_d[col] = i
self.info = info
def __getitem__(self, item):
if isinstance(item, int):
value = self.info[item]
else:
n = self.cols_d[item]
value = self.info[n]
return self.decode(value)
def __nonzero__(self):
return bool(self.info)
def items(self):
return [(col, self.decode(self.info[i]))
for i, col in enumerate(self.cols)]
def as_dict(self):
d = {}
for i, col in enumerate(self.cols):
d[col] = self.decode(self.info[i])
return d
def keys(self):
return self.cols
def values(self):
return map(self.decode, self.info)
def decode(self, value):
if value is None:
return value
if isinstance(value, str):
# decode strings stored as utf-8 into unicode
return value.decode('utf-8')
return value
def utf8getter(n):
def utf8get(fields):
if fields[n] is None: return fields[n]
return fields[n].decode('utf-8', 'replace')
return utf8get
def itemgetter(n):
return lambda fields:fields[n]
def FastResultRow(cols):
"""Create a ResultRow-like class that has all fields already preparsed.
Non-UTF-8-String columns must be suffixed with !."""
getters = {}
_keys = []
for i, col in enumerate(cols.split()):
if col[-1] == '!':
col = col[:-1]
getter = itemgetter(i)
else:
getter = utf8getter(i)
_keys.append(col)
getters[i] = getters[col] = getter
class _FastResultRow:
_getters = getters
cols = _keys
def __init__(self, cols, info):
self.info = info
def __getitem__(self, index):
try:
return self._getters[index](self.info)
except KeyError:
if isinstance(index, int):
raise IndexError, 'row index out of range'
raise
def __len__(self):
return len(self.info)
def __nonzero__(self):
return bool(self.info)
def as_dict(self):
res = {}
for key in self.cols:
res[key] = self[key]
return res
def keys(self):
return self.cols
def values(self):
res = [None] * len(self.info)
for i in xrange(len(self.info)):
res[i] = self[i]
return res
def items(self):
res = [None] * len(self.info)
for i, col in enumerate(self.cols):
res[i] = (col, self[col])
return res
return _FastResultRow
def Result(cols, sequence, type=ResultRow):
return [type(cols, item) for item in iter(sequence)]
def safe_execute(cursor, sql, params=None):
"""Tries to safely execute the given sql
This will try to encode the incoming parameters into UTF-8 (where
possible).
"""
# Fast path to no param queries
if params is None:
return cursor.execute(sql)
if isinstance(cursor, sqlite3_cursor):
sql = sql.replace('%s', "?")
# Encode every incoming param to UTF-8 if it's a string
safe_params = []
for param in params:
if isinstance(param, unicode):
safe_params.append(param.encode("UTF-8", "replace"))
else:
safe_params.append(param)
return cursor.execute(sql, safe_params)
def binary(cursor, bytes):
if isinstance(cursor, sqlite3_cursor):
# XXX is this correct?
return bytes
return psycopg2.Binary(bytes)
class StorageError(Exception):
pass
def _format_es_fields(hit):
name = hit['fields']['name'][0]
version = hit['fields']['version'][0]
summary = hit['fields'].get('summary', [None])[0]
if summary is not None:
summary = summary.encode('utf8')
_pypi_hidden = hit['fields'].get('_pypi_hidden', [False])[0]
return (name, version, summary, _pypi_hidden)
class Store:
''' Store info about packages, and allow query and retrieval.
XXX update schema info ...
Packages are unique by (name, version).
'''
def __init__(self, config, queue=None, redis=None, package_bucket=None):
self.config = config
self.username = None
self.userip = None
self._conn = None
self._cursor = None
self._trove = None
if self.config.database_driver == 'sqlite3':
self.true, self.false = '1', '0'
self.can_lock = False
else:
self.true, self.false = 'TRUE', 'FALSE'
self.can_lock = True
self.queue = queue
self.count_redis = redis
self.package_bucket = package_bucket
self._changed_packages = set()
self._deleted_files = set()
def package_url(self, url_path, name, version):
''' return a URL for the link to display a particular package
'''
if not isinstance(name, str): name = name.encode('utf-8')
if version is None:
# changelog entry with no version
version = ''
else:
if not isinstance(version, str): version = version.encode('utf-8')
version = '/'+urllib.quote(version)
return u'%s/%s%s'%(url_path, urllib.quote(name), version)
def enqueue(self, func, *args, **kwargs):
if self.queue is None:
func(*args, **kwargs)
else:
self.queue.enqueue(func, *args, **kwargs)
def download_counts(self, name):
# Download Counts from redis
download_counts = {}
if self.count_redis is not None:
# 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),
name,
)
for x in xrange(25)
]
last_1 = sum(
[int(x) for x in self.count_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),
name,
)
for x in xrange(8)
]
last_7 = sum(
[int(x) for x in self.count_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),
name,
)
for x in xrange(31)
]
last_30 = sum(
[int(x) for x in self.count_redis.mget(*keys) if x is not None]
)
download_counts = {
"last_day": last_1,
"last_week": last_7,
"last_month": last_30,
}
return download_counts
def last_id(self, tablename):
''' Return an SQL expression that returns the last inserted row,
where the row is in the given table.
'''
if self.config.database_driver == 'sqlite3':
return 'last_insert_rowid()'
else:
return "currval('%s_id_seq')" % tablename
def trove(self):
if not self._trove:
self._trove = trove.Trove(self.get_cursor())
return self._trove
def add_journal_entry(self, name, version, action, submitted_date,
submitted_by, submitted_from):
cursor = self.get_cursor()
safe_execute(cursor, """
INSERT INTO journals
(name, version, action, submitted_date, submitted_by,
submitted_from)
VALUES
(%s, %s, %s, %s, %s, %s)
""", (name, version, action, submitted_date, submitted_by,
submitted_from))
self._add_invalidation(name)
def store_package(self, name, version, info):
''' Store info about the package to the database.
If the name doesn't exist, we add a new package with the current
user as the Owner of the package.
If the version doesn't exist, we add a new release, hiding all
previous releases.
If the name and version do exist, we just edit (in place) and add a
journal entry.
'''
date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
cursor = self.get_cursor()
# see if we're inserting or updating a package
if not self.has_package(name):
# insert the new package entry
cols = 'name'
vals = '%s'
args = (name,)
# if a bugtracker url is provided then insert it too
if 'bugtrack_url' in info:
cols += ', bugtrack_url'
vals += ', %s'
args += (info['bugtrack_url'], )
sql = 'insert into packages (%s) values (%s)' % (cols, vals)
safe_execute(cursor, sql, args)
# journal entry
self.add_journal_entry(name, None, "create", date,
self.username, self.userip)
# first person to add an entry may be considered owner - though
# make sure they don't already have the Role (this might just
# be a new version, or someone might have already given them
# the Role)
if not self.has_role('Owner', name):
self.add_role(self.username, 'Owner', name)
self._add_invalidation(None)
# extract the Trove classifiers
classifiers = info.get('classifiers', [])
classifiers.sort()
# now see if we're inserting or updating a release
message = None
relationships = defaultdict(set)
old_cifiers = []
html = readme_renderer.rst.render(info.get('description', ''))
if self.has_release(name, version):
# figure the changes
existing = self.get_package(name, version)
# handle the special vars that most likely won't have been
# submitted
for k in ('_pypi_ordering', '_pypi_hidden', 'bugtrack_url'):
if not info.has_key(k):
info[k] = existing[k]
# figure which cols in the table to update, if any
specials = 'name version'.split()
old = []
cols = []
vals = []
for k, v in existing.items():
if not info.has_key(k):
continue
if k not in specials and info.get(k, None) != v:
old.append(k)
cols.append(k)
vals.append(info[k])
vals.extend([name, version])
# pull out the bugtrack_url and put it in the packages table
# instead
if 'bugtrack_url' in cols:
sql = 'update packages set bugtrack_url=%s where name=%s'
safe_execute(cursor, sql, (info['bugtrack_url'], name))
del vals[cols.index('bugtrack_url')]
cols.remove('bugtrack_url')
# get old classifiers list
old_cifiers = self.get_release_classifiers(name, version)
old_cifiers.sort()
if info.has_key('classifiers') and old_cifiers != classifiers:
old.append('classifiers')
# get old classifiers list
for kind, specifier in self.get_release_dependencies(name, version):
relationships[kind].add(specifier)
for nkind, skind in dependency.by_val.items():
# numerical kinds in relationships; string kinds in info
try:
new_val = set(info[skind])
except KeyError:
# value not provided
continue
if relationships[skind] != new_val:
old.append(skind)
# no update when nothing changes
if not old:
return None
# create the journal/user message
message = 'update %s'%', '.join(old)
# update
if cols:
cols = ','.join(['%s=%%s'%x for x in cols])
safe_execute(cursor, '''update releases set %s where name=%%s
and version=%%s'''%cols, vals)
# journal the update
self.add_journal_entry(name, version, message, date,
self.username, self.userip)
else:
# round off the information (make sure name and version are in
# the info dict)
info['name'] = name
info['version'] = version
# figure the ordering
info['_pypi_ordering'] = self.fix_ordering(name, version)
# perform the insert
cols = ('name version author author_email maintainer '
'maintainer_email home_page license summary description '
'keywords platform requires_python '
'download_url _pypi_ordering _pypi_hidden').split()
args = tuple([info.get(k, None) for k in cols])
params = ','.join(['%s']*len(cols))
scols = ','.join(cols)
sql = 'insert into releases (%s) values (%s)'%(scols, params)
safe_execute(cursor, sql, args)
# journal entry
self.add_journal_entry(name, version, "new release", date,
self.username, self.userip)
# hide all other releases of this package if thus configured
if self.get_package_autohide(name):
safe_execute(cursor, 'update releases set _pypi_hidden=%s where '
'name=%s and version <> %s', (self.true, name, version))
# add description urls
if html:
# grab the packages hosting_mode
hosting_mode = self.get_package_hosting_mode(name)
if hosting_mode in ["pypi-scrape-crawl", "pypi-scrape"]:
self.update_description_urls(name, version, get_description_urls(html))
# handle trove information
if info.has_key('classifiers') and old_cifiers != classifiers:
safe_execute(cursor, 'delete from release_classifiers where name=%s'
' and version=%s', (name, version))
for classifier in classifiers:
safe_execute(cursor, 'select id from trove_classifiers where'
' classifier=%s', (classifier, ))
trove_id = cursor.fetchone()[0]
safe_execute(cursor, 'insert into release_classifiers '
'(name, version, trove_id) values (%s, %s, %s)',
(name, version, trove_id))
# handle relationship specifiers
for nkind, skind in dependency.by_val.items():
if not info.has_key(skind) or relationships[nkind] == set(info[skind]):
continue
safe_execute(cursor, '''delete from release_dependencies where name=%s
and version=%s and kind=%s''', (name, version, nkind))
for specifier in info[skind]:
safe_execute(cursor, '''insert into release_dependencies (name, version,
kind, specifier) values (%s, %s, %s, %s)''', (name,
version, nkind, specifier))
self._add_invalidation(name)
return message
def fix_ordering(self, name, new_version=None):
''' Fix the _pypi_ordering column for a package's releases.
If "new_version" is supplied, insert it into the sequence and
return the ordering value for it.
'''
cursor = self.get_cursor()
# load up all the version strings for this package and sort them
safe_execute(cursor,
'select version,_pypi_ordering from releases where name=%s',
(name,))
all_versions = list(cursor.fetchall())
if new_version is not None:
all_versions.append((new_version, None))
sorted_versions = sorted(
all_versions,
key=lambda x: packaging.version.parse(x[0]),
)
new_order = 0
for order, (ver, current) in enumerate(sorted_versions):
if current != order:
safe_execute(
cursor,
"""
UPDATE releases SET _pypi_ordering = %s
WHERE name = %s AND version = %s
""",
(order, name, ver),
)
if ver == new_version:
new_order = order
self._add_invalidation(name)
# return the ordering for this release
return new_order
def has_package(self, name):
''' Determine whether the package exists in the database.
Returns true/false.
'''
cursor = self.get_cursor()
sql = 'select count(*) from packages where name=%s'
safe_execute(cursor, sql, (name, ))
return int(cursor.fetchone()[0])
def find_package(self, name):
'''Return names of packages that differ from name only in case.'''
cursor = self.get_cursor()
sql = 'select name from packages where normalize_pep426_name(name)=normalize_pep426_name(%s)'
safe_execute(cursor, sql, (name, ))
return [r[0] for r in cursor.fetchall()]
def has_release(self, name, version):
''' Determine whether the release exists in the database.
Returns true/false.
'''
cursor = self.get_cursor()
sql = 'select count(*) from releases where name=%s and version=%s'
safe_execute(cursor, sql, (name, version))
return int(cursor.fetchone()[0])
def get_cheesecake_index(self, index_id):
index = {'absolute': -1,
'relative': -1,
'subindices': []}
cursor = self.get_cursor()
sql = 'select absolute, relative from cheesecake_main_indices where id = %d'
safe_execute(cursor, sql, (index_id,))
index['absolute'], index['relative'] = cursor.fetchone()
sql = 'select name, value, details from cheesecake_subindices where main_index_id = %d'
safe_execute(cursor, sql, (index_id,))
for name, value, details in cursor.fetchall():
index['subindices'].append(dict(name=name, value=value, details=details))
index['subindices'].sort(lambda x,y: cmp(x['name'], y['name']))
return index
_Package = FastResultRow('''name version author author_email
maintainer maintainer_email home_page license summary description
keywords platform requires_python download_url
_pypi_ordering! _pypi_hidden! cheesecake_installability_id!
cheesecake_documentation_id! cheesecake_code_kwalitee_id! bugtrack_url!''')
def get_package(self, name, version):
''' Retrieve info about the package from the database.
Returns a mapping with the package info.
'''
cursor = self.get_cursor()
sql = '''select packages.name as name, version, author,
author_email, maintainer, maintainer_email, home_page,
license, summary, description, keywords,
platform, requires_python, download_url, _pypi_ordering,
_pypi_hidden,
cheesecake_installability_id,
cheesecake_documentation_id,
cheesecake_code_kwalitee_id, bugtrack_url
from packages, releases
where packages.name=%s and version=%s
and packages.name = releases.name'''
safe_execute(cursor, sql, (name, version))
return self._Package(None, cursor.fetchone())
def get_package_urls(self, name, relative=None):
'''Return all URLS (home, download, files) for a package,
Return list of (link, rel, label) or None if there are no releases.
'''
cursor = self.get_cursor()
file_urls = []
# uploaded files
safe_execute(cursor, '''select filename, python_version, md5_digest
from release_files where name=%s''', (name,))
for fname, pyversion, md5 in cursor.fetchall():
# Put files first, to have setuptools consider
# them before going to other sites
url = self.gen_file_url(pyversion, name, fname, relative) + \
"#md5=" + md5
file_urls.append((url, "internal", fname))
return sorted(file_urls)
def get_uploaded_file_urls(self, name):
cursor = self.get_cursor()
urls = []
safe_execute(cursor, '''select filename, python_version
from release_files where name=%s''', (name,))
for fname, pyversion in cursor.fetchall():
urls.append(self.gen_file_url(pyversion, name, fname))
return urls
_Description_URLs = FastResultRow('id! version url')
def list_description_urls(self, name, version=None):
if version is None:
sql = "SELECT id, version, url FROM description_urls WHERE name=%s"
params = [name]
else:
sql = """SELECT id, version, url FROM description_urls
WHERE name=%s AND version=%s"""
params = [name, version]
cursor = self.get_cursor()
safe_execute(cursor, sql, params)
return Result(None, cursor.fetchall(), self._Description_URLs)
def add_description_url(self, name, version, url):
cursor = self.get_cursor()
safe_execute(cursor, """INSERT INTO description_urls (name, version, url)
VALUES (%s, %s, %s)""", [name, version, url])
date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
self.add_journal_entry(name, version, "add url " + url, date,
self.username, self.userip)
def remove_description_url(self, url_id):
cursor = self.get_cursor()
sql = "SELECT name, version, url FROM description_urls WHERE id=%s"
safe_execute(cursor, sql, [url_id])
results = cursor.fetchone()
if results is None:
return
name, version, url = results
sql = "DELETE FROM description_urls WHERE id=%s"
safe_execute(cursor, sql, [url_id])
date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
self.add_journal_entry(name, version, "remove url " + url, date,
self.username, self.userip)
def get_stable_version(self, name):
''' Retrieve the version marked as a package:s stable version.
'''
cursor = self.get_cursor()
sql = 'select stable_version from packages where name=%s'
safe_execute(cursor, sql, (name, ))
return cursor.fetchone()[0]
def top_packages(self, num=None):
cursor = self.get_cursor()
sql = """SELECT name, SUM(downloads) AS downloads FROM release_files
GROUP BY name ORDER BY downloads DESC"""
if num is not None:
sql += " LIMIT %s"
safe_execute(cursor, sql, (num,))
else:
safe_execute(cursor, sql)
return [(p[0], p[1]) for p in cursor.fetchall()]
_Packages = FastResultRow('name')
def get_packages(self):
''' Fetch the complete list of packages from the database.
'''
cursor = self.get_cursor()
safe_execute(cursor, 'select name from packages order by name')
return Result(None, cursor.fetchall(), self._Packages)
def get_packages_with_serial(self):
cursor = self.get_cursor()
safe_execute(cursor, "SELECT name, last_serial FROM packages")
return dict((n,i) for n, i in cursor.fetchall())
def get_packages_utf8(self):
'''Fetch the complete list of package names, UTF-8 encoded
'''
cursor = self.get_cursor()
cursor.execute('select name from packages order by name')
return (p[0] for p in cursor.fetchall())
_Journal = FastResultRow('action submitted_date! submitted_by submitted_from id!')
def get_journal(self, name, version):
''' Retrieve info about the package from the database.
Returns a list of the journal entries, giving those entries
specific to the nominated version and those entries not specific
to any version.
'''
cursor = self.get_cursor()
# get the generic stuff or the stuff specific to the version
sql = '''select action, submitted_date, submitted_by,
submitted_from, id from journals where name=%s and (version=%s or
version is NULL) order by submitted_date'''
safe_execute(cursor, sql, (name, version))
return Result(None, cursor.fetchall(), self._Journal)
def count_packages(self):
''' Determine the number of packages registered with the index.
'''
cursor = self.get_cursor()
cursor.execute('select count(*) from packages')
return int(cursor.fetchone()[0])
_Query_Packages = FastResultRow('name version summary _pypi_ordering!')
def query_packages(self, spec, operator='and'):
''' Find packages that match the spec.
Return a list of (name, version) tuples.
'''
if operator not in ('and', 'or'):
operator = 'and'
where = []
for k, v in spec.items():
if k not in ['name', 'version', 'author', 'author_email',
'maintainer', 'maintainer_email',
'home_page', 'license', 'summary',
'description', 'keywords', 'platform',
'download_url']:
continue
if type(v) != type([]): v = [v]
# Quote the bits in the string that need it and then embed
# in a "substring" search. Note - need to quote the '%' so
# they make it through the python layer happily
v = ['%%'+s.lower().replace("'", "''")+'%%' for s in v]
# now add to the where clause
where.append('(' + ' or '.join(["lower(%s) LIKE '%s'"%(k,
s.encode('utf-8')) for s in v]) + ')')
if where:
where = ' %s '%operator.join(where)
if '_pypi_hidden' in spec:
if spec['_pypi_hidden'] in ('1', 1): v = self.true
else: v = self.false
if where:
where += ' AND _pypi_hidden = %s'%v
else:
where = '_pypi_hidden = %s'%v
# construct the SQL
if where:
where = ' where ' + where
else:
where = ''
# do the fetch
cursor = self.get_cursor()
sql = '''select name, version, summary, _pypi_ordering
from releases %s
order by lower(name), _pypi_ordering'''%where
safe_execute(cursor, sql)
return Result(None, cursor.fetchall(), self._Query_Packages)
def search_packages(self, spec, operator='and'):
''' Search for packages that match the spec.
Return a list of (name, version, summary, _pypi_ordering) tuples.
'''
if self.config.database_releases_index_url is None or self.config.database_releases_index_name is None:
return self.query_packages(spec, operator=operator)
if operator not in ('and', 'or'):
operator = 'and'
hidden = False
if '_pypi_hidden' in spec:
if spec['_pypi_hidden'] in ('1', 1):
hidden = True
terms = []
for k, v in spec.items():
if k not in ['name', 'version', 'author', 'author_email',
'maintainer', 'maintainer_email',
'home_page', 'license', 'summary',
'description', 'keywords', 'platform',
'download_url']:
continue
if type(v) != type([]): v = [v]
if k == 'name':
terms.extend(["(name_exact:%s OR name:*%s*)" % (s.encode('utf-8').lower(), s.encode('utf-8')) for s in v])
else:
terms.extend(["%s:*%s*" % (k, s.encode('utf-8')) for s in v])
join_string = ' %s '%(operator.upper())
query_params = {
'q': join_string.join(terms),
'fields': 'name,summary,version,_pypi_ordering,_pypi_hidden',
'sort': 'name,_pypi_ordering',
'size': '10000',
'type': 'phrase'
}
query_string = urllib.urlencode(query_params)
index_url = "/".join([self.config.database_releases_index_url, self.config.database_releases_index_name])
r = requests.get(index_url + '/release/_search?' + query_string)
data = r.json()
results = [_format_es_fields(r) for r in data['hits']['hits'] if r['fields'].get('_pypi_hidden', [False])[0] == hidden]
return Result(None, results, self._Query_Packages)
_Classifiers = FastResultRow('classifier')
def get_classifiers(self):
''' Fetch the list of valid classifiers from the database.
'''
cursor = self.get_cursor()
safe_execute(cursor, 'select classifier from trove_classifiers'
' order by classifier')
return Result(None, cursor.fetchall(), self._Classifiers)
_ClassifierID = FastResultRow('classifier id')
def get_classifier_ids(self, classifiers):
'''Map list of classifiers to classifier IDs'''
cursor = self.get_cursor()
placeholders = ','.join(['%s'] * len(classifiers))
safe_execute(cursor, 'select classifier, id from trove_classifiers '
'where classifier in (%s)' % placeholders, classifiers)
return dict(cursor.fetchall())
_Release_Classifiers = FastResultRow('classifier trove_id!')
def get_release_classifiers(self, name, version):
''' Fetch the list of classifiers for the release.
'''
cursor = self.get_cursor()
safe_execute(cursor, '''select classifier, trove_id
from trove_classifiers, release_classifiers where id=trove_id
and name=%s and version=%s order by classifier''', (name, version))
return Result(None, cursor.fetchall(), self._Release_Classifiers)
_Release_Relationships = FastResultRow('specifier')
def get_release_relationships(self, name, version, relationship):
''' Fetch the list of relationships of a particular type, either
"requires", "provides" or "obsoletes".
'''
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies where
name=%s and version=%s and kind=%s''', (name, version,
getattr(dependency, relationship)))
return Result(None, cursor.fetchall(), self._Release_Relationships)
_Release_Dependencies = FastResultRow('kind! specifier')
def get_release_dependencies(self, name, version):
'''Fetch all release dependencies of a release.'''
cursor = self.get_cursor()
safe_execute(cursor, '''select kind, specifier from release_dependencies
where name=%s and version=%s''', (name, version))
return Result(None, cursor.fetchall(), self._Release_Dependencies)
def get_release_downloads(self, name, version):
'''Fetch current download count for a release.'''
cursor = self.get_cursor()
safe_execute(cursor, '''select filename, downloads from release_files where
name=%s and version=%s''', (name, version))
return cursor.fetchall()
_User_Packages_Roles = FastResultRow('role_name package_name')
def get_user_packages(self, name):
'''Fetch all packages and roles associated to user.'''
cursor = self.get_cursor()
safe_execute(cursor, '''select role_name, package_name from roles where