Mailing List Archive

csv2trac.py
In part of my giving back to the Trac community (okay, so it's a little
self-serving too), I spun up the below code to import a CSV file of tickets
into the Trac database.

I don't know if it's a sad reflection on me or a tribute to Python, but the
comments are longer than the code (but took less time to write!)

Will place in /contrib if y'all like it.

Cheers,

:D

--------------------------------------------------------------------
Daragh Fitzpatrick Daragh@UChicago.edu (773) 702-8976

Solutions Architect NSIT Administrative Systems
Renewal Projects and Architecture University of Chicago
--------------------------------------------------------------------

---- cut here ----

"""
csv2trac.py: Import a CSV file into a Trac issue database.

Description:
Imports CSV File with the following format:
id,time,changetime,component,severity,priority,owner,reporter,cc,url,
version,milestone,status,resolution,summary,description,keywords
where:
status = new, assigned, reopened, closed
priority = highest, high, normal, low, lowest
severity = blocker, critical, major, normal, minor, trivial,
enhancement
No smarts, you get to do all the work in the CSV file (sorry!)

Dependencies:
Trac 0.7 from http://trac.edgewall.com/
Python 2.3 from http://www.python.org/

Notes:
NB! This will delete existing tickets with conflicting IDs!
Make sure you have removed ',' & ';' from the source data

To-Do:
Create importer that will load any file using XSLT and a style-sheet for the
import format. Volunteers?

Copyright Daragh Fitzpatrick <daragh@i2i-Tech.com>
"""
import csv

def main():
import optparse
p = optparse.OptionParser('%prog file.csv /path/to/trac/environment')
opt, args = p.parse_args()
if len(args) != 2:
p.error("2 arguments required")
try:
importCSV(csv.reader(file(args[0])), args[1])
except Exception, e:
print 'Error:', e

def importCSV(reader, env):
import trac.Environment
env = trac.Environment.Environment(env)
db = env.get_db_cnx()
db.autocommit = False

for row in reader:
print row[0]
c = db.cursor()
c.execute("""DELETE FROM ticket where id=%s""",row[0])
c.execute("""INSERT INTO ticket (id, time, changetime, component,
severity,
priority, owner, reporter, cc, url, version, milestone,
status,
resolution, summary, description, keywords)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s)""",
row[0], row[1], row[2], row[3], row[4], row[5], row[6],
row[7],row[8],
row[9], row[10], row[11], row[12], row[13], row[14],
row[15], row[16] )
db.commit()

if __name__ == '__main__':
main()
csv2trac.py [ In reply to ]
Jonas, Daniel,

Please accept the following (and attached) contribution. Thanks
again for a fantastic product!

Cheers,

:D

--------------------------------------------------------------------
Daragh Fitzpatrick Daragh@UChicago.edu (773) 702-8976

Solutions Architect NSIT Administrative Systems
Renewal Projects and Architecture University of Chicago
--------------------------------------------------------------------
-----Original Message-----
From: trac-bounces@bobcat.edgewall.com
[mailto:trac-bounces@bobcat.edgewall.com] On Behalf Of Daragh Fitzpatrick
Sent: Monday, May 24, 2004 2:59 PM
To: trac@bobcat.edgewall.com
Subject: [Trac] csv2trac.py


In part of my giving back to the Trac community (okay, so it's a little
self-serving too), I spun up the below code to import a CSV file of tickets
into the Trac database.

I don't know if it's a sad reflection on me or a tribute to Python, but the
comments are longer than the code (but took less time to write!)

Will place in /contrib if y'all like it.

Cheers,

:D

--------------------------------------------------------------------
Daragh Fitzpatrick Daragh@UChicago.edu (773) 702-8976

Solutions Architect NSIT Administrative Systems
Renewal Projects and Architecture University of Chicago
--------------------------------------------------------------------

---- cut here ----
"""
csv2trac.py: Import a CSV file into a Trac issue database.

== Description: ==
Imports CSV File with the following format:
id, time, changetime, component, severity, priority, owner,
reporter, cc, url, version, milestone, status, resolution,
summary, description, keywords
where status = new, assigned, reopened, closed

No smarts, you get to do all the work in the CSV file (sorry!)

== Dependencies: ==
* Trac 0.7 http://trac.edgewall.com/
* Python 2.3 from http://www.python.org/

== Notes: ==
NB! This will delete existing tickets with conflicting IDs!
* Make sure you have removed ',' from the source data
* Clean up the data E.g., names are equivalent to Trac user names, etc.
* Set component, priority, version and severity in trac-admin based on your
project

== To-Do: ==
* Create importer that will load any file using XSLT and a style-sheet for
the import format.

Copyright Daragh Fitzpatrick <daragh@i2i-Tech.com>
"""

import csv

def main():
import optparse
p = optparse.OptionParser('%prog file.csv /path/to/trac/environment')
opt, args = p.parse_args()
if len(args) != 2:
p.error("2 arguments required")
try:
importCSV(csv.reader(file(args[0])), args[1])
except Exception, e:
print 'Error:', e

def importCSV(csvfile, env):
import trac.Environment
env = trac.Environment.Environment(env)
db = env.get_db_cnx()
db.autocommit = False

for row in csvfile:
print row[0]
c = db.cursor()
c.execute("""DELETE FROM ticket where id=%s""",row[0])
c.execute("""INSERT INTO ticket (id, time, changetime, component,
severity,
priority, owner, reporter, cc, url, version, milestone,
status,
resolution, summary, description, keywords)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s)""",
row[0], row[1], row[2], row[3], row[4], row[5], row[6],
row[7],row[8],
row[9], row[10], row[11], row[12], row[13], row[14],
row[15], row[16] )
db.commit()

if __name__ == '__main__':
main()
-------------- next part --------------
"""
csv2trac.py: Import a CSV file into a Trac issue database.

== Description: ==
Imports CSV File with the following format:
id, time, changetime, component, severity, priority, owner,
reporter, cc, url, version, milestone, status, resolution,
summary, description, keywords
where status = new, assigned, reopened, closed

No smarts, you get to do all the work in the CSV file (sorry!)

== Dependencies: ==
* Trac 0.7 http://trac.edgewall.com/
* Python 2.3 from http://www.python.org/

== Notes: ==
NB! This will delete existing tickets with conflicting IDs!
* Make sure you have removed ',' from the source data
* Clean up the data E.g., names are equivalent to Trac user names, etc.
* Set component, priority, version and severity in trac-admin based on your project

== To-Do: ==
* Create importer that will load any file using XSLT and a style-sheet for the import format.

Copyright Daragh Fitzpatrick <daragh@i2i-Tech.com>
"""

import csv

def main():
import optparse
p = optparse.OptionParser('%prog file.csv /path/to/trac/environment')
opt, args = p.parse_args()
if len(args) != 2:
p.error("2 arguments required")
try:
importCSV(csv.reader(file(args[0])), args[1])
except Exception, e:
print 'Error:', e

def importCSV(csvfile, env):
import trac.Environment
env = trac.Environment.Environment(env)
db = env.get_db_cnx()
db.autocommit = False

for row in csvfile:
print row[0]
c = db.cursor()
c.execute("""DELETE FROM ticket where id=%s""",row[0])
c.execute("""INSERT INTO ticket (id, time, changetime, component, severity,
priority, owner, reporter, cc, url, version, milestone, status,
resolution, summary, description, keywords)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7],row[8],
row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16] )
db.commit()

if __name__ == '__main__':
main()