-
Notifications
You must be signed in to change notification settings - Fork 0
/
repoview.py
executable file
·958 lines (806 loc) · 32.6 KB
/
repoview.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
#!/usr/bin/python3
# -*- mode: Python; indent-tabs-mode: nil; -*-
"""
Repoview is a small utility to generate static HTML pages for a repodata
directory, to make it easily browseable.
@author: Konstantin Ryabitsev & contributors
@copyright: 2005 by Duke University, 2006-2007 by Konstantin Ryabitsev & co
@license: GPL
"""
##
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Copyright (C) 2005 by Duke University, http://www.duke.edu/
# Copyright (C) 2006 by McGill University, http://www.mcgill.ca/
# Copyright (C) 2007 by Konstantin Ryabitsev and contributors
# Author: Konstantin Ryabitsev <[email protected]>
#
#pylint: disable-msg=F0401,W0704
import os
import shutil
import sys
import time
from functools import cmp_to_key
import hashlib as md5
from argparse import ArgumentParser
import jinja2
from rpm import labelCompare
from xml.etree.ElementTree import fromstring, ElementTree, TreeBuilder
import sqlite3 as sqlite
##
# Some hardcoded constants
#
PKGKID = 'package.j2'
PKGFILE = '%s.html'
GRPKID = 'group.j2'
GRPFILE = '%s.group.html'
IDXKID = 'index.j2'
IDXFILE = 'index.html'
RSSKID = 'rss.j2'
RSSFILE = 'latest-feed.xml'
ISOFORMAT = '%a, %d %b %Y %H:%M:%S %z'
VERSION = '0.7.1'
SUPPORTED_DB_VERSION = 10
DEFAULT_TEMPLATEDIR = '/usr/share/repoview/templates'
def _mkid(text):
"""
Make a web-friendly filename out of group names and package names.
@param text: the text to clean up
@type text: str
@return: a web-friendly filename
@rtype: str
"""
text = text.replace('/', '.')
text = text.replace(' ', '_')
return text
def _humansize(bytez):
"""
This will return the size in sane units (KiB or MiB).
@param bytes: number of bytes
@type bytes: int
@return: human-readable string
@rtype: str
"""
if bytez < 1024:
return '%d Bytes' % bytez
bytez = int(bytez)
kbytes = bytez/1024
if kbytes/1024 < 1:
return '%d KiB' % kbytes
return '%0.1f MiB' % (float(kbytes)/1024)
class Repoview:
"""
The working horse class.
"""
def __del__(self):
for entry in self.cleanup:
if os.access(entry, os.W_OK):
os.unlink(entry)
def __init__(self, opts):
"""
@param opts: ArgumentParser's opts
@type opts: ArgumentParser
"""
# list of files to remove at the end of processing
self.cleanup = []
self.opts = opts
if opts.outdir:
self.outdir = opts.outdir
else:
self.outdir = os.path.join(opts.repodir, 'repoview')
self.exclude = '1=1'
self.state_data = {} #?
self.written = {} #?
self.groups = []
self.letter_groups = []
self.pconn = None # primary.sqlite
self.oconn = None # other.sqlite
self.sconn = None # state db
self.fconn = None # filelists.sqlite
self.setup_repo()
self.setup_outdir()
self.setup_state_db()
self.setup_excludes()
if not self.groups:
self.setup_rpm_groups()
letters = self.setup_letter_groups()
repo_data = {
'title': opts.title,
'letters': letters,
'my_version': VERSION,
'env': {}
}
try:
assert opts.env is not None
repo_data['env'] = {
e.split('=', 1)[0]: e.split('=', 1)[1] for e in opts.env
}
except AssertionError:
pass
except IndexError:
sys.stderr.write('invalid environment arguments. Exiting.\n')
sys.exit(1)
def ymd(stamp):
return time.strftime('%Y-%m-%d', time.localtime(int(stamp)))
self.j2loader = jinja2.FileSystemLoader(opts.templatedir)
self.j2env = jinja2.Environment(autoescape=True, trim_blocks=True, loader=self.j2loader)
self.j2env.filters['ymd'] = ymd
self.group_kid = self.j2env.get_template(GRPKID)
self.pkg_kid = self.j2env.get_template(PKGKID)
count = 0
for group_data in self.groups + self.letter_groups:
(grp_name, grp_filename, grp_description, pkgnames) = group_data
group_data = {
'name': grp_name,
'description': grp_description,
'filename': grp_filename,
}
packages = self.do_packages(repo_data, group_data, sorted(pkgnames))
if not packages:
# Empty groups are ignored
del self.groups[count]
continue
count += 1
group_data['packages'] = packages
checksum = self.mk_checksum(repo_data, group_data)
if self.has_changed(grp_filename, checksum):
# write group file
self.say('Writing group %s\n' % grp_filename)
outfile = os.path.join(self.outdir, grp_filename)
with open(outfile, "w") as fh:
fh.write(self.group_kid.render(
repo_data=repo_data, group_data=group_data
))
latest = self.get_latest_packages()
repo_data['latest'] = latest
repo_data['groups'] = self.groups
checksum = self.mk_checksum(repo_data)
if self.has_changed('index.html', checksum):
# Write index.html and rss feed (if asked)
self.say('Writing index.html...')
idx_kid = self.j2env.get_template(IDXKID)
outfile = os.path.join(self.outdir, 'index.html')
with open(outfile, "w") as fh:
fh.write(idx_kid.render(
repo_data=repo_data, url=self.opts.url, latest=latest,
groups=self.groups,
time=time.strftime('%Y-%m-%d')
))
self.say('done\n')
# rss feed
if self.opts.url:
self.do_rss(repo_data, latest)
self.remove_stale()
self.sconn.commit()
def setup_state_db(self):
"""
Sets up the state-tracking database.
@rtype: void
"""
self.say('Examining state db...')
if self.opts.statedir:
# we'll use the md5sum of the repo location to make it unique
unique = '%s.state.sqlite' % md5.md5(self.outdir).hexdigest()
statedb = os.path.join(self.opts.statedir, unique)
else:
statedb = os.path.join(self.outdir, 'state.sqlite')
if os.access(statedb, os.W_OK):
if self.opts.force:
# clean slate -- remove state db and start over
os.unlink(statedb)
else:
# state_db not found, go into force mode
self.opts.force = True
self.sconn = sqlite.connect(statedb)
scursor = self.sconn.cursor()
query = """CREATE TABLE IF NOT EXISTS state (
filename TEXT UNIQUE,
checksum TEXT)"""
scursor.execute(query)
# read all state data into memory to track orphaned files
query = """SELECT filename, checksum FROM state"""
scursor.execute(query)
while True:
row = scursor.fetchone()
if row is None:
break
self.state_data[row[0]] = row[1]
self.say('done\n')
def setup_repo(self):
"""
Examines the repository, makes sure that it's valid and supported,
and then opens the necessary databases.
@rtype: void
"""
self.say('Examining repository...')
repomd = os.path.join(self.opts.repodir, 'repodata', 'repomd.xml')
if not os.access(repomd, os.R_OK):
sys.stderr.write('Not found: %s\n' % repomd)
sys.stderr.write('Does not look like a repository. Exiting.\n')
sys.exit(1)
repoxml = open(repomd).read()
xml = fromstring(repoxml) #IGNORE:E1101
# look for primary_db, other_db, and optionally group
primary = other = comps = filelists = dbversion = None
xmlns = 'http://linux.duke.edu/metadata/repo'
for datanode in xml.findall('{%s}data' % xmlns):
href = datanode.find('{%s}location' % xmlns).attrib['href']
if datanode.attrib['type'] == 'primary_db':
primary = os.path.join(self.opts.repodir, href)
dbversion = datanode.find('{%s}database_version' % xmlns).text
elif datanode.attrib['type'] == 'other_db':
other = os.path.join(self.opts.repodir, href)
elif datanode.attrib['type'] == 'group':
comps = os.path.join(self.opts.repodir, href)
elif datanode.attrib['type'] == 'filelists_db':
filelists = os.path.join(self.opts.repodir, href)
if primary is None or dbversion is None:
self.say('Sorry, sqlite files not found in the repository.\n'
'Please rerun createrepo with a -d flag and try again.\n')
sys.exit(1)
if int(dbversion) > SUPPORTED_DB_VERSION:
self.say('Sorry, the db_version in the repository is %s, but '
'repoview only supports versions up to %s. Please check '
'for a newer repoview version.\n' % (dbversion,
SUPPORTED_DB_VERSION))
sys.exit(1)
self.say('done\n')
self.say('Opening primary database...')
primary = self.z_handler(primary)
self.pconn = sqlite.connect(primary)
self.say('done\n')
self.say('Opening changelogs database...')
other = self.z_handler(other)
self.oconn = sqlite.connect(other)
self.say('done\n')
self.say('Opening filelists database...')
filelists = self.z_handler(filelists)
self.fconn = sqlite.connect(filelists)
self.say('done\n')
if self.opts.comps:
comps = self.opts.comps
if comps:
self.setup_comps_groups(comps)
def say(self, text):
"""
Unless in quiet mode, output the text passed.
@param text: something to say
@type text: str
@rtype: void
"""
if not self.opts.quiet:
sys.stdout.write(text)
def setup_excludes(self):
"""
Formulates an SQL exclusion rule that we use throughout in order
to respect the ignores passed on the command line.
@rtype: void
"""
# Formulate exclusion rule
xarches = []
for xarch in self.opts.xarch:
xarch = xarch.replace("'", "''")
xarches.append("arch != '%s'" % xarch)
if xarches:
self.exclude += ' AND ' + ' AND '.join(xarches)
pkgs = []
for pkg in self.opts.ignore:
pkg = pkg.replace("'", "''")
pkg = pkg.replace("*", "%")
pkgs.append("name NOT LIKE '%s'" % pkg)
if pkgs:
self.exclude += ' AND ' + ' AND '.join(pkgs)
def setup_outdir(self):
"""
Sets up the output directory.
@rtype: void
"""
if self.opts.force and os.access(self.outdir, os.R_OK):
# clean slate -- remove everything
shutil.rmtree(self.outdir)
if not os.access(self.outdir, os.R_OK):
os.mkdir(self.outdir, 0o755)
layoutsrc = os.path.join(self.opts.templatedir, 'layout')
layoutdst = os.path.join(self.outdir, 'layout')
if os.path.isdir(layoutsrc) and not os.access(layoutdst, os.R_OK):
self.say('Copying layout...')
shutil.copytree(layoutsrc, layoutdst)
self.say('done\n')
def get_package_data(self, pkgname):
"""
Queries the packages and changelog databases and returns package data
in a dict:
pkg_data = {
'name': str,
'filename': str,
'summary': str,
'description': str,
'url': str,
'rpm_license': str,
'rpm_sourcerpm': str,
'vendor': str,
'rpms': []
}
the "rpms" key is a list of tuples with the following members:
(epoch, version, release, arch, time_build, size, location_href,
author, changelog, time_added)
@param pkgname: the name of the package to look up
@type pkgname: str
@return: a REALLY hairy dict of values
@rtype: list
"""
# fetch versions
query = """SELECT pkgKey,
epoch,
version,
release,
arch,
summary,
description,
url,
time_build,
rpm_license,
rpm_sourcerpm,
size_package,
location_href,
rpm_vendor
FROM packages
WHERE name='%s' AND %s
ORDER BY arch ASC""" % (pkgname, self.exclude)
pcursor = self.pconn.cursor()
pcursor.execute(query)
rows = pcursor.fetchall()
if not rows:
# Sorry, nothing found
return None
if len(rows) == 1:
# only one package matching this name
versions = [rows[0]]
else:
# we will use the latest package as the "master" to
# obtain things like summary, description, etc.
# go through all available packages and create a dict
# keyed by (e,v,r)
temp = {}
for row in rows:
temp[(row[1], row[2], row[3], row[4])] = row
keys = list(temp.keys())
keys.sort(
key=cmp_to_key(lambda a, b: labelCompare(a[:3], b[:3])),
reverse=True
)
versions = []
for key in keys:
versions.append(temp[key])
pkg_filename = _mkid(PKGFILE % pkgname)
pkg_data = {
'name': pkgname,
'filename': pkg_filename,
'summary': None,
'description': None,
'url': None,
'rpm_license': None,
'rpm_sourcerpm': None,
'vendor': None,
'rpms': []
}
for row in versions:
(pkg_key, epoch, version, release, arch, summary,
description, url, time_build, rpm_license, rpm_sourcerpm,
size_package, location_href, vendor) = row
if pkg_data['summary'] is None:
pkg_data['summary'] = summary
pkg_data['description'] = description
pkg_data['url'] = url
pkg_data['rpm_license'] = rpm_license
pkg_data['rpm_sourcerpm'] = rpm_sourcerpm
pkg_data['vendor'] = vendor
size = _humansize(size_package)
# Get latest changelog entry for each version
query = '''SELECT author, date, changelog
FROM changelog WHERE pkgKey=%d
ORDER BY date DESC LIMIT 1''' % pkg_key
ocursor = self.oconn.cursor()
ocursor.execute(query)
orow = ocursor.fetchone()
if not orow:
author = time_added = changelog = None
else:
(author, time_added, changelog) = orow
# strip email and everything that follows from author
try:
author = author[:author.index('<')].strip()
except ValueError:
pass
filelist = []
query = ''' select dirname, filenames, filetypes from filelist
where pkgKey=%d order by dirname desc''' % pkg_key
fcursor = self.fconn.cursor()
fcursor.execute(query)
frows = fcursor.fetchall()
for frow in frows:
fidx = 0
(dirname, filenames, filetypes) = frow
for fname in filenames.split('/'):
filelist.append((filetypes[fidx], (dirname + '/' + fname)))
fidx += 1
pkg_data['rpms'].append((epoch, version, release, arch,
time_build, size, location_href,
author, changelog, time_added, filelist))
return pkg_data
def do_packages(self, repo_data, group_data, pkgnames):
"""
Iterate through package names and write the ones that changed.
@param repo_data: the dict with repository data
@type repo_data: dict
@param group_data: the dict with group data
@type group_data: dict
@param pkgnames: a list of package names (strings)
@type pkgnames: list
@return: a list of tuples related to packages, which we later use
to create the group page. The members are as such:
(pkg_name, pkg_filename, pkg_summary)
@rtype: list
"""
# this is what we return for the group object
pkg_tuples = []
for pkgname in pkgnames:
pkg_filename = _mkid(PKGFILE % pkgname)
if pkgname in self.written.keys():
pkg_tuples.append(self.written[pkgname])
continue
pkg_data = self.get_package_data(pkgname)
if pkg_data is None:
# sometimes comps does not reflect reality
continue
pkg_tuple = (pkgname, pkg_filename, pkg_data['summary'])
pkg_tuples.append(pkg_tuple)
checksum = self.mk_checksum(repo_data, group_data, pkg_data)
if self.has_changed(pkg_filename, checksum):
self.say('Writing package %s\n' % pkg_filename)
self.pkg_kid.group_data = group_data
self.pkg_kid.pkg_data = pkg_data
outfile = os.path.join(self.outdir, pkg_filename)
with open(outfile, "w") as fh:
fh.write(self.pkg_kid.render(
repo_data=repo_data,
group_data=group_data,
pkg_data=pkg_data
))
self.written[pkgname] = pkg_tuple
else:
self.written[pkgname] = pkg_tuple
return pkg_tuples
def mk_checksum(self, *args):
"""
A fairly dirty function used for state tracking. This is how we know
if the contents of the page have changed or not.
@param *args: dicts
@rtype *args: dicts
@return: an md5 checksum of the dicts passed
@rtype: str
"""
mangle = []
for data in args:
# since dicts are non-deterministic, we get keys, then sort them,
# and then create a list of values, which we then pickle.
keys = data.keys()
for key in sorted(keys):
mangle.append(data[key])
return md5.md5(str(mangle).encode()).hexdigest()
def has_changed(self, filename, checksum):
"""
Figure out if the contents of the filename have changed, and do the
necessary state database tracking bits.
@param filename: the filename to check if it's changed
@type filename: str
@param checksum: the checksum from the current contents
@type checksum: str
@return: true or false depending on whether the contents are different
@rtype: bool
"""
# calculate checksum
scursor = self.sconn.cursor()
if filename not in self.state_data.keys():
# totally new entry
query = '''INSERT INTO state (filename, checksum)
VALUES ('%s', '%s')''' % (filename, checksum)
scursor.execute(query)
return True
if self.state_data[filename] != checksum:
# old entry, but changed
query = """UPDATE state
SET checksum='%s'
WHERE filename='%s'""" % (checksum, filename)
scursor.execute(query)
# remove it from state_data tracking, so we know we've seen it
del self.state_data[filename]
return True
# old entry, unchanged
del self.state_data[filename]
return False
def remove_stale(self):
"""
Remove errant stale files from the output directory, left from previous
repoview runs.
@rtype void
"""
scursor = self.sconn.cursor()
for filename in self.state_data.keys():
self.say('Removing stale file %s\n' % filename)
fullpath = os.path.join(self.outdir, filename)
if os.access(fullpath, os.W_OK):
os.unlink(fullpath)
query = """DELETE FROM state WHERE filename='%s'""" % filename
scursor.execute(query)
def z_handler(self, dbfile):
"""
If the database file is compressed, uncompresses it and returns the
filename of the uncompressed file.
@param dbfile: the name of the file
@type dbfile: str
@return: the name of the uncompressed file
@rtype: str
"""
(_, ext) = os.path.splitext(dbfile)
if ext == '.bz2':
from bz2 import BZ2File
zfd = BZ2File(dbfile)
elif ext == '.gz':
from gzip import GzipFile
zfd = GzipFile(dbfile)
elif ext == '.xz':
from lzma import LZMAFile
zfd = LZMAFile(dbfile)
else:
# not compressed (or something odd)
return dbfile
import tempfile
(unzfd, unzname) = tempfile.mkstemp('.repoview')
self.cleanup.append(unzname)
unzfd = open(unzname, 'wb')
while True:
data = zfd.read(16384)
if not data:
break
unzfd.write(data)
zfd.close()
unzfd.close()
return unzname
def setup_comps_groups(self, compsxml):
"""
Utility method for parsing comps.xml.
@param compsxml: the location of comps.xml
@type compsxml: str
@rtype: void
"""
from yum.comps import Comps
self.say('Parsing comps.xml...')
comps = Comps()
comps.add(compsxml)
for group in comps.groups:
if not group.user_visible or not group.packages:
continue
group_filename = _mkid(GRPFILE % group.groupid)
self.groups.append([group.name, group_filename, group.description,
group.packages])
self.say('done\n')
def setup_rpm_groups(self):
"""
When comps is not around, we use the (useless) RPM groups.
@rtype: void
"""
self.say('Collecting group information...')
query = """SELECT DISTINCT lower(rpm_group) AS rpm_group
FROM packages
ORDER BY rpm_group ASC"""
pcursor = self.pconn.cursor()
pcursor.execute(query)
for (rpmgroup,) in pcursor.fetchall():
qgroup = rpmgroup.replace("'", "''")
query = """SELECT DISTINCT name
FROM packages
WHERE lower(rpm_group)='%s'
AND %s
ORDER BY name""" % (qgroup, self.exclude)
pcursor.execute(query)
pkgnames = []
for (pkgname,) in pcursor.fetchall():
pkgnames.append(pkgname)
group_filename = _mkid(GRPFILE % rpmgroup)
self.groups.append([rpmgroup, group_filename, None, pkgnames])
self.say('done\n')
def get_latest_packages(self, limit=30):
"""
Return necessary data for the latest NN packages.
@param limit: how many do you want?
@type limit: int
@return: a list of tuples containting the following data:
(pkgname, filename, version, release, built)
@rtype: list
"""
self.say('Collecting latest packages...')
query = """SELECT name
FROM packages
WHERE %s
GROUP BY name
ORDER BY MAX(time_build) DESC LIMIT %s""" % (self.exclude, limit)
pcursor = self.pconn.cursor()
pcursor.execute(query)
latest = []
query = """SELECT version, release, time_build
FROM packages
WHERE name = '%s'
ORDER BY time_build DESC LIMIT 1"""
for (pkgname,) in pcursor.fetchall():
filename = _mkid(PKGFILE % pkgname.replace("'", "''"))
pcursor.execute(query % pkgname)
(version, release, built) = pcursor.fetchone()
latest.append((pkgname, filename, version, release, built))
self.say('done\n')
return latest
def setup_letter_groups(self):
"""
Figure out which letters we have and set up the necessary groups.
@return: a string containing all first letters of all packages
@rtype: str
"""
self.say('Collecting letters...')
query = """SELECT DISTINCT substr(upper(name), 1, 1) AS letter
FROM packages
WHERE %s
ORDER BY letter""" % self.exclude
pcursor = self.pconn.cursor()
pcursor.execute(query)
letters = ''
for (letter,) in pcursor.fetchall():
letters += letter
rpmgroup = 'Letter %s' % letter
description = 'Packages beginning with letter "%s".' % letter
pkgnames = []
query = """SELECT DISTINCT name
FROM packages
WHERE name LIKE '%s%%'
AND %s""" % (letter, self.exclude)
pcursor.execute(query)
for (pkgname,) in pcursor.fetchall():
pkgnames.append(pkgname)
group_filename = _mkid(GRPFILE % rpmgroup).lower()
letter_group = (rpmgroup, group_filename, description, pkgnames)
self.letter_groups.append(letter_group)
self.say('done\n')
return letters
def do_rss(self, repo_data, latest):
"""
Write the RSS feed.
@param repo_data: the dict containing repository data
@type repo_data: dict
@param latest: the list of tuples returned by get_latest_packages
@type latest: list
@rtype: void
"""
self.say('Generating rss feed...')
etb = TreeBuilder()
out = os.path.join(self.outdir, RSSFILE)
etb.start('rss', {'version': '2.0'})
etb.start('channel', {})
etb.start('title', {})
etb.data(repo_data['title'])
etb.end('title')
etb.start('link', {})
etb.data('%s/repoview/%s' % (self.opts.url, RSSFILE))
etb.end('link')
etb.start('description', {})
etb.data('Latest packages for %s' % repo_data['title'])
etb.end('description')
etb.start('lastBuildDate', {})
etb.data(time.strftime(ISOFORMAT))
etb.end('lastBuildDate')
etb.start('generator', {})
etb.data('Repoview-%s' % repo_data['my_version'])
etb.end('generator')
rss_kid = self.j2env.get_template(RSSKID)
for row in latest:
pkg_data = self.get_package_data(row[0])
rpm = pkg_data['rpms'][0]
(epoch, version, release, arch, built) = rpm[:5]
etb.start('item', {})
etb.start('guid', {})
etb.data('%s/repoview/%s+%s:%s-%s.%s' % (self.opts.url,
pkg_data['filename'],
epoch, version, release,
arch))
etb.end('guid')
etb.start('link', {})
etb.data('%s/repoview/%s' % (self.opts.url, pkg_data['filename']))
etb.end('link')
etb.start('pubDate', {})
etb.data(time.strftime(ISOFORMAT, time.gmtime(int(built))))
etb.end('pubDate')
etb.start('title', {})
etb.data('Update: %s-%s-%s' % (pkg_data['name'], version, release))
etb.end('title')
etb.start('description', {})
etb.data(rss_kid.render(
repo_data=repo_data,
url=self.opts.url,
pkg_data=pkg_data,
))
etb.end('description')
etb.end('item')
etb.end('channel')
etb.end('rss')
rss = etb.close()
etree = ElementTree(rss)
out = os.path.join(self.outdir, RSSFILE)
etree.write(out, 'utf-8')
self.say('done\n')
def main():
"""
Parse the options and invoke the repoview class.
@rtype: void
"""
parser = ArgumentParser()
parser.add_argument("args", nargs=1, help="path to the repository")
parser.add_argument('--version', action='version', version='%(prog)s '+VERSION)
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true',
help='Do not output anything except fatal errors.')
parser.add_argument('-f', '--force', dest='force', action='store_true',
help='Regenerate the pages even if the repomd checksum has not changed')
parser.add_argument('-s', '--state-dir', dest='statedir',
help='Create the state-tracking db in this directory '
'(default: store in output directory)')
repo_opts = parser.add_argument_group("repository specific options")
repo_opts.add_argument('-i', '--ignore-package', dest='ignore', action='append',
default=[],
help='Optionally ignore these packages -- can be a shell-style glob. '
'This is useful for excluding debuginfo packages, e.g.: '
'"-i *debuginfo* -i *doc*". '
'The globbing will be done against name-epoch-version-release, '
'e.g.: "foo-0-1.0-1"')
repo_opts.add_argument('-x', '--exclude-arch', dest='xarch', action='append',
default=[],
help='Optionally exclude this arch. E.g.: "-x src -x ia64"')
repo_opts.add_argument('-c', '--comps', dest='comps',
help='Use an alternative comps.xml file (default: off)')
tpl_opts = parser.add_argument_group("template specific options")
tpl_opts.add_argument('-k', '--template-dir', dest='templatedir',
default=DEFAULT_TEMPLATEDIR,
help='Use an alternative directory with kid templates instead of '
'the default: %(default)s. The template directory must contain four '
'required template files: index.kid, group.kid, package.kid, rss.kid '
'and the "layout" dir which will be copied into the repoview directory')
tpl_opts.add_argument('-o', '--output-dir', dest='outdir',
default='repoview',
help='Create the repoview pages in this subdirectory inside '
'the repository (default: "%(default)s")')
tpl_opts.add_argument('-t', '--title', dest='title',
default='Repoview',
help='Describe the repository in a few words. '
'By default, "%(default)s" is used. '
'E.g.: -t "Extras for Fedora Core 4 x86"')
tpl_opts.add_argument('-E', '--environment', dest='env', action='append',
help='Add environment variables for usage in templates. '
'E.g.: -E "foo=bar" -E "baz=yatta"')
rss_opts = parser.add_argument_group("RSS specific options")
rss_opts.add_argument('-u', '--url', dest='url',
help='Repository URL to use when generating the RSS feed. E.g.: '
'-u "http://fedoraproject.org/extras/4/i386". Leaving it off will '
'skip the rss feed generation')
opts = parser.parse_args()
opts.repodir = opts.args[0]
Repoview(opts)
if __name__ == '__main__':
main()