1 | #!/usr/bin/python |
---|
2 | #--------------------------------------------------------------- |
---|
3 | # Project : Mandriva Linux |
---|
4 | # Module : rss |
---|
5 | # File : rpm2rss.py |
---|
6 | # Version : $Id: rpm2rss.py 914 2006-05-02 10:31:10Z pterjan $ |
---|
7 | # Author : Frederic Lepied |
---|
8 | # Created On : Fri Apr 15 09:50:43 2005 |
---|
9 | #--------------------------------------------------------------- |
---|
10 | |
---|
11 | import site |
---|
12 | site.addsitedir("/usr/share/rpmlint") |
---|
13 | import Pkg |
---|
14 | |
---|
15 | import rpm |
---|
16 | import time |
---|
17 | import sys |
---|
18 | import types |
---|
19 | |
---|
20 | from RSS import ns, CollectionChannel |
---|
21 | |
---|
22 | MAX_ITEMS = 100 |
---|
23 | |
---|
24 | def _createChannel(filename, title, description, link): |
---|
25 | try: |
---|
26 | channel = CollectionChannel() |
---|
27 | channel.parse("file:" + filename) |
---|
28 | |
---|
29 | return channel |
---|
30 | except: |
---|
31 | fields = { (ns.rss10, "title"): title, |
---|
32 | (ns.rss10, "description"): description, |
---|
33 | (ns.rss10, "link"): link } |
---|
34 | |
---|
35 | return CollectionChannel({ (ns.rss10, "channel"): fields }) |
---|
36 | |
---|
37 | def filter(s): |
---|
38 | s = s.decode('utf-8', 'ignore') |
---|
39 | s = s.encode('ascii', 'xmlcharrefreplace') |
---|
40 | return s |
---|
41 | |
---|
42 | def idx(array, i): |
---|
43 | if types.ListType == type(array): |
---|
44 | return array[i] |
---|
45 | else: |
---|
46 | return array |
---|
47 | |
---|
48 | def _createItem(filename, link): |
---|
49 | pkg = Pkg.Pkg(filename, "/tmp") |
---|
50 | |
---|
51 | tb = pkg[rpm.RPMTAG_BUILDTIME] |
---|
52 | builddate = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(tb)) |
---|
53 | |
---|
54 | tc = idx(pkg[rpm.RPMTAG_CHANGELOGTIME], 0) |
---|
55 | changelogdate = time.strftime("%a %b %d %Y", time.gmtime(tc)) |
---|
56 | |
---|
57 | n = idx(pkg[rpm.RPMTAG_CHANGELOGNAME], 0) |
---|
58 | e = idx(pkg[rpm.RPMTAG_CHANGELOGTEXT], 0) |
---|
59 | |
---|
60 | |
---|
61 | return { (ns.rss10, "title"): "%s-%s-%s" % (pkg[rpm.RPMTAG_NAME], |
---|
62 | pkg[rpm.RPMTAG_VERSION], |
---|
63 | pkg[rpm.RPMTAG_RELEASE]), |
---|
64 | (ns.rss10, "link"): "%s%s&version=%s-%s" % (link, |
---|
65 | pkg[rpm.RPMTAG_NAME], |
---|
66 | pkg[rpm.RPMTAG_VERSION], |
---|
67 | pkg[rpm.RPMTAG_RELEASE]), |
---|
68 | (ns.rss10, "description"): "%s %s<br /><pre>%s</pre>" % (changelogdate, |
---|
69 | filter(n), |
---|
70 | filter(e)), |
---|
71 | (ns.dc, "date"): builddate } |
---|
72 | |
---|
73 | if len(sys.argv) != 7: |
---|
74 | print "usage: %s <rss filename> <channel title> <channel summary> <channel link> <package file> <package link>" % sys.argv[0] |
---|
75 | sys.exit(1) |
---|
76 | |
---|
77 | filename = sys.argv[1] |
---|
78 | title = sys.argv[2] |
---|
79 | summary = sys.argv[3] |
---|
80 | link = sys.argv[4] |
---|
81 | package = sys.argv[5] |
---|
82 | plink = sys.argv[6] |
---|
83 | |
---|
84 | channel = _createChannel(filename, title, summary, link) |
---|
85 | |
---|
86 | channel.addItem(_createItem(package, plink)) |
---|
87 | del channel[(ns.rss10, "items")][MAX_ITEMS:] |
---|
88 | open(filename, "w").write( str(channel) ) |
---|
89 | |
---|
90 | # rpm2rss.py ends here |
---|