1 |
|
# w.c.s. - web application for online forms
|
2 |
|
# Copyright (C) 2005-2013 Entr'ouvert
|
3 |
|
#
|
4 |
|
# This program is free software; you can redistribute it and/or modify
|
5 |
|
# it under the terms of the GNU General Public License as published by
|
6 |
|
# the Free Software Foundation; either version 2 of the License, or
|
7 |
|
# (at your option) any later version.
|
8 |
|
#
|
9 |
|
# This program is distributed in the hope that it will be useful,
|
10 |
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
# GNU General Public License for more details.
|
13 |
|
#
|
14 |
|
# You should have received a copy of the GNU General Public License
|
15 |
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
16 |
|
|
17 |
|
import os
|
18 |
|
import sys
|
19 |
|
import traceback
|
20 |
|
|
21 |
|
|
22 |
|
num_columns = 0
|
23 |
|
try:
|
24 |
|
import curses
|
25 |
|
except ImportError:
|
26 |
|
curses = None
|
27 |
|
else:
|
28 |
|
try:
|
29 |
|
curses.setupterm()
|
30 |
|
num_columns = curses.tigetnum('cols')
|
31 |
|
except:
|
32 |
|
pass
|
33 |
|
|
34 |
|
|
35 |
|
from qommon.ctl import Command, make_option
|
36 |
|
|
37 |
|
def update_progress(progress):
|
38 |
|
if not num_columns:
|
39 |
|
return
|
40 |
|
sys.stdout.write('[%s] %s%%\r' % (
|
41 |
|
('#'*((num_columns-10)*progress/100)).ljust(num_columns-15), progress))
|
42 |
|
|
43 |
|
class CmdConvertToSql(Command):
|
44 |
|
name = 'convert-to-sql'
|
45 |
|
|
46 |
|
def __init__(self):
|
47 |
|
Command.__init__(self, [
|
48 |
|
make_option('--dbname', metavar='DATABASE', action='store',
|
49 |
|
dest='dbname'),
|
50 |
|
make_option('--user', metavar='USER', action='store',
|
51 |
|
dest='user'),
|
52 |
|
make_option('--password', metavar='PASSWORD', action='store',
|
53 |
|
dest='password'),
|
54 |
|
make_option('--host', metavar='HOSTNAME', action='store',
|
55 |
|
dest='host'),
|
56 |
|
make_option('--port', metavar='PORT', action='store',
|
57 |
|
dest='port')
|
58 |
|
])
|
59 |
|
|
60 |
|
def execute(self, base_options, sub_options, args):
|
61 |
|
import publisher
|
62 |
|
|
63 |
|
self.config.remove_option('main', 'error_log')
|
64 |
|
publisher.WcsPublisher.configure(self.config)
|
65 |
|
pub = publisher.WcsPublisher.create_publisher(
|
66 |
|
register_tld_names=False)
|
67 |
|
|
68 |
|
hostname = args[0]
|
69 |
|
pub.app_dir = os.path.join(pub.app_dir, hostname)
|
70 |
|
pub.set_config()
|
71 |
|
|
72 |
|
from wcs.formdef import FormDef
|
73 |
|
from wcs import sql
|
74 |
|
|
75 |
|
if sub_options.port:
|
76 |
|
sub_options.port = int(sub_options.port)
|
77 |
|
|
78 |
|
pub.cfg['postgresql'] = {
|
79 |
|
'database': sub_options.dbname,
|
80 |
|
'user': sub_options.user,
|
81 |
|
'password': sub_options.password,
|
82 |
|
'host': sub_options.host,
|
83 |
|
'port': sub_options.port,
|
84 |
|
}
|
85 |
|
|
86 |
|
sql.get_connection_and_cursor(new=True)
|
87 |
|
|
88 |
|
errors = []
|
89 |
|
|
90 |
|
print 'converting users'
|
91 |
|
from users import User
|
92 |
|
sql.do_user_table()
|
93 |
|
count = User.count()
|
94 |
|
for i, user_id in enumerate(User.keys()):
|
95 |
|
user = User.get(user_id)
|
96 |
|
user.__class__ = sql.SqlUser
|
97 |
|
try:
|
98 |
|
user.store()
|
99 |
|
except AssertionError:
|
100 |
|
errors.append((user, traceback.format_exc()))
|
101 |
|
update_progress(100*i/count)
|
102 |
|
sql.SqlUser.fix_sequences()
|
103 |
|
|
104 |
|
if errors:
|
105 |
|
error_log = file('error_user.log', 'w')
|
106 |
|
for user, trace in errors:
|
107 |
|
print >> error_log, 'user_id', user.id
|
108 |
|
print >> error_log, trace
|
109 |
|
print >> error_log, '-'*80
|
110 |
|
error_log.close()
|
111 |
|
print 'There were some errors, see error_user.log for details.'
|
112 |
|
|
113 |
|
errors = []
|
114 |
|
for formdef in FormDef.select():
|
115 |
|
print ('converting %s' % formdef.name).ljust(num_columns-1)
|
116 |
|
sql.do_formdef_tables(formdef, rebuild_views=True,
|
117 |
|
rebuild_global_views=True)
|
118 |
|
data_class = formdef.data_class(mode='files')
|
119 |
|
count = data_class.count()
|
120 |
|
|
121 |
|
# load all objects a first time, to allow the migrate() code to be
|
122 |
|
# run and the eventual changes properly saved.
|
123 |
|
for id in data_class.keys():
|
124 |
|
formdata = data_class.get(id)
|
125 |
|
delattr(sys.modules['formdef'], formdef.url_name.title())
|
126 |
|
|
127 |
|
# once this is done, reload and store everything in postgresql
|
128 |
|
sql_data_class = formdef.data_class(mode='sql')
|
129 |
|
for i, id in enumerate(data_class.keys()):
|
130 |
|
formdata = data_class.get(id)
|
131 |
|
formdata._formdef = formdef
|
132 |
|
formdata._evolution = formdata.evolution
|
133 |
|
formdata.__class__ = sql_data_class
|
134 |
|
try:
|
135 |
|
formdata.store()
|
136 |
|
except AssertionError:
|
137 |
|
errors.append((formdata, traceback.format_exc()))
|
138 |
|
update_progress(100*i/count)
|
139 |
|
sql_data_class.fix_sequences()
|
140 |
|
|
141 |
|
print 'done'.ljust(num_columns-1)
|
142 |
|
|
143 |
|
sql.do_tracking_code_table()
|
144 |
|
sql.do_session_table()
|
145 |
|
sql.do_meta_table()
|
146 |
|
|
147 |
|
if errors:
|
148 |
|
error_log = file('error_formdata.log', 'w')
|
149 |
|
for formdata, trace in errors:
|
150 |
|
print >> error_log, formdata.formdef, formdata.id
|
151 |
|
print >> error_log, trace
|
152 |
|
print >> error_log, '-'*80
|
153 |
|
error_log.close()
|
154 |
|
print 'There were some errors, see error_formdata.log for details.'
|
155 |
|
|
156 |
|
if not pub.has_site_option('postgresql'):
|
157 |
|
print 'You still have to edit your site-options.cfg'
|
158 |
|
|
159 |
|
pub.write_cfg()
|
160 |
|
|
161 |
|
|
162 |
|
CmdConvertToSql.register()
|
163 |
|
-
|