Created
February 7, 2013 15:56
-
-
Save Phaeilo/4731855 to your computer and use it in GitHub Desktop.
Load a svn xml logfile into a sqlite database for easier analysis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import datetime | |
import sqlite3 | |
import sys | |
def convert(logfile_filename, database_filename): | |
cnt = 0 | |
# open file handles | |
with open(logfile_filename, "rb") as logfile: | |
with sqlite3.connect(database_filename) as db: | |
cursor = db.cursor() | |
# create schema | |
cursor.execute(""" | |
create table commits ( | |
revision integer primary key, | |
author text not null, | |
date integer not null, | |
message text null | |
) | |
""") | |
db.commit() | |
# parse logfile | |
tree = ET.parse(logfile) | |
log = tree.getroot() | |
assert log.tag == "log" | |
# iterate over commits | |
for logentry in log: | |
assert logentry.tag == "logentry" | |
# extract information | |
revision = logentry.get("revision") | |
assert revision is not None | |
revision = int(revision) | |
author = logentry.find("author") | |
assert author is not None | |
author = author.text | |
date = logentry.find("date") | |
assert date is not None | |
date = datetime.datetime.strptime( | |
date.text, "%Y-%m-%dT%H:%M:%S.%fZ") | |
msg = logentry.find("msg") | |
assert msg is not None | |
msg = msg.text | |
if msg is not None and len(msg) == 0: | |
msg = None | |
# insert database record | |
cursor.execute(""" | |
insert into commits (revision, author, date, message) | |
values (?, ?, ?, ?) | |
""", (revision, author, date, msg)) | |
cnt += 1 | |
# commit inserted records | |
db.commit() | |
return cnt | |
def main(): | |
if len(sys.argv) != 3: | |
print "usage %s <svn xml logfile> <sqlite database>" % (sys.argv[0],) | |
sys.exit(1) | |
cnt = convert(sys.argv[1], sys.argv[2]) | |
print "converted %d entries" % (cnt,) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment