~ubuntu-branches/ubuntu/quantal/gozerbot/quantal

« back to all changes in this revision

Viewing changes to build/scripts-2.5/gozerbot-upgrade

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2010-09-29 18:20:02 UTC
  • mfrom: (3.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100929182002-gi532gnem1vlu6jy
Tags: 0.9.1.3-5
Added python2.5 build dependency. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.5
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
 
 
5
""" upgrades de gozerdata directory """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
import sys, os, tempfile, types
 
10
 
 
11
sys.path.insert(0, os.getcwd())
 
12
 
 
13
from optparse import OptionParser
 
14
import gozerbot
 
15
 
 
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',
 
20
                  metavar='PATH',
 
21
                  help="copy over the logfiles")
 
22
 
 
23
opts, args = parser.parse_args()
 
24
opts.args = args
 
25
 
 
26
from gozerbot.utils.exception import handle_exception
 
27
from gozerbot.plugins import plugins
 
28
from gozerbot.datadir import datadir
 
29
import time
 
30
 
 
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)
 
37
    try:
 
38
        plugins.plugs[plugin].upgrade()
 
39
    except AttributeError:
 
40
        pass
 
41
 
 
42
plugin = opts.plug
 
43
if plugin:
 
44
    print "UPGRADING %s" % plugin
 
45
    from gozerbot.database.alchemy import startmaindb
 
46
    startmaindb()
 
47
    upgradeplug(plugin)
 
48
    print "done"
 
49
    os._exit(0)
 
50
 
 
51
try:
 
52
    source = sys.argv[1]
 
53
    target = sys.argv[2]
 
54
except IndexError:
 
55
    print "gozerbot-upgrade <old botdir> <new botdir>"
 
56
    os._exit(1)
 
57
 
 
58
sourcedata = source + os.sep + 'gozerdata'
 
59
targetdata = target + os.sep + 'gozerdata'
 
60
 
 
61
print "STARTING"
 
62
 
 
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
 
76
 
 
77
from gozerbot.utils.popen import gozerpopen
 
78
from gozerbot.utils.generic import gethighest, dosed, copyfile, convertpickle
 
79
copied = []
 
80
 
 
81
def upgrade(source, target):
 
82
    try:
 
83
        shutil.copytree(source, target)
 
84
    except OSError:
 
85
        handle_exception()
 
86
        print "can't copy %s to %s" % (source, target)
 
87
        os._exit(1)
 
88
    time.sleep(3)
 
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\./')
 
96
            copied.append(f)
 
97
    print "COPIED %s" % ' .. '.join(copied)
 
98
 
 
99
print "MAKE TMP DIR"
 
100
tmpdir = tempfile.mkdtemp()
 
101
 
 
102
def do08():
 
103
    print "doing 0.8 upgrade" 
 
104
    print "DOING THE SED"
 
105
    import gozerbot.compat.persist
 
106
    import gozerbot.compat.config
 
107
    global sourcedata
 
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):
 
114
        os.mkdir(plugsdir)
 
115
    print "CONVERTING MAIN CONFIG FILE" 
 
116
    ddd = ddir + os.sep + 'plugs' + os.sep + 'udp'
 
117
    try:
 
118
        if not os.path.isdir(ddd):
 
119
            os.mkdir(ddd)
 
120
    except:
 
121
        print "can't make udp plug dir"
 
122
        os._exit(1)
 
123
    ddd = ddir + os.sep + 'plugs' + os.sep + 'tcp'
 
124
    try:
 
125
        if not os.path.isdir(ddd):
 
126
            os.mkdir(ddd)
 
127
    except:
 
128
        print "can't make tcp plug dir"
 
129
        os._exit(1)
 
130
    cfg = gozerbot.compat.config.Config(sourcedata)
 
131
    cfg.reload()
 
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']
 
138
    else:
 
139
        newcfg['owner'] = []
 
140
        newcfg['owner'].append("%s" % cfg['owneruserhost'])
 
141
    if type(cfg['jabberowner']) == types.ListType:
 
142
        newcfg['owner'] += cfg['jabberowner']
 
143
    else:
 
144
        newcfg['owner'].append(cfg['jabberowner'])
 
145
    try:
 
146
        newcfg['owner'].remove('bartholo@localhost')
 
147
    except ValueError:
 
148
        pass
 
149
    try:
 
150
        newcfg['owner'].remove('dunker@jabber.xs4all.nl')
 
151
    except ValueError:
 
152
        pass
 
153
    try:
 
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
 
161
    newcfg['nodb'] = 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'
 
167
    else:
 
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']
 
174
    try:
 
175
        newcfg['stripident'] = cfg['stripident']
 
176
    except KeyError:
 
177
        pass
 
178
    try:
 
179
        newcfg['bindhost'] = cfg['bindhost']
 
180
    except KeyError:
 
181
        pass
 
182
    for key, value in cfg.iteritems():
 
183
        if key.startswith('udp'):
 
184
            try:
 
185
                udpcfg[key] = int(value)
 
186
            except (ValueError, TypeError):
 
187
                udpcfg[key] = value
 
188
        elif key.startswith('tcp'):
 
189
            tcpcfg[key] = value
 
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']
 
196
    newcfg.save()
 
197
    from gozerbot.config import config
 
198
    config.reload()
 
199
    udpcfg.save()
 
200
    tcpcfg.save()
 
201
    print "MAKING FLEET CONFIGS"
 
202
    fleet = gozerbot.compat.persist.Persist(sourcedata + os.sep + 'fleet')
 
203
    if fleet.data:
 
204
        for name, data in fleet.data.iteritems():
 
205
            ddd = ddir + os.sep + 'fleet' + os.sep + name
 
206
            try:
 
207
                if not os.path.isdir(ddd):
 
208
                    os.mkdir(ddd)
 
209
                fleetcfg = Config(ddir + os.sep + 'fleet' + os.sep + name, 'config', \
 
210
inittxt=fleetbotconfigtxt) 
 
211
            except Exception, ex:
 
212
                handle_exception()
 
213
                continue
 
214
            fleetcfg['name'] = name
 
215
            try:
 
216
                fleetcfg['type'] = data[0]
 
217
            except:
 
218
                fleetcfg['type'] = 'irc'
 
219
            if fleetcfg['type'] == 'jabber':
 
220
                fleetcfg['type'] = 'xmpp'
 
221
            try:
 
222
                fleetcfg['owner'] = data[1]
 
223
            except:
 
224
                fleetcfg['owner'] = []
 
225
            if fleetcfg['type'] == 'irc':
 
226
                try:
 
227
                    fleetcfg['nick'] = data[2]
 
228
                except:
 
229
                    fleetcfg['nick'] = 'gozerbot'
 
230
                try:
 
231
                    fleetcfg['server'] = data[3]
 
232
                except:
 
233
                    fleetcfg['server'] = 'localhost'
 
234
                try:
 
235
                    fleetcfg['port'] = int(data[4])
 
236
                except: 
 
237
                    fleetcfg['port'] = 0
 
238
                try:
 
239
                    fleetcfg['password'] = data[5]
 
240
                except:
 
241
                    fleetcfg['password'] = ''
 
242
                try:
 
243
                    fleetcfg['ipv6'] = int(data[6])
 
244
                except:
 
245
                    fleetcfg['ipv6'] = 0
 
246
                try:
 
247
                    fleetcfg['ssl'] = int(data[7])
 
248
                except:
 
249
                    fleetcfg['ssl'] = 0
 
250
            else:
 
251
                try:
 
252
                    fleetcfg['host'] = data[2]
 
253
                except:
 
254
                    fleetcfg['host'] = 'jabber.xs4all.nl'
 
255
                fleetcfg['server'] =  fleetcfg['host']
 
256
                try:
 
257
                    fleetcfg['user'] = data[3]
 
258
                except:
 
259
                    fleetcfg['user'] = 'botter@jabber.xs4all.nl'
 
260
                try:
 
261
                    fleetcfg['password'] = data[4]
 
262
                except: 
 
263
                    fleetcfg['password'] = 'notset'
 
264
                try:
 
265
                    fleetcfg['port'] = int(data[5])
 
266
                except:
 
267
                    fleetcfg['port'] = 5222
 
268
            try: 
 
269
                print "saving %s fleet bot data" % fleetcfg['name']
 
270
                fleetcfg.save()
 
271
            except Exception, ex:
 
272
                handle_exception() 
 
273
            
 
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')
 
277
 
 
278
    print "DISABLING DEFAULT BOTS"
 
279
    try:
 
280
        ddd = ddir + os.sep + 'fleet' + os.sep + 'default'
 
281
        if not os.path.isdir(ddd):
 
282
            os.mkdir(ddd)
 
283
        fleetcfg = Config(ddd, inittxt=fleetbotconfigtxt)
 
284
        fleetcfg['enable'] = 0
 
285
        fleetcfg.save()
 
286
    except:
 
287
        pass
 
288
    try:
 
289
        ddd = ddir + os.sep + 'fleet' + os.sep + 'jabber'
 
290
        if not os.path.isdir(ddd):
 
291
            os.mkdir(ddd)
 
292
        fleetcfg = Config(ddd, inittxt=fleetbotconfigtxt)
 
293
        fleetcfg['enable'] = 0
 
294
        fleetcfg.save()
 
295
    except:
 
296
        pass
 
297
 
 
298
    print "COPY PLUGIN CONFIGS"
 
299
 
 
300
    plugsdir = ddir + os.sep + 'plugs'
 
301
 
 
302
    if not os.path.isdir(plugsdir):
 
303
        os.mkdir(plugsdir)
 
304
 
 
305
    import gozerbot.compat.persistconfig
 
306
    got = []
 
307
    for file in os.listdir(sourcedata):
 
308
        if file.endswith('-config'):
 
309
            try:
 
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))
 
313
                continue
 
314
            if not p:
 
315
                print "can't make config of %s" % file
 
316
                continue
 
317
            pdir = ddir + os.sep + 'plugs' + os.sep + file.split('-')[0]
 
318
            plugcfg = Config(pdir, 'config')
 
319
            for name, option in p.data.iteritems():
 
320
                try:
 
321
                    try:
 
322
                        plugcfg[name] = int(option.value)
 
323
                    except (ValueError, TypeError):
 
324
                        plugcfg[name] = option.value
 
325
                except Exception, ex:
 
326
                    try:
 
327
                        try:
 
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)
 
333
            got.append(file)
 
334
            plugcfg.save()
 
335
    if got:
 
336
        print "converted %s" % ' .. '.join(got)
 
337
 
 
338
    got = []
 
339
    if os.path.isfile(sourcedata + os.sep + 'db' + os.sep + 'users.db'):
 
340
        for f in os.listdir(sourcedata + os.sep + 'db'):
 
341
            try:
 
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))
 
346
                    continue
 
347
            got.append(f)
 
348
        print "copied over database files: %s" % ' .. '.join(got)
 
349
    if not cfg['dbenable']:    
 
350
        print "ADDING USERS"
 
351
        import gozerbot.datadir
 
352
        gozerbot.datadir.datadir = ddir
 
353
        reload(gozerbot.database.db)
 
354
 
 
355
        from gozerbot.compat.users import Users
 
356
        u = Users(sourcedata + os.sep + 'users')
 
357
        assert(u)
 
358
        from gozerbot.database.alchemy import startmaindb
 
359
        from gozerbot.users import DbUsers
 
360
        users = DbUsers(ddir)
 
361
        startmaindb(ddir)
 
362
        added = []
 
363
        for i in u.data:
 
364
            try:
 
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)))
 
368
                continue
 
369
            try:
 
370
                users.adduseremail(i.name, i.email)
 
371
            except:
 
372
                pass
 
373
            for j in i.permit:
 
374
                try:
 
375
                    users.adduserpermit(i.name, j)
 
376
                except:
 
377
                    pass
 
378
            for j in i. status:
 
379
                try:
 
380
                    users.adduserstatus(i.name, j)
 
381
                except:
 
382
                    pass
 
383
            added.append(i.name)
 
384
        print "added the following users: %s" % ' .. '.join(added)
 
385
 
 
386
    print "UPGRADE DATABASE"
 
387
    from gozerbot.database.alchemy import startmaindb, dbupgrade
 
388
    try:
 
389
        startmaindb(ddir, newcfg)
 
390
        dbupgrade(newcfg)
 
391
    except Exception, ex:
 
392
        handle_exception()
 
393
 
 
394
    print "COPY OVER OLD FILES"
 
395
 
 
396
    for f in os.listdir(sourcedata):
 
397
        try:
 
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))
 
402
 
 
403
    print "CHECKING FOR OLD DATADIRS"
 
404
    if os.path.isdir(targetdata + '.old'):
 
405
        newdir = gethighest(target, 'gozerdata.old')
 
406
    else:
 
407
        newdir = 'gozerdata.old'
 
408
 
 
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)
 
412
 
 
413
    print "COPYING RESULTS TO %s" % targetdata
 
414
    gozerbot.datadir.datadir = targetdata
 
415
    shutil.copytree(ddir, targetdata)
 
416
    time.sleep(5)
 
417
    try:
 
418
        import gozerplugs
 
419
    except ImportError:
 
420
        return
 
421
    print "UPGRADING PLUGINS"
 
422
    startmaindb()
 
423
    plugdone = []
 
424
    threads = []
 
425
    for f in gozerplugs.__all__:
 
426
        try:
 
427
            print "upgrading %s" % f
 
428
            #threads.append(start_new_thread(upgradeplug, (f, )))
 
429
            upgradeplug(f)
 
430
            plugdone.append(f)
 
431
        except AttributeError:
 
432
            pass
 
433
        except Exception, ex:
 
434
            handle_exception()
 
435
    #for thread in threads:
 
436
    #    thread.join()
 
437
    print "upgraded: %s" % ' '.join(plugdone)
 
438
 
 
439
def do09():
 
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')
 
444
    else:
 
445
        newdir = 'gozerdata.old'
 
446
 
 
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)
 
453
 
 
454
def copylogfiles():
 
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')
 
459
    else:
 
460
        newdir = 'logs.old'
 
461
 
 
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)
 
467
 
 
468
def copymyplugs():
 
469
    if not os.path.isdir(source + os.sep + 'myplugs'):
 
470
        print "no myplugs dir found"
 
471
        return
 
472
    t = target + os.sep + 'myplugs'
 
473
    if not os.path.isdir(t):
 
474
        os.mkdir(t)
 
475
    print "CHECKING FOR OLD MYPLUGS DIRS"
 
476
    if os.path.isdir(target + os.sep + 'myplugs.old'):
 
477
        newdir = gethighest(target, 'myplugs.old')
 
478
    else:
 
479
        newdir = 'myplugs.old'
 
480
 
 
481
    if os.path.isdir(t):
 
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)
 
486
 
 
487
try:
 
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"
 
492
        do09()
 
493
        copymyplugs()
 
494
    else:
 
495
        do08()
 
496
    if opts.logs:
 
497
        copylogfiles()
 
498
 
 
499
finally:
 
500
    print "REMOVING TMP DIR"
 
501
    shutil.rmtree(tmpdir)
 
502
    
 
503
print "DONE"