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
/
store.py
executable file
·2282 lines (1973 loc) · 81.8 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, time, hashlib, random
import string, datetime, binascii, urllib, urllib2, cgi
from collections import defaultdict
try:
import sqlite3
sqlite3_cursor = sqlite3.Cursor
except ImportError:
sqlite3_cursor = type(None)
from defusedxml import ElementTree
import trove
from mini_pkg_resources import safe_name
# csrf modules
import hmac
from base64 import b64encode
import openid.store.sqlstore
import requests
import itertools
import readme_renderer.rst
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
from perfmetrics import statsd_client_from_uri
from dogadapter import dogstatsd
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):
if not self.info:
return []
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.statsd_uri = "statsd://127.0.0.1:8125?prefix=%s" % (config.database_name)
self.statsd_reporter = statsd_client_from_uri(self.statsd_uri)
self.dogstatsd = dogstatsd
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):
# Report Zero, skip redis
download_counts = {
"last_day": 0,
"last_week": 0,
"last_month": 0,
}
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 and version do exist, we just edit (in place)
'''
date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
cursor = self.get_cursor()
# now see if we're inserting or updating a release
message = None
relationships = defaultdict(set)
old_cifiers = []
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 == '_pypi_hidden' and info.get(k, None) != v:
old.append(k)
cols.append(k)
vals.append(info[k])
vals.extend([name, version])
# no update when nothing changes
if not old:
return None
# 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)
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, version, requires_python, md5_digest, path
FROM release_files
WHERE release_files.name=%s
''', (name,))
files = sorted(cursor.fetchall(), key=lambda f: (normalize_version_number(f[1]), f[0]))
for fname, _, requires_python, md5, path in files:
# Put files first, to have setuptools consider
# them before going to other sites
url = self.gen_file_url('<not used arg>', name, fname, path=path, prefix=relative) + \
"#md5=" + md5
file_urls.append((url, "internal", fname, requires_python))
return file_urls
def get_uploaded_file_urls(self, name):
cursor = self.get_cursor()
urls = []
safe_execute(cursor, '''select filename, python_version, path
from release_files where name=%s''', (name,))
for fname, pyversion, path in cursor.fetchall():
urls.append(self.gen_file_url(pyversion, name, fname, path=path))
return urls
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')
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:*%s* OR name_exact:%s OR name_exact:%s OR name_exact:%s)" % (s.encode('utf-8'), safe_name(s).lower().encode('utf-8'), safe_name(s).lower().replace('-', '_'), safe_name(s).lower().replace('_', '-')) 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])
start_time = int(round(time.time() * 1000))
try:
r = requests.get(index_url + '/release/_search?' + query_string, timeout=0.5)
data = r.json()
except requests.exceptions.Timeout:
self.statsd_reporter.incr('store.search-packages.timeout')
self.dogstatsd.increment('store.search-packages.timeout')
data = {}
end_time = int(round(time.time() * 1000))
self.statsd_reporter.timing('store.search-packages', end_time - start_time)
self.dogstatsd.timing('store.search-packages', end_time - start_time)
results = []
if 'hits' in data.keys():
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
user_name=%s''', (name,))
return Result(None, cursor.fetchall(), self._User_Packages_Roles)
_Package_Roles = FastResultRow('role_name user_name')
def get_package_roles(self, name):
''' Fetch the list of Roles for the package.
'''
cursor = self.get_cursor()
safe_execute(cursor, '''select role_name, user_name
from roles where package_name=%s''', (name, ))
return Result(None, cursor.fetchall(), self._Package_Roles)
def get_package_autohide(self, name):
cursor = self.get_cursor()
safe_execute(cursor, 'select autohide from packages where name=%s',
[name])
return cursor.fetchall()[0][0]
def set_package_autohide(self, name, value):
cursor = self.get_cursor()
safe_execute(cursor, 'update packages set autohide=%s where name=%s',
[value, name])
def get_package_hosting_mode(self, name):
cursor = self.get_cursor()
safe_execute(cursor, 'select hosting_mode from packages where name=%s',
[name])
return cursor.fetchall()[0][0]
def _get_package_url(self, name):
name = name.split()[0]
cursor = self.get_cursor()
sql = 'select * from packages where name=%s'
safe_execute(cursor, sql, (name, ))
exists = cursor.fetchone() is not None
if not exists:
return None
return self.config.url + '/' + name
def get_package_requires_dist(self, name, version):
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies
where name=%s and version=%s and kind=%s''', (name, version,
dependency.requires_dist))
packages = []
for package in cursor.fetchall():
pack = {'name': package[0],
'href': self._get_package_url(package[0])}
packages.append(pack)
return packages
def get_package_provides_dist(self, name, version):
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies
where name=%s and version=%s and kind=%s''', (name, version,
dependency.provides_dist))
packages = []
for package in cursor.fetchall():
pack = {'name': package[0],
'href': self._get_package_url(package[0])}
packages.append(pack)
return packages
def get_package_obsoletes_dist(self, name, version):
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies
where name=%s and version=%s and kind=%s''', (name, version,
dependency.obsoletes_dist))
packages = []
for package in cursor.fetchall():
pack = {'name': package[0],
'href': self._get_package_url(package[0])}
packages.append(pack)
return packages
def get_package_requires_external(self, name, version):
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies
where name=%s and version=%s and kind=%s''', (name, version,
dependency.requires_external))
return [package[0] for package in cursor.fetchall()]
def get_package_project_url(self, name, version):
cursor = self.get_cursor()
safe_execute(cursor, '''select specifier from release_dependencies
where name=%s and version=%s and kind=%s''', (name, version,
dependency.project_url))
project_urls = []
for project in cursor.fetchall():
project_urls.append(project[0].split(','))
return project_urls
def get_unique(self, iterable):
''' Iterate over list of (name,version,date,summary) tuples
and return list of unique (taking name and version into
account) elements.
'''
d = {}
L = []
for name,version,date,summary in iterable:
k = (name, version)
if not d.has_key(k):
L.append((name,version,date,summary))
d[k] = 1
return L
_Updated_Releases = FastResultRow('name version submitted_date! summary')
def updated_releases(self, since):
'''Fetch all releases younger than "since" argument.
'''
assert isinstance(since, int)
cursor = self.get_cursor()
safe_execute(cursor, '''
select j.name,j.version,j.submitted_date,r.summary
from journals j, releases r
where j.version is not NULL
and j.action = 'new release'
and j.name = r.name and j.version = r.version
and r._pypi_hidden = '''+self.false+'''
and j.submitted_date > %s
order by submitted_date desc
''', (time.strftime('%Y-%m-%d %H:%M:%S +0000', time.gmtime(since)),))
return Result(None, self.get_unique(cursor.fetchall()),
self._Updated_Releases)
_Changelog = FastResultRow('name version submitted_date! action id!')
def changelog(self, since, full=False):
'''Fetch (name, version, submitted_date, action, id) since 'since'
argument.
'''
assert isinstance(since, int)
cursor = self.get_cursor()
query = '''
select name, version, submitted_date, action, id
from journals j
where j.submitted_date > %s
order by j.submitted_date
'''
if not full:
query += 'limit 50000'
params = (time.strftime('%Y-%m-%d %H:%M:%S +0000', time.gmtime(since)),)
safe_execute(cursor, query, params)
return Result(None, cursor.fetchall(), self._Changelog)
def changelog_since_serial(self, since, full=False):
'''Fetch (name, version, submitted_date, action, id) since 'since' id
argument.
'''
assert isinstance(since, int)
cursor = self.get_cursor()
query = '''
select name, version, submitted_date, action, id
from journals j where j.id > %s
order by j.id
'''
if not full:
query += 'limit 50000'
safe_execute(cursor, query, (since,))
return Result(None, cursor.fetchall(), self._Changelog)
def changelog_last_serial(self):
'''Fetch the last event's serial id.
'''
cursor = self.get_cursor()
safe_execute(cursor, 'select max(id) from journals j')
return cursor.fetchone()[0]
def last_serial_for_package(self, package):
cursor = self.get_cursor()
safe_execute(
cursor,
"SELECT last_serial FROM packages WHERE name = %s",
(package,),
)
row = cursor.fetchone()
if row:
return row[0]
def changed_packages(self, since):
"Fetch list of names of packages changed 'since'"
assert isinstance(since, int)
cursor = self.get_cursor()
safe_execute(cursor, 'select distinct(name) from journals where submitted_date > %s',
(time.strftime('%Y-%m-%d %H:%M:%S +0000', time.gmtime(since)),))
return [r[0] for r in cursor.fetchall()]
def changelog_last_hour(self):
return self.changelog(int(time.time())-3600, full=True)
_Latest_Packages = FastResultRow('name version submitted_date! summary')
def latest_packages(self, num=40):
'''Fetch "number" latest packages registered, youngest to oldest.
'''
cursor = self.get_cursor()
statement = '''SELECT
p.name, r.version, p.created as submitted_date, r.summary
FROM releases r, (
SELECT packages.name, max_order, packages.created
FROM packages
JOIN (
SELECT name, max(_pypi_ordering) AS max_order
FROM releases
GROUP BY name
) mo ON packages.name = mo.name
) p
WHERE p.name = r.name
AND p.max_order = r._pypi_ordering
ORDER BY p.created DESC
LIMIT %d
''' % num
safe_execute(cursor, statement)
result = Result(None, cursor.fetchall()[:num],