2
# -*- coding: utf-8 -*-
5
""" upgrades de gozerdata directory """
7
__copyright__ = 'this file is in the public domain'
9
import sys, os, tempfile, types
11
sys.path.insert(0, os.getcwd())
13
from optparse import OptionParser
16
parser = OptionParser(usage='usage: %prog [options]', version='%prog ' + gozerbot.__version__)
17
parser.add_option('-p', '--plug', type='string', default=False, dest='plug',
18
help="provide plugin to upgrade")
19
parser.add_option('-l', '--logs', action="store_true", default=False, dest='logs',
21
help="copy over the logfiles")
23
opts, args = parser.parse_args()
26
from gozerbot.utils.exception import handle_exception
27
from gozerbot.plugins import plugins
28
from gozerbot.datadir import datadir
31
def upgradeplug(plugin):
32
if not os.path.exists(datadir + os.sep + 'plugs'):
33
os.mkdir(datadir + os.sep + 'plugs')
34
if not os.path.exists(datadir + os.sep + 'plugs' + os.sep + plugin):
35
os.mkdir(datadir + os.sep + 'plugs' + os.sep + plugin)
36
plugins.reload('gozerplugs', plugin, enable=False)
38
plugins.plugs[plugin].upgrade()
39
except AttributeError:
44
print "UPGRADING %s" % plugin
45
from gozerbot.database.alchemy import startmaindb
55
print "gozerbot-upgrade <old botdir> <new botdir>"
58
sourcedata = source + os.sep + 'gozerdata'
59
targetdata = target + os.sep + 'gozerdata'
63
import gozerbot.config
64
oldloglevel = gozerbot.config.config['loglevel']
65
gozerbot.config.config.set('loglevel', 100)
66
from gozerbot.utils.exception import handle_exception
67
from gozerbot.datadir import makedirs
68
from gozerbot.persist.persist import Persist
69
from gozerbot.utils.log import rlog
70
from gozerbot.database.db import Db
71
from gozerbot.config import Config, fleetbotconfigtxt, mainconfigtxt
72
from gozerbot.plugins import plugins
73
from gozerbot.gozerimport import gozer_import
74
from gozerbot.threads.thr import start_new_thread
75
import re, os, sys, shutil, thread, time
77
from gozerbot.utils.popen import gozerpopen
78
from gozerbot.utils.generic import gethighest, dosed, copyfile, convertpickle
81
def upgrade(source, target):
83
shutil.copytree(source, target)
86
print "can't copy %s to %s" % (source, target)
89
for f in os.listdir(target):
90
if not os.path.isdir(f) and not f.endswith('.db'):
91
dosed(target + os.sep + f, 's/cgozerbot\./cgozerbot\.compat\./')
92
dosed(target + os.sep + f, 's/myplugs\./gozerbot\.compat\./')
93
dosed(target + os.sep + f, 's/cgozerplugs\.plugs\./cgozerbot\.compat\./')
94
dosed(target + os.sep + f, 's/cgozerplugs\./cgozerbot\.compat\./')
95
dosed(target + os.sep + f, 's/gozerbot\.compat\.persist\./gozerbot\.compat\./')
97
print "COPIED %s" % ' .. '.join(copied)
100
tmpdir = tempfile.mkdtemp()
103
print "doing 0.8 upgrade"
104
print "DOING THE SED"
105
import gozerbot.compat.persist
106
import gozerbot.compat.config
108
upgrade(sourcedata, tmpdir + os.sep + 'olddata')
109
origsource = sourcedata
110
sourcedata = tmpdir + os.sep + 'olddata'
111
ddir = tmpdir + os.sep + 'gozerdata'
112
plugsdir = ddir + os.sep + 'plugs'
113
if not os.path.isdir(plugsdir):
115
print "CONVERTING MAIN CONFIG FILE"
116
ddd = ddir + os.sep + 'plugs' + os.sep + 'udp'
118
if not os.path.isdir(ddd):
121
print "can't make udp plug dir"
123
ddd = ddir + os.sep + 'plugs' + os.sep + 'tcp'
125
if not os.path.isdir(ddd):
128
print "can't make tcp plug dir"
130
cfg = gozerbot.compat.config.Config(sourcedata)
132
newcfg = Config(ddir, 'mainconfig', mainconfigtxt)
133
print "converting from %s to %s" % (cfg['version'], newcfg['version'])
134
udpcfg = Config(ddir + os.sep + 'plugs' + os.sep + 'udp')
135
tcpcfg = Config(ddir + os.sep + 'plugs' + os.sep + 'tcp')
136
if type(cfg['owneruserhost']) == types.ListType:
137
newcfg['owner'] = cfg['owneruserhost']
140
newcfg['owner'].append("%s" % cfg['owneruserhost'])
141
if type(cfg['jabberowner']) == types.ListType:
142
newcfg['owner'] += cfg['jabberowner']
144
newcfg['owner'].append(cfg['jabberowner'])
146
newcfg['owner'].remove('bartholo@localhost')
150
newcfg['owner'].remove('dunker@jabber.xs4all.nl')
154
newcfg['loglevel'] = int(cfg['loglevel'])
155
except (ValueError, TypeError):
156
newcfg['loglevel'] = cfg['loglevel']
157
newcfg['quitmsg'] = cfg['quitmsg']
158
newcfg['mask'] = cfg['umask'] or cfg['mask']
159
newcfg['mask'] = str(newcfg['mask'])
160
newcfg['debug'] = cfg['debug'] or 0
162
if not cfg.has_key('dbenable') or not cfg.has_key('dbtype'):
163
newcfg['dbtype'] = 'mysql'
164
elif not cfg['dbenable']:
165
newcfg['dbtype'] = 'sqlite'
166
newcfg['dbname'] = 'db/main.db'
168
newcfg['dbtype'] = cfg['dbtype'] or 'mysql'
169
newcfg['dbname'] = cfg['dbname']
170
newcfg['dbhost'] = cfg['dbhost']
171
newcfg['dbuser'] = cfg['dbuser']
172
newcfg['dbpasswd'] = cfg['dbpasswd']
173
print 'using dbtype: %s' % newcfg['dbtype']
175
newcfg['stripident'] = cfg['stripident']
179
newcfg['bindhost'] = cfg['bindhost']
182
for key, value in cfg.iteritems():
183
if key.startswith('udp'):
185
udpcfg[key] = int(value)
186
except (ValueError, TypeError):
188
elif key.startswith('tcp'):
190
if not udpcfg.has_key('udpbot'):
191
udpcfg['udpbot'] = "main"
192
if not udpcfg.has_key('udpseed'):
193
udpcfg['udpseed'] = ""
194
udpcfg['udpparty'] = cfg['partyudp']
195
tcpcfg['tcpparty'] = cfg['partytcp']
197
from gozerbot.config import config
201
print "MAKING FLEET CONFIGS"
202
fleet = gozerbot.compat.persist.Persist(sourcedata + os.sep + 'fleet')
204
for name, data in fleet.data.iteritems():
205
ddd = ddir + os.sep + 'fleet' + os.sep + name
207
if not os.path.isdir(ddd):
209
fleetcfg = Config(ddir + os.sep + 'fleet' + os.sep + name, 'config', \
210
inittxt=fleetbotconfigtxt)
211
except Exception, ex:
214
fleetcfg['name'] = name
216
fleetcfg['type'] = data[0]
218
fleetcfg['type'] = 'irc'
219
if fleetcfg['type'] == 'jabber':
220
fleetcfg['type'] = 'xmpp'
222
fleetcfg['owner'] = data[1]
224
fleetcfg['owner'] = []
225
if fleetcfg['type'] == 'irc':
227
fleetcfg['nick'] = data[2]
229
fleetcfg['nick'] = 'gozerbot'
231
fleetcfg['server'] = data[3]
233
fleetcfg['server'] = 'localhost'
235
fleetcfg['port'] = int(data[4])
239
fleetcfg['password'] = data[5]
241
fleetcfg['password'] = ''
243
fleetcfg['ipv6'] = int(data[6])
247
fleetcfg['ssl'] = int(data[7])
252
fleetcfg['host'] = data[2]
254
fleetcfg['host'] = 'jabber.xs4all.nl'
255
fleetcfg['server'] = fleetcfg['host']
257
fleetcfg['user'] = data[3]
259
fleetcfg['user'] = 'botter@jabber.xs4all.nl'
261
fleetcfg['password'] = data[4]
263
fleetcfg['password'] = 'notset'
265
fleetcfg['port'] = int(data[5])
267
fleetcfg['port'] = 5222
269
print "saving %s fleet bot data" % fleetcfg['name']
271
except Exception, ex:
274
convertpickle(sourcedata + os.sep + '%s.channels' % fleetcfg['name'], ddd + os.sep + 'channels')
275
convertpickle(sourcedata + os.sep + '%s.userhosts' % fleetcfg['name'], ddd + os.sep + 'userhosts')
276
convertpickle(sourcedata + os.sep + '%s.state' % fleetcfg['name'], ddd + os.sep + 'state')
278
print "DISABLING DEFAULT BOTS"
280
ddd = ddir + os.sep + 'fleet' + os.sep + 'default'
281
if not os.path.isdir(ddd):
283
fleetcfg = Config(ddd, inittxt=fleetbotconfigtxt)
284
fleetcfg['enable'] = 0
289
ddd = ddir + os.sep + 'fleet' + os.sep + 'jabber'
290
if not os.path.isdir(ddd):
292
fleetcfg = Config(ddd, inittxt=fleetbotconfigtxt)
293
fleetcfg['enable'] = 0
298
print "COPY PLUGIN CONFIGS"
300
plugsdir = ddir + os.sep + 'plugs'
302
if not os.path.isdir(plugsdir):
305
import gozerbot.compat.persistconfig
307
for file in os.listdir(sourcedata):
308
if file.endswith('-config'):
310
p = gozerbot.compat.persist.Persist(sourcedata + os.sep + file)
311
except Exception, ex:
312
print "can't make config of %s: %s" % (file, str(ex))
315
print "can't make config of %s" % file
317
pdir = ddir + os.sep + 'plugs' + os.sep + file.split('-')[0]
318
plugcfg = Config(pdir, 'config')
319
for name, option in p.data.iteritems():
322
plugcfg[name] = int(option.value)
323
except (ValueError, TypeError):
324
plugcfg[name] = option.value
325
except Exception, ex:
328
plugcfg[name] = int(option)
329
except (ValueError, TypeError):
330
plugcfg[name] = option
331
except Exception, ex:
332
print "exception converting %s: %s/%s" % (file, name, option)
336
print "converted %s" % ' .. '.join(got)
339
if os.path.isfile(sourcedata + os.sep + 'db' + os.sep + 'users.db'):
340
for f in os.listdir(sourcedata + os.sep + 'db'):
342
shutil.copy(sourcedata + os.sep + 'db' + os.sep + f, ddir + os.sep + 'db' + os.sep + f)
343
except Exception, ex:
344
if 'directory' not in str(ex):
345
print "can't copy %s .. %s" % (f, str(ex))
348
print "copied over database files: %s" % ' .. '.join(got)
349
if not cfg['dbenable']:
351
import gozerbot.datadir
352
gozerbot.datadir.datadir = ddir
353
reload(gozerbot.database.db)
355
from gozerbot.compat.users import Users
356
u = Users(sourcedata + os.sep + 'users')
358
from gozerbot.database.alchemy import startmaindb
359
from gozerbot.users import DbUsers
360
users = DbUsers(ddir)
365
users.add(i.name, i.userhosts, i.perms)
366
except Exception, ex:
367
rlog(10, 'upgrade', "can't add user %s .. %s" % (i.name, str(ex)))
370
users.adduseremail(i.name, i.email)
375
users.adduserpermit(i.name, j)
380
users.adduserstatus(i.name, j)
384
print "added the following users: %s" % ' .. '.join(added)
386
print "UPGRADE DATABASE"
387
from gozerbot.database.alchemy import startmaindb, dbupgrade
389
startmaindb(ddir, newcfg)
391
except Exception, ex:
394
print "COPY OVER OLD FILES"
396
for f in os.listdir(sourcedata):
398
shutil.copy(sourcedata + os.sep + f, ddir + os.sep + 'old' + os.sep + f)
399
except Exception, ex:
400
if 'directory' not in str(ex):
401
print "can't copy %s .. %s" % (f, str(ex))
403
print "CHECKING FOR OLD DATADIRS"
404
if os.path.isdir(targetdata + '.old'):
405
newdir = gethighest(target, 'gozerdata.old')
407
newdir = 'gozerdata.old'
409
if os.path.isdir(targetdata):
410
print "MOVING %s TO %s" % (targetdata, target + os.sep + newdir)
411
os.rename(targetdata, target + os.sep + newdir)
413
print "COPYING RESULTS TO %s" % targetdata
414
gozerbot.datadir.datadir = targetdata
415
shutil.copytree(ddir, targetdata)
421
print "UPGRADING PLUGINS"
425
for f in gozerplugs.__all__:
427
print "upgrading %s" % f
428
#threads.append(start_new_thread(upgradeplug, (f, )))
431
except AttributeError:
433
except Exception, ex:
435
#for thread in threads:
437
print "upgraded: %s" % ' '.join(plugdone)
440
print "doing 0.9 upgrade"
441
print "CHECKING FOR OLD DATADIRS"
442
if os.path.isdir(targetdata + '.old'):
443
newdir = gethighest(target, 'gozerdata.old')
445
newdir = 'gozerdata.old'
447
if os.path.isdir(targetdata):
448
print "MOVING %s TO %s" % (targetdata, target + os.sep + newdir)
449
os.rename(targetdata, target + os.sep + newdir)
450
print "COPYING RESULTS TO %s" % targetdata
451
gozerbot.datadir.datadir = targetdata
452
shutil.copytree(sourcedata, targetdata)
455
t = target + os.sep + 'logs'
456
print "CHECKING FOR OLD DATADIRS"
457
if os.path.isdir(target + os.sep + 'logs.old'):
458
newdir = gethighest(target, 'logs.old')
462
if os.path.isdir(target):
463
print "MOVING %s TO %s" % (t, target + os.sep + newdir)
464
os.rename(t, target + os.sep + newdir)
465
print "COPYING LOGFILEs TO %s" % t
466
shutil.copytree(source + os.sep + 'logs', t)
469
if not os.path.isdir(source + os.sep + 'myplugs'):
470
print "no myplugs dir found"
472
t = target + os.sep + 'myplugs'
473
if not os.path.isdir(t):
475
print "CHECKING FOR OLD MYPLUGS DIRS"
476
if os.path.isdir(target + os.sep + 'myplugs.old'):
477
newdir = gethighest(target, 'myplugs.old')
479
newdir = 'myplugs.old'
482
print "MOVING %s TO %s" % (t, target + os.sep + newdir)
483
os.rename(t, target + os.sep + newdir)
484
print "COPYING MYPLUGS TO %s" % t
485
shutil.copytree(source + os.sep + 'myplugs', t)
488
print 'CREATING DIRECTORIES'
489
makedirs(tmpdir + os.sep + 'gozerdata')
490
if os.path.isdir(sourcedata + os.sep + 'fleet'):
491
print "O.9 datadir detected"
500
print "REMOVING TMP DIR"
501
shutil.rmtree(tmpdir)