Repository navigation
Expand file tree
/
Copy pathdatasource.py
More file actions
610 lines (534 loc) · 20 KB
/
Copy pathdatasource.py
File metadata and controls
610 lines (534 loc) · 20 KB
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
# pkgs.void - web catalog of Void Linux packages.
# Copyright (C) 2019-2021 Piotr Wójcik <chocimier@tlen.pl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import abc
import datetime
import hashlib
import sqlite3
from collections import namedtuple
import ujson as json
from settings import config
import sink
from custom_types import Interest
from metadata import MetapackageInterest
from xbps import pkgname_from_pkgver
_PackageRow = namedtuple(
'PackageRow',
(
'pkgname',
'pkgver',
'arch',
'restricted',
'builddate',
'repodata',
'templatedata',
'mainpkg',
'depends_count',
'upstreamver',
'repo',
'popularity',
)
)
class PackageRow(_PackageRow):
def __new__(
cls,
*,
pkgver,
arch,
repo,
pkgname=None,
restricted=False,
builddate='',
repodata=None,
templatedata=None,
mainpkg='',
depends_count=None,
upstreamver='',
popularity=0,
):
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=unused-argument
def to_jsondata(arg):
if isinstance(arg, str):
return arg
if arg is None:
return to_json({})
return to_json(arg)
if not pkgname:
pkgname = pkgname_from_pkgver(pkgver)
repodata = to_jsondata(repodata)
templatedata = to_jsondata(templatedata)
function_locals = locals()
fields = {f: function_locals[f] for f in _PackageRow._fields}
return super().__new__(cls, **fields)
@staticmethod
def from_record(record):
return PackageRow(**dict(zip(_PackageRow._fields, record)))
def to_json(dictionary):
return json.dumps(dictionary, sort_keys=True)
def from_json(text):
return json.loads(text)
def datetime_to_string(datetime_obj):
return datetime_obj.isoformat()
def datetime_from_string(string):
return datetime.datetime.fromisoformat(string)
def _date_as_string(date):
return date.strftime('%Y-%m-%d')
def dailyable(package_row):
return (
not package_row.repo.startswith('multilib')
and not package_row.pkgname.endswith('-devel')
and not package_row.pkgname.endswith('-dbg')
)
def daily_hash(pkgname, date, bits=None):
'''We want to select small (around 20) subset of packages
with following properties:
- changing daily
- not partitioning: every pair of packages has chance to be chosen some day
- precomputable: not forcing to process every package to select
- adding or removing package do not affect choise of other packages
- size may vary
To achieve this, we compute hash of pair of pkgname and date.
Then package is chosen when such hash has common prefix with hash of date.
Lenght of prefix in bits is adjusted such that set has required size.
'''
string = pkgname + _date_as_string(date)
hash_value = hashlib.md5(string.encode()).digest()
integer = int.from_bytes(hash_value, 'big')
binary = "{num:0{width}b}".format(num=integer, width=8*len(hash_value))
return binary[:bits]
class Datasource(metaclass=abc.ABCMeta):
@abc.abstractmethod
def __enter__(self):
'''Enters runtime context.'''
@abc.abstractmethod
def __exit__(self, exc_type, exc_val, exc_tb):
'''Exits runtime context.'''
@abc.abstractmethod
def create(self, package_row, dates):
'''Saves information about package into database.
Computes if it is daily package, and register if so.'''
@abc.abstractmethod
def read(self, **kwargs):
'''Finds packages that match criteria passed as keyword arguments.'''
@abc.abstractmethod
def exists(self, **kwargs):
'''Finds whether packages that match criteria
passed as keyword arguments exists.'''
@abc.abstractmethod
def same_template(self, pkgname):
'''Returns names of packages built from same template.'''
@abc.abstractmethod
def list_all(self):
'''Returns list of all packages.'''
@staticmethod
@abc.abstractmethod
def search_fields():
'''Returns names of searchable fields'''
@abc.abstractmethod
def search(self, term, fields):
'''Searches for packages described by term.
Returns iterator of dictionaries with keys 'name', 'description'.'''
@abc.abstractmethod
def update(self, **kwargs):
'''Finds packages matching criteria passed as keyword arguments
and sets values passed as keyword arguments prefixed with 'set_'.'''
@abc.abstractmethod
def of_day(self, date):
'''Returns different packages every day.'''
@abc.abstractmethod
def metapackages(self, allowed=None):
'''Returns names of packages that solely depend on other packages,
optionally limited to _allowed_ statuses.'''
@abc.abstractmethod
def newest(self, count):
'''Finds names of _count_ most recently build packages'''
@abc.abstractmethod
def popular(self, at_most):
'''Finds names of packages being more popular than
at_most-th most popular package'''
@abc.abstractmethod
def longest_names(self, at_most):
'''Finds names of packages having name longer than
at_most-th longest-name-bearing package'''
@abc.abstractmethod
def add_auxiliary(self, key, value):
'''Sets auxiliary values of _key_.'''
@abc.abstractmethod
def auxiliary(self, key):
'''Returns auxiliary values of _key_.'''
@abc.abstractmethod
def finish_creating(self):
'''Creates indices after data is stored'''
class SqliteDataSource(Datasource):
def __init__(self, path, mode):
'''Opens datasource stored as sqlite database.
path: path of database file
mode: 'read' or 'write'
'''
self._db = sqlite3.connect(path)
self._cursor = self._db.cursor()
if mode == 'write':
self._initialize()
self.metadata_interest = MetapackageInterest()
def _initialize(self):
self._cursor.execute('''create table if not exists packages (
arch text not null,
pkgname text not null,
pkgver text not null,
restricted integer not null,
builddate text not null,
repodata text not null,
templatedata text not null,
mainpkg text not null,
depends_count integer,
upstreamver text not null,
repo text not null,
popularity integer default 0)
''')
self._cursor.execute('''create table if not exists daily_hash (
pkgname text not null,
date text not null,
unique(pkgname, date)
)
''')
self._cursor.execute('''create table if not exists pkgnames (
pkgname text primary key,
same_template text not null
)
''')
self._cursor.execute('''create table if not exists metapackages (
pkgname text unique not null,
classification text not null
)
''')
time = datetime_to_string(sink.now().replace(microsecond=0))
self._cursor.execute('''create table if not exists auxiliary
as select ? as key, ? as value
''', ['update_time', time])
self._cursor.execute('''create virtual table if not exists
search_terms using fts4(
{},
tokenize=porter
)
'''.format(', '.join(self.search_fields())))
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self._db.commit()
self._db.close()
def create(self, package_row, dates):
'''Saves information about package into database.
Computes if it is daily package, and register if so.'''
query = 'INSERT INTO packages ({}) VALUES ({})'.format(
', '.join(PackageRow._fields),
', '.join('?' * len(PackageRow._fields))
)
self._cursor.execute(query, package_row)
self._add_search_terms(package_row)
self._add_daily_hashes(package_row, dates)
self._register_metapackage(package_row)
def _add_search_terms(self, package_row):
for data in (package_row.repodata, package_row.templatedata):
self._add_search_terms_from_data(package_row.pkgname, data)
def _add_search_terms_from_data(self, pkgname, data):
data = from_json(data)
if not data:
return
query = 'INSERT INTO search_terms ({}) VALUES ({})'.format(
', '.join(self.search_fields()),
', '.join('?' * len(self.search_fields()))
)
self._cursor.execute(query, (
pkgname,
data.get('short_desc'),
data.get('homepage'),
))
def _add_daily_hashes(self, package_row, dates):
if not dailyable(package_row):
return
hash_query = '''INSERT OR IGNORE INTO daily_hash (pkgname, date)
VALUES (?, ?)'''
for date in dates:
hash_value = daily_hash(
package_row.pkgname,
date,
config.DAILY_HASH_BITS
)
if daily_hash('', date).startswith(hash_value):
self._cursor.execute(
hash_query,
(package_row.pkgname, _date_as_string(date))
)
def _register_metapackage(self, package_row):
pkgname = package_row.pkgname
repodata = package_row.repodata
if (package_row.depends_count
and package_row.depends_count > 1
and not pkgname.endswith('-32bit')
and from_json(repodata).get('installed_size') == 0):
query = '''INSERT OR IGNORE INTO metapackages
(pkgname, classification)
VALUES (?, ?)'''
self._cursor.execute(
query,
[pkgname, self.metadata_interest.get(pkgname).value]
)
def read(self, **kwargs):
'''Finds packages that match criteria passed as keyword arguments.'''
fixed = [i for i in kwargs if i in PackageRow._fields]
query = 'SELECT {} FROM packages WHERE {}'.format(
', '.join(PackageRow._fields),
' AND '.join(f'{i} = ?' for i in fixed)
)
self._cursor.execute(query, [kwargs[i] for i in fixed])
return (PackageRow.from_record(x) for x in self._cursor.fetchall())
def exists(self, **kwargs):
'''Finds whether packages that match criteria
passed as keyword arguments exists.'''
fixed = [i for i in kwargs if i in PackageRow._fields]
query = 'SELECT 1 FROM packages WHERE {} LIMIT 1'.format(
' AND '.join(f'{i} = ?' for i in fixed)
)
self._cursor.execute(query, [kwargs[i] for i in fixed])
return bool(self._cursor.fetchall())
def same_template(self, pkgname):
'''Returns names of packages built from same template.'''
query = ('SELECT pkgname '
'FROM pkgnames '
'WHERE same_template = ('
' SELECT same_template '
' FROM pkgnames '
' WHERE pkgname = ? '
') '
'ORDER BY pkgname')
self._cursor.execute(query, [pkgname])
return (x[0] for x in self._cursor.fetchall())
def list_all(self):
'''Returns list of all packages.'''
query = 'SELECT {} FROM packages group by pkgname'.format(
', '.join(PackageRow._fields),
)
self._cursor.execute(query)
return (PackageRow.from_record(x) for x in self._cursor.fetchall())
@staticmethod
def search_fields():
'''Returns names of searchable fields'''
return (
'name',
'description',
'homepage',
)
def search(self, term, fields):
'''Searches for packages described by term.
Returns iterator of dictionaries with keys 'name', 'description'.'''
effective_fields = (
set(self.search_fields()).intersection(fields)
or self.search_fields()
)
query = '''select distinct name, description from search_terms
where {}
order by name'''.format(
' or '.join(f'{f} match ?' for f in effective_fields)
)
try:
self._cursor.execute(query, [term]*len(effective_fields))
except sqlite3.OperationalError:
return 'Invalid search term'
keys = ('name', 'description')
return (dict(zip(keys, vals)) for vals in self._cursor.fetchall())
@staticmethod
def _sets(argname):
prefix = 'set_'
if argname.startswith(prefix):
return argname[len(prefix):]
return None
def update(self, **kwargs):
'''Finds packages matching criteria passed as keyword arguments
and sets values passed as keyword arguments prefixed with 'set_'.
NOTE: update of search_terms is done only for usage
existing at the time of writing, that is adding templatedata
'''
updated = [i for i in kwargs if self._sets(i) in PackageRow._fields]
fixed = [
i
for i in kwargs
if not self._sets(i) and i in PackageRow._fields
]
query = 'UPDATE packages SET {} WHERE {}'.format(
', '.join('{} = ?'.format(self._sets(i)) for i in updated),
' AND '.join('{} = ?'.format(i) for i in fixed) or True
)
self._cursor.execute(query, [kwargs[i] for i in updated + fixed])
pkgname = kwargs.get('pkgname')
templatedata = kwargs.get('set_templatedata')
if pkgname and templatedata:
self._add_search_terms_from_data(pkgname, templatedata)
def of_day(self, date):
'''Returns different packages every day'''
query = (
'select pkgname from daily_hash '
+ 'where date = ?'
+ 'order by pkgname'
)
self._cursor.execute(query, [_date_as_string(date)])
return (x[0] for x in self._cursor.fetchall())
def metapackages(self, allowed=None):
'''Returns names of packages that solely depend on other packages,
optionally limited to _allowed_ statuses.'''
where = ''
if allowed:
where = 'where classification in ({}) '.format(
', '.join('?'*len(allowed))
)
query = (
'select pkgname, classification from metapackages '
+ where
+ 'order by pkgname '
)
self._cursor.execute(query, [i.value for i in (allowed or [])])
return (
{'pkgname': x[0], 'classification': Interest(x[1])}
for x in self._cursor.fetchall()
)
def newest(self, count):
'''Finds names of _count_ most recently build packages'''
query = (
'select distinct pkgname from packages '
'where repo not like "multilib%" '
'and pkgname not like "%-devel" '
'and pkgname not like "%-dbg" '
"and builddate != '' "
'order by builddate desc '
'limit ?'
)
self._cursor.execute(query, [int(count)])
return (x[0] for x in self._cursor.fetchall())
def popular(self, at_most):
'''Finds names of packages being more popular than
at_most-th most popular package'''
query = (
'select distinct pkgname from packages '
'where popularity > ('
' select popularity from ('
' select distinct pkgname, popularity '
' from packages '
' where popularity > 0 '
' order by popularity desc'
' limit 1 offset ?'
' )'
')'
'order by popularity desc'
)
self._cursor.execute(query, [int(at_most)])
return (x[0] for x in self._cursor.fetchall())
def longest_names(self, at_most):
'''Finds names of packages having name longer than
at_most-th longest-name-bearing package'''
query = (
'select distinct pkgname from packages '
'where repo not like "multilib%" '
'and pkgname not like "%-devel" '
'and pkgname not like "%-dbg" '
'and length(pkgname) > ('
' select length(pkgname) from ('
' select distinct pkgname '
' from packages '
' where repo not like "multilib%" '
' and pkgname not like "%-devel" '
' and pkgname not like "%-dbg" '
' )'
' order by length(pkgname) desc'
' limit 1 offset ?'
')'
'order by pkgname'
)
self._cursor.execute(query, [int(at_most)])
return (x[0] for x in self._cursor.fetchall())
def add_auxiliary(self, key, value):
'''Sets auxiliary values of _key_.'''
query = (
'INSERT INTO auxiliary (key, value)'
'VALUES (?, ?)'
)
self._cursor.execute(query, [key, value])
def auxiliary(self, key):
'''Returns auxiliary values of _key_.'''
query = (
'select value from auxiliary '
'where key = ? '
)
self._cursor.execute(query, [key])
return (x[0] for x in self._cursor.fetchall())
def finish_creating(self):
self._cursor.execute('''insert into
search_terms(search_terms)
values('optimize')
''')
self._cursor.execute('''insert or replace into
pkgnames(pkgname, same_template)
select first.pkgname, min(second.pkgname)
from packages as first
join packages as second
on first.mainpkg = second.mainpkg
group by first.pkgname
''')
self._cursor.execute('''create index if not exists pkgname_idx
on packages (
pkgname
)
''')
self._cursor.execute('''create index if not exists pkgver_idx
on packages (
pkgver
)
''')
self._cursor.execute('''create index if not exists builddate_idx
on packages (
builddate desc
)
''')
self._cursor.execute('''create index if not exists popularity_idx
on packages (
popularity
)
''')
self._cursor.execute('''create index if not exists daily_hash_idx
on daily_hash (
date
)
''')
self._cursor.execute('''create index if not exists group_idx
on pkgnames (
same_template
)
''')
self._cursor.execute('''analyze''')
def custom_factory(classname, *args, **kwargs):
return globals()[classname](*args, **kwargs)
def datasource_arguments(temporary):
if temporary:
return config.DATASOURCE_ARGUMENTS_TEMPORARY.split(',')
return config.DATASOURCE_ARGUMENTS.split(',')
def factory(temporary=False):
return custom_factory(
config.DATASOURCE_CLASS,
*datasource_arguments(temporary=temporary)
)
def update(func):
with factory(temporary=True) as source:
func(source)
source.finish_creating()