Skip to content

Commit

Permalink
Minor refactoring for clarity and approachability
Browse files Browse the repository at this point in the history
  • Loading branch information
rictic committed Nov 5, 2008
1 parent ab73278 commit 1306686
Showing 1 changed file with 85 additions and 88 deletions.
173 changes: 85 additions & 88 deletions bin/convert_logs.py
@@ -1,5 +1,5 @@
#!/usr/bin/python
# Standard library imports
#!/usr/bin/env python

from datetime import datetime
from optparse import OptionParser
import os
Expand All @@ -17,27 +17,64 @@
CVS_SEP = "----------------------------"


class Event(object):
""" Event to hold all of the separate events as we parse them from the logs. """

def __init__(self, filename, date, author):
self.filename = filename
self.date = date
self.author = author
def parse_args(argv):
""" Parses command line arguments and returns an options object
along with any extra arguments.
"""
p = OptionParser()

p.add_option("-s", "--svn-log", dest="svn_log",
metavar="<log file>",
help="input svn log to convert to standard event xml")

p.add_option("-c", "--cvs-log", dest="cvs_log",
metavar="<log file>",
help="input cvs log to convert to standard event xml")

p.add_option("-g", "--git-log", dest="git_log",
metavar="<log file>",
help="input git log to convert to standard event xml")

p.add_option("-v", "--vss-log", dest="vss_log",
metavar="<log file>",
help="input vss report to convert to standard event xml")

p.add_option("-t", "--starteam-log", dest="starteam_log",
metavar="<log file>",
help="input starteam log to convert to standard event xml")

p.add_option("-w", "--wikiswarm-log", dest="wikiswarm_log",
metavar="<log file>",
help="input wikiswarm log to convert to standard event xml")

p.add_option("-m", "--mercurial-log", dest="mercurial_log",
metavar="<log file>",
help="input mercurial log to convert to standard event xml")

p.add_option("-l", "--gnu-changelog", dest="gnu_log",
metavar="<log file>",
help="input GNU Changelog to convert to standard event xml")

p.add_option( "-p", "--perforce-path", dest="perforce_path",
metavar="<log file>",
help="get data from perforce and save it to standard event xml")

def properties(self):
"""returns a dict of properties and their names for XML serialization"""
return {
"date": str(self.date),
"filename": self.filename,
"author": self.author
}

# Some version control system's logs are not in chronological order, so
# this compare method will return a compare of the date attributes.
def __cmp__(self, other):
return cmp(self.date, other.date)
p.add_option( "-o", "--output-log", dest="output_log",
metavar="<log file>",
help="specify event log output file")

p.add_option("--nosort", dest="nosort", default=False, action="store_true",
help="use if the input log is already in chronological order for speed")

p.add_option("--ignore-author", dest="ignore_author", default=None,
action="store",
help="Ignore authors that match this regular expression.")


(options, args) = p.parse_args(argv)

return (options, args)


def main(argv):
""" Calls the parse_args function based on the
Expand Down Expand Up @@ -85,15 +122,15 @@ def main(argv):
# check for valid cmd line arguments before doing anything
if opts.ignore_author is not None:
try:
re.compile(opts.ignore_author)
opts.ignore_author = re.compile(opts.ignore_author)
except sre_constants.error:
print >>stderr, "Unable to compile author reg ex: %s" % \
opts.ignore_author
sys.exit(1)

if not os.path.exists(log_file):
#hacky, but OptionParse doesn't support options that only sometimes
# have an extra value
# take an argument
if log_file == "stdin":
log_file = sys.stdin
else:
Expand All @@ -102,13 +139,15 @@ def main(argv):
else:
log_file = open(log_file, 'r')

#Parse the log_file into an iterable of Events
events = parser(log_file, opts)

# Remove all authors we wanted to ignore here
if opts.ignore_author is not None:
events = remove_ignored_author(opts.ignore_author, events)
events = ifilter(lambda e: opts.ignore_author.match(e.author) is None,
events)

#its really best if we don't have to sort, but by default most are
#its really best if we don't have to sort, but most data sources are
# in the reverse order that we want, so we sort by default
if not opts.nosort:
events= sorted(list(events))
Expand Down Expand Up @@ -136,63 +175,6 @@ def create_event_xml(events, output):
generator.endElement('file_events')
generator.endDocument()

def parse_args(argv):
""" Parses command line arguments and returns an options object
along with any extra arguments.
"""
p = OptionParser()

p.add_option("-s", "--svn-log", dest="svn_log",
metavar="<log file>",
help="input svn log to convert to standard event xml")

p.add_option("-c", "--cvs-log", dest="cvs_log",
metavar="<log file>",
help="input cvs log to convert to standard event xml")

p.add_option("-g", "--git-log", dest="git_log",
metavar="<log file>",
help="input git log to convert to standard event xml")

p.add_option("-v", "--vss-log", dest="vss_log",
metavar="<log file>",
help="input vss report to convert to standard event xml")

p.add_option("-t", "--starteam-log", dest="starteam_log",
metavar="<log file>",
help="input starteam log to convert to standard event xml")

p.add_option("-w", "--wikiswarm-log", dest="wikiswarm_log",
metavar="<log file>",
help="input wikiswarm log to convert to standard event xml")

p.add_option("-m", "--mercurial-log", dest="mercurial_log",
metavar="<log file>",
help="input mercurial log to convert to standard event xml")

p.add_option("-l", "--gnu-changelog", dest="gnu_log",
metavar="<log file>",
help="input GNU Changelog to convert to standard event xml")

p.add_option( "-p", "--perforce-path", dest="perforce_path",
metavar="<log file>",
help="get data from perforce and save it to standard event xml")

p.add_option( "-o", "--output-log", dest="output_log",
metavar="<log file>",
help="specify event log output file")

p.add_option("--nosort", dest="nosort", default=False, action="store_true",
help="use if the input log is already in chronological order for speed")

p.add_option("--ignore-author", dest="ignore_author", default=None,
action="store",
help="Ignore authors that match this regular expression.")


(options, args) = p.parse_args(argv)

return (options, args)


def parse_svn_log(file_handle, opts):
Expand Down Expand Up @@ -428,12 +410,6 @@ def parse_perforce_path(file_handle, opts):
yield Event(file_name, int(changelist['time'] + '000'), changelist['user'])


def remove_ignored_author(ignore, events):
""" Remove the events that match the given ignore reg ex. """
events = ifilter(lambda evt: re.match(ignore, evt.author) is None,
events)
return events


def run_marshal(command):
import marshal
Expand All @@ -449,6 +425,27 @@ def run_marshal(command):
stream.close()
return results

class Event(object):
""" Event to hold all of the separate events as we parse them from the logs. """

def __init__(self, filename, date, author):
self.filename = filename
self.date = date
self.author = author

def properties(self):
"""returns a dict of properties and their names for XML serialization"""
return {
"date": str(self.date),
"filename": self.filename,
"author": self.author
}

# Some version control system's logs are not in chronological order, so
# this compare method will return a compare of the date attributes.
def __cmp__(self, other):
return cmp(self.date, other.date)


# Main entry point.
if __name__ == "__main__":
Expand Down

0 comments on commit 1306686

Please sign in to comment.