Mercurial > genshi > mirror
comparison examples/trac/scripts/tracdb2env @ 39:93b4dcbafd7b trunk
Copy Trac to main branch.
author | cmlenz |
---|---|
date | Mon, 03 Jul 2006 18:53:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
38:ee669cb9cccc | 39:93b4dcbafd7b |
---|---|
1 #!/usr/bin/env python | |
2 import os | |
3 import sys | |
4 import sqlite | |
5 import ConfigParser | |
6 from trac.env import Environment | |
7 | |
8 additional_config = \ | |
9 (('notification', 'smtp_enabled', 'false'), | |
10 ('notification', 'smtp_server', 'localhost'), | |
11 ('notification', 'smtp_replyto', 'trac@localhost'), | |
12 ('logging', 'log_type', 'none'), | |
13 ('logging', 'log_file', 'trac.log'), | |
14 ('logging', 'log_level', 'DEBUG'), | |
15 ('attachment', 'max_size', '262144'), | |
16 ('trac', 'default_charset', 'iso-8859-15'), | |
17 ('trac', 'database', 'sqlite:db/trac.db')) | |
18 | |
19 def db2env(db_path, env_path): | |
20 env = Environment(env_path, create=1) | |
21 # Open the databases | |
22 old_cnx = sqlite.connect(db_path) | |
23 new_cnx = env.get_db_cnx() | |
24 old_cursor = old_cnx.cursor() | |
25 new_cursor = new_cnx.cursor() | |
26 convert_config(env, old_cursor) | |
27 convert_db(old_cursor, new_cursor) | |
28 new_cursor.execute("INSERT INTO system VALUES('database_version', '7')") | |
29 new_cnx.commit() | |
30 | |
31 def convert_config(env, old_cursor): | |
32 old_cursor.execute('SELECT section, name, value FROM config') | |
33 while 1: | |
34 row = old_cursor.fetchone() | |
35 if not row: | |
36 break | |
37 row = [row[0], row[1], row[2]] | |
38 if row[0] == 'general': | |
39 row[0] = 'trac' | |
40 if row[1] == 'database_version': | |
41 continue | |
42 env.set_config(row[0], row[1], row[2]) | |
43 for v in additional_config: | |
44 env.set_config(*v) | |
45 env.save_config() | |
46 | |
47 def to_utf8(row): | |
48 x = [] | |
49 for v in row: | |
50 if type(v) == type(''): | |
51 try: | |
52 u = unicode(v, 'utf-8') | |
53 x.append(v) | |
54 except UnicodeError: | |
55 u = unicode(v, 'iso-8859-15') | |
56 x.append(u.encode('utf-8')) | |
57 else: | |
58 x.append(v) | |
59 return x | |
60 | |
61 def copy_tuples(table, from_cursor, to_cursor, fields='*'): | |
62 from_cursor.execute('SELECT %s FROM %s' % (fields, table)) | |
63 while 1: | |
64 row = from_cursor.fetchone() | |
65 if not row: | |
66 break | |
67 row = to_utf8(row) | |
68 if fields == '*': | |
69 to_cursor.execute('INSERT INTO %s VALUES(%s)' \ | |
70 % (table, ', '.join(['%s'] * len(row))), *row) | |
71 else: | |
72 to_cursor.execute('INSERT INTO %s (%s) VALUES(%s)' \ | |
73 % (table, fields, | |
74 ', '.join(['%s'] * len(row))), *row) | |
75 | |
76 def convert_db(old_cursor, new_cursor): | |
77 copy_tuples('revision', old_cursor, new_cursor) | |
78 copy_tuples('node_change', old_cursor, new_cursor) | |
79 copy_tuples('auth_cookie', old_cursor, new_cursor) | |
80 copy_tuples('enum', old_cursor, new_cursor) | |
81 copy_tuples('ticket_change', old_cursor, new_cursor) | |
82 copy_tuples('permission', old_cursor, new_cursor) | |
83 copy_tuples('component', old_cursor, new_cursor) | |
84 copy_tuples('milestone', old_cursor, new_cursor, "name, time") | |
85 new_cursor.execute("UPDATE milestone SET descr=''") | |
86 copy_tuples('version', old_cursor, new_cursor) | |
87 copy_tuples('report', old_cursor, new_cursor, | |
88 'id,author,title,sql') | |
89 copy_tuples('ticket', old_cursor, new_cursor, | |
90 'id,time,changetime,component,severity,priority,' | |
91 'owner,reporter,cc,url,version,milestone,status,' | |
92 'resolution,summary,description') | |
93 copy_tuples('wiki', old_cursor, new_cursor, | |
94 'name,version,time,author,ipnr,text') | |
95 | |
96 if __name__ == '__main__': | |
97 if len(sys.argv) != 3: | |
98 print >> sys.stderr, 'Usage: %s <db-file> <env-dir>\n' % sys.argv[0] | |
99 print >> sys.stderr, \ | |
100 'Creates a new Trac environment and initializes it with ' \ | |
101 'information\nfrom an existing pre 0.7 trac database.' | |
102 print >> sys.stderr | |
103 sys.exit(1) | |
104 db2env(sys.argv[1], sys.argv[2]) | |
105 print >> sys.stderr, 'Environment successfully created.' |