2 # Generate two rss feeds for a directory with .changes file
4 # License: GPL v2 or later
5 # Author: Filippo Giunchedi <filippo@debian.org>
12 import encodings.ascii
13 from optparse import OptionParser
14 from datetime import datetime
18 from debian_bundle.deb822 import Changes
20 inrss_filename = "NEW_in.rss"
21 outrss_filename = "NEW_out.rss"
22 db_filename = "status.db"
24 parser = OptionParser()
25 parser.set_defaults(queuedir="queue", outdir="out", datadir="status", max_entries="30")
27 parser.add_option("-q", "--queuedir", dest="queuedir",
28 help="The queue dir (%default)")
29 parser.add_option("-o", "--outdir", dest="outdir",
30 help="The output directory (%default)")
31 parser.add_option("-d", "--datadir", dest="datadir",
32 help="The data dir (%default)")
33 parser.add_option("-m", "--max-entries", dest="max_entries", type="int",
34 help="Max number of entries to keep (%default)")
38 self.feed_in = PyRSS2Gen.RSS2(
39 title = "Packages entering NEW",
40 link = "http://ftp-master.debian.org/new.html",
41 description = "Debian packages entering the NEW queue" )
43 self.feed_out = PyRSS2Gen.RSS2(
44 title = "Packages leaving NEW",
45 link = "http://ftp-master.debian.org/new.html",
46 description = "Debian packages leaving the NEW queue" )
51 """ Return an ASCII encoded copy of the input UTF-8 string """
53 res = unicode(src, 'utf-8').encode('ascii', 'replace')
54 except UnicodeDecodeError:
58 def purge_old_items(feed, max):
59 """ Purge RSSItem from feed, no more than max. """
60 if feed.items is None or len(feed.items) == 0:
63 feed.items = feed.items[:max]
66 def parse_changes(fname):
67 """ Parse a .changes file named fname.
69 Return {fname: parsed} """
71 m = Changes(open(fname))
73 wanted_fields = set(['Source', 'Version', 'Architecture', 'Distribution',
74 'Date', 'Maintainer', 'Description', 'Changes'])
76 if not set(m.keys()).issuperset(wanted_fields):
79 return {os.path.basename(fname): m}
81 def parse_queuedir(dir):
82 """ Parse dir for .changes files.
84 Return a dictionary {filename: parsed_file}"""
86 if not os.path.exists(dir):
90 for fname in os.listdir(dir):
91 if not fname.endswith(".changes"):
94 parsed = parse_changes(os.path.join(dir, fname))
100 def add_rss_item(status, msg, direction):
101 if direction == "in":
102 feed = status.feed_in
103 title = "%s %s entered NEW" % (msg['Source'], msg['Version'])
104 pubdate = msg['Date']
105 elif direction == "out":
106 feed = status.feed_out
107 title = "%s %s left NEW" % (msg['Source'], msg['Version'])
108 pubdate = datetime.utcnow()
112 description = "<pre>Description: %s\nChanges: %s\n</pre>" % \
113 (utf2ascii(msg['Description']), utf2ascii(msg['Changes']))
119 description = description,
120 author = utf2ascii(msg['Maintainer']),
121 link = "http://ftp-master.debian.org/new/%s_%s.html" % \
122 (msg['Source'], msg['Version'])
126 def update_feeds(curqueue, status):
127 # inrss -> append all items in curqueue not in status.queue
128 # outrss -> append all items in status.queue not in curqueue
130 for (name, parsed) in curqueue.items():
131 if not status.queue.has_key(name):
133 add_rss_item(status, parsed, "in")
135 for (name, parsed) in status.queue.items():
136 if not curqueue.has_key(name):
138 add_rss_item(status, parsed, "out")
142 if __name__ == "__main__":
144 (settings, args) = parser.parse_args()
146 if not os.path.exists(settings.outdir):
147 sys.stderr.write("Outdir '%s' does not exists\n" % settings.outdir)
151 if not os.path.exists(settings.datadir):
152 sys.stderr.write("Datadir '%s' does not exists\n" % settings.datadir)
156 status_db = os.path.join(settings.datadir, db_filename)
159 status = cPickle.load(open(status_db))
163 current_queue = parse_queuedir(settings.queuedir)
164 if not current_queue:
165 sys.stderr.write("Unable to scan queuedir '%s'\n" % settings.queuedir)
169 update_feeds(current_queue, status)
171 purge_old_items(status.feed_in, settings.max_entries)
172 purge_old_items(status.feed_out, settings.max_entries)
174 feed_in_file = os.path.join(settings.outdir, inrss_filename)
175 feed_out_file = os.path.join(settings.outdir, outrss_filename)
178 status.feed_in.write_xml(file(feed_in_file, "w+"), "utf-8")
179 status.feed_out.write_xml(file(feed_out_file, "w+"), "utf-8")
181 sys.stderr.write("Unable to write feeds: %s\n", why)
184 status.queue = current_queue
187 cPickle.dump(status, open(status_db, "w+"))
189 sys.stderr.write("Unable to save status: %s\n", why)