#!/usr/bin/env python # commiturl.py # gsutter@zer0.org _version_id = "$Id: commiturl.py,v 1.3 2003/08/17 06:22:34 gsutter Exp $" import os, sys import string import re import getopt import fileinput def usage(progname): print ("%s adds URLs to FreeBSD commit messages." % progname) print ("Usage: %s [--debug] []" % progname) def checkrepo(line): matchrepo = r"\s\sFreeBSD\s([a-z]+)\srepository" repo = re.match(matchrepo, line) if repo: cvsrepo = repo.expand(r"\1") if sys.debug == 1: print ("DEBUG: cvsrepo: %s" % cvsrepo) return(cvsrepo) def checkcommit(line, cvsrepo): matchnew = "\s+[\d.]+\s+\+\d+\s+\-\d+\s+([-A-Za-z]+)/(.*)\s+\((dead|new)\)" matchdiff = "\s+([\d.]+)\.(\d+)\s+\+\d+\s+\-\d+\s+([-A-Za-z]+)/(.*)" matchpr = "\s+PR\:[^\d]+(\d+)" pr = re.match(matchpr, line) if pr: prinfo = pr.expand(r"\1") if sys.debug == 1: print ("DEBUG: prinfo: %s" % prinfo) prurl = r"http://www.freebsd.org/cgi/query-pr.cgi?pr=" + prinfo return(prurl) new = re.match(matchnew, line) if new: (cvstree, cvsinfo) = new.expand(r"\1"), new.expand(r"\2") if sys.debug == 1: print ("DEBUG: cvstree (new): %s" % cvstree) print ("DEBUG: cvsinfo (new): %s" % cvsinfo) if cvstree == "CVSROOT": cvstree = "CVSROOT-%s" % cvsrepo cvsurl = r"http://cvsweb.freebsd.org/" + cvstree + r"/" + cvsinfo if sys.debug == 1: print ("DEBUG: cvsurl: %s" % cvsurl) return(cvsurl) diff = re.match(matchdiff, line) if diff: (diffmajor, diffminor, cvstree, cvsinfo) = diff.expand(r"\1"), \ diff.expand(r"\2"), diff.expand(r"\3"), diff.expand(r"\4") if sys.debug == 1: print ("DEBUG: diffmajor: %s" % diffmajor) print ("DEBUG: diffminor: %s" % diffminor) print ("DEBUG: cvstree (diff): %s" % cvstree) print ("DEBUG: cvsinfo (diff): %s" % cvsinfo) if cvstree == "CVSROOT": cvstree = "CVSROOT-%s" % cvsrepo cvsurl = r"http://cvsweb.freebsd.org/" + cvstree + r"/" + cvsinfo + \ r".diff?&r1=" + str(diffmajor) + r"." + str((int(diffminor) - 1)) + \ r"&r2=" + str(diffmajor) + r"." + str(diffminor) + r"&f=h" if sys.debug == 1: print ("DEBUG: cvsurl: %s" % cvsurl) return(cvsurl) return() def main(argv, stdout, environ): progname = string.split(argv[0], "/")[-1] options, args = getopt.getopt(argv[1:], "dh", ["debug", "help"]) # default is non-debug mode sys.debug = 0 for (field, value) in options: if field == "--debug" or field == "-d": sys.debug = 1 if field == "--help" or field== "-h": usage(progname) sys.exit(-1) if len(args) > 1: usage(progname) print "Exiting: can take 0 or 1 arguments." sys.exit(-1) # initialize with fake repo name, since we know the real repo will # be identified before it is needed in a URL. cvsrepo = "foo" urllist = [] for line in fileinput.input(args): repo = checkrepo(line) if repo: if sys.debug == 1: print ("DEBUG: repo: %s" % repo) cvsrepo = repo url = checkcommit(line, cvsrepo) if url: if sys.debug == 1: print ("DEBUG: url: %s" % url) urllist.append(url) print ("%s" % line), for url in urllist: print ("%s" % url) if __name__ == "__main__": main(sys.argv, sys.stdout, os.environ)