#!/usr/local/bin/python """ p4rss.py - generate RSS 2.0 feed from 10 most recent changes from Perforce. To run it, python p4rss.py > /path/to/rssfile Other options of p4 changes, -i, -u, -s, etc are not supported. see sample.p4rss.xml for sample output. For more information see p4rss.html RSS 2.0 documentation: http://backend.userland.com/rss """ import ConfigParser import marshal import os import re import string import sys import time from email.Utils import formatdate # customization code copied from python cookbook 4.11 p135 # begin-customization # change the values in p4rss.ini _configDefault = { "c.filespec": '//depot/...', "c.title": 'Most recent changes in public Perforce depot', "c.site_url": 'http://public.perforce.com', "c.cgi_url": '/cgi-bin/p4db/changeView.cgi?CH=', "c.maxchanges": "10", "c.maildomain":'' } def loadConfig(file, config = {}): config = config.copy() cp = ConfigParser.ConfigParser() cp.read(file) for sec in cp.sections(): name = string.lower(sec) for opt in cp.options(sec): config[name + "." + string.lower(opt)] = string.strip(cp.get(sec, opt)) return config c = loadConfig("p4rss.ini", _configDefault) # end-of-cusotmization p4changes = 'p4 -G changes -l -m ' + c['c.maxchanges'] + ' ' + c['c.filespec'] #p4changes = 'p4 -G changes -l -m ' + str(maxchanges) + ' ' + path stream = os.popen(p4changes, 'rb') # enttab borrowed from xmllib.py enttab={'<':'<', '>':'>', '&':'&', '"':'"', '\'': '''} def replaceEntity(match): rawEntity=match.group() return enttab[rawEntity] def escapeEntities(s): ent=re.compile('&|>|<|"'"|'"'') return ent.sub(replaceEntity, s) # print RSS 2.0 print "" print "" print "" print "\t%s" % c['c.title'] print "\t%s" % c['c.site_url'] print "\ten-US" print "\tp4 changes -l -m %s %s in RSS 2.0\n" %(c['c.maxchanges'], c['c.filespec']) print "\t%s" % formatdate(time.time(), True) #time.asctime() try: while 1: dict = marshal.load(stream) print "\t" print "\t\t%s" % dict['change'] print "\t\t%s%s%s" % (c['c.site_url'], c['c.cgi_url'], dict['change']) print "\t\t%s" % formatdate(int(dict['time']), True) print "\t\t%s" % (dict['user']+c['c.maildomain']) print "\t\t%s" % escapeEntities(dict['desc']) print "\t" except EOFError: pass print "" print ""