#!/usr/bin/python2.4 import sys from optparse import OptionParser import os from time import strptime, time, strftime class MalformedDataException(Exception): def __init__(self, str): self.string = str def __str__(self): return self.string def main(): parser = OptionParser() parser.add_option("-o","--outfile",dest="outfile", help="Specify the file to which the history output will be written") opts, args = parser.parse_args() for filename in args: if os.path.exists(filename): print "Parsing %s" % filename parse(filename, opts.outfile) def parse(filename, outfile): """ Function to manage reading in all the commits """ f = open(filename, 'rb') o = open(outfile, 'ab') c = Commit(filename) while(c.read(f)): c.to_cvsHistory(o) del c c = Commit(filename) class Commit(object): """ Class to encompass a cvs commit """ source = "" root_dir = 'gentoo-x86' def __init__(self, path): if path.endswith(',v'): path = path[:path.find(',v')] if 'Attic' in path: self.action = 'R' path = path.replace('/Attic','') # we just want bits after gentoo-x86 split = path.rfind('/') self.basename = path[split + 1:] self.dirname = path[:split] def read(self, stream): l = stream.readline().strip() while(not l.startswith('desc')): if not len(l): l = stream.readline().strip() continue revno = l.split()[0] if '.' in revno: self.revno = revno if self.revno == '1.1': self.action = 'A' else: self.action = 'M' if l.startswith('date'): split = l.split('\t') if len(split) > 2: self.date = split[1].strip(' \t\n;') self._Base16Date() self.author = split[2].strip(' \t\n;').split()[1] else: raise MalformedDataException("Expecting a tuple of at least length 3, got %s" % split) return True l = stream.readline().strip() return False def _Base16Date(self): """ Change self.date into a string representation of a base16 date """ unixtime = int(strftime("%s", strptime(self.date,"%Y.%m.%d.%H.%M.%S"))) self.base16date = hex(unixtime)[2:] def to_cvsHistory(self, stream): """ write this comment in cvs history format: M46231f4c|opfer||gentoo-x86/net-dialup/hsfmodem|1.70|Manifest R, M or A for 'removed','modified', 'added, then the Base16 (hex) value of the timestamp then the username, , path to file, revno, and then the filename """ try: stream.write("%s%s|%s|%s|%s|%s|%s\n" % (self.action, self.base16date, self.author, self.source, self.dirname, self.revno, self.basename)) except: import pdb pdb.set_trace() if __name__ == "__main__": sys.exit(not main())