1 | # Export existing tickets from the database to XML
|
---|
2 |
|
---|
3 | # Constants
|
---|
4 |
|
---|
5 | TRAC_ENV = "/srv/trac/dev.haiku-os.org"
|
---|
6 | OUTPUT= "webpositive-export/"
|
---|
7 |
|
---|
8 | #
|
---|
9 | # 1. set up the XML Elementtree thingie
|
---|
10 | #
|
---|
11 |
|
---|
12 | from xml.etree.ElementTree import Element, SubElement
|
---|
13 |
|
---|
14 | rootelement = Element("Tickets")
|
---|
15 |
|
---|
16 | #
|
---|
17 | # 2. Open the Trac Env
|
---|
18 | #
|
---|
19 |
|
---|
20 | from trac.env import Environment
|
---|
21 | env = Environment(TRAC_ENV)
|
---|
22 | env.verify()
|
---|
23 |
|
---|
24 | #
|
---|
25 | # 3. Get open tickets
|
---|
26 | #
|
---|
27 |
|
---|
28 | db = env.get_db_cnx()
|
---|
29 | ticketcursor = db.cursor()
|
---|
30 | ticketcursor.execute ("SELECT id,time,component,owner,reporter,summary,description FROM ticket WHERE status='accepted' OR status='assigned' OR status='new' OR status='reopened' ORDER BY id LIMIT 20")
|
---|
31 |
|
---|
32 | #
|
---|
33 | # 4. Export the ticket details, and the comments to XML
|
---|
34 | #
|
---|
35 |
|
---|
36 | attachmentslist = [] # for all attachments that need to be copied
|
---|
37 |
|
---|
38 | for id, time, component, owner, reporter, summary, description in ticketcursor:
|
---|
39 | ticketelement = SubElement(rootelement, "Ticket")
|
---|
40 | e = SubElement(ticketelement, "id")
|
---|
41 | e.text = str(id)
|
---|
42 | e = SubElement(ticketelement, "time")
|
---|
43 | e.text = str(time)
|
---|
44 | e = SubElement(ticketelement, "component")
|
---|
45 | e.text = component
|
---|
46 | e = SubElement(ticketelement, "owner")
|
---|
47 | e.text = owner
|
---|
48 | e = SubElement(ticketelement, "reporter")
|
---|
49 | e.text = reporter
|
---|
50 | e = SubElement(ticketelement, "summary")
|
---|
51 | e.text = summary
|
---|
52 | e = SubElement(ticketelement, "description")
|
---|
53 | e.text = description
|
---|
54 |
|
---|
55 | # Retrieve comments
|
---|
56 | commentscursor = db.cursor()
|
---|
57 | commentscursor.execute("SELECT time,author,oldvalue,newvalue FROM ticket_change WHERE ticket=%s AND field='comment' ORDER BY oldvalue", (id,))
|
---|
58 |
|
---|
59 | commentselement = SubElement(ticketelement, "Comments")
|
---|
60 | for time, author, comment_num, comment in commentscursor:
|
---|
61 | e = SubElement(commentselement, "Comment")
|
---|
62 | e.set("time", str(time))
|
---|
63 | e.set("author", author)
|
---|
64 | e.set("num", comment_num)
|
---|
65 | e.text = comment
|
---|
66 |
|
---|
67 | # Retrieve attachments
|
---|
68 | attachmentscursor = db.cursor()
|
---|
69 | attachmentscursor.execute("SELECT filename,time,description,author,size FROM attachment WHERE id=%s", (str(id),))
|
---|
70 |
|
---|
71 | attachmentselement = SubElement(ticketelement, "Attachments")
|
---|
72 | for filename, time, description, author, size in attachmentscursor:
|
---|
73 | e = SubElement(attachmentselement, "Attachment")
|
---|
74 | e.set("filename", filename)
|
---|
75 | e.set("time", str(time))
|
---|
76 | e.set("author", author)
|
---|
77 | e.set("size", str(size))
|
---|
78 | e.text = description
|
---|
79 | path = TRAC_ENV + "/attachments/ticket/" + str(id) + "/" + filename
|
---|
80 | attachmentslist.append(path)
|
---|
81 |
|
---|
82 | #
|
---|
83 | # 5. Copy the attachments
|
---|
84 | #
|
---|
85 |
|
---|
86 | import os, shutil
|
---|
87 | os.mkdir(OUTPUT)
|
---|
88 |
|
---|
89 | for file in attachmentslist:
|
---|
90 | shutil.copy(file,OUTPUT)
|
---|
91 |
|
---|
92 | #
|
---|
93 | # 6. Dump the output
|
---|
94 | #
|
---|
95 |
|
---|
96 | import xml.etree
|
---|
97 | f = open(OUTPUT + "webpositive-export.xml", "w")
|
---|
98 | f.write(xml.etree.ElementTree.tostring(rootelement))
|
---|
99 | f.close()
|
---|