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

« back to all changes in this revision

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

  • 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
#
 
3
#
 
4
 
 
5
""" exec gozerbot commands from the command line. """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# ==============
 
10
# IMPORT SECTION
 
11
 
 
12
# gozerbot imports
 
13
 
 
14
def showexceptions():
 
15
    teller = 0
 
16
 
 
17
    for ex in exceptionevents:
 
18
        teller += 1
 
19
        print "EXCEPTION NR %s" % teller
 
20
        print ex[1]
 
21
        print "command: "  + ex[0].txt
 
22
        print "options: " + str(ex[0].options)
 
23
 
 
24
    print "total of %s exceptions" % teller
 
25
    return exceptionlist
 
26
 
 
27
import sys, os, time
 
28
starttime = time.time()
 
29
sys.path.insert(0, os.getcwd())
 
30
 
 
31
from gozerbot.tests import tests
 
32
from gozerbot.utils.exception import handle_exception, exceptionlist, exceptionevents
 
33
from gozerbot.eventbase import EventBase
 
34
from gozerbot.botbase import BotBase
 
35
from gozerbot.users import users
 
36
from gozerbot.exit import globalshutdown
 
37
from gozerbot.eventhandler import mainhandler
 
38
from gozerbot.utils.log import enable_logging, disable_logging
 
39
from gozerbot.utils.generic import waitforqueue, makeoptions
 
40
from gozerbot.threads.thr import start_new_thread
 
41
from gozerbot.periodical import periodical
 
42
from gozerbot.plugins import plugins
 
43
from gozerbot.database.alchemy import startmaindb
 
44
from gozerbot.persist.persist import Persist
 
45
from gozerbot.commands import cmnds
 
46
 
 
47
import gozerbot
 
48
 
 
49
# basic imports
 
50
from optparse import OptionParser
 
51
import asyncore, time, Queue
 
52
 
 
53
# skip part after ##
 
54
index = 0
 
55
for i in sys.argv:
 
56
    if '//' not in i:
 
57
        index += 1
 
58
    else:
 
59
        break
 
60
    
 
61
cmndargs = sys.argv[1:index]
 
62
botargs = sys.argv[index+1:]
 
63
 
 
64
# END IMPORT
 
65
# ==========
 
66
 
 
67
parser = OptionParser(usage='usage: %prog [options]', version='%prog ' + gozerbot.__version__)
 
68
parser.add_option('-l', '--loglevel', type='string', default=False, dest='loglevel',
 
69
                  help="loglevel")
 
70
parser.add_option('-c', '--chan', type='string', default=False, dest='channel',
 
71
                  help="channel to use")
 
72
parser.add_option('-n', '--nick', type='string', default=False, dest='nick',
 
73
                  help="nick to use")
 
74
parser.add_option('-u', '--userhost', type='string', default=False, dest='userhost',
 
75
                  help="userhost to use")
 
76
parser.add_option('-e', '--exceptions', action="store_true", default=False, dest='exceptions',
 
77
                  help="show exceptions list after finishing the command")
 
78
parser.add_option('-w', '--wait', type="string", default=False, dest='wait',
 
79
                  help="nr of seconds to wait for completion")
 
80
parser.add_option('-v', '--verbose', action="store_true", default=False, dest='verbose',
 
81
                  help="be verbose")
 
82
parser.add_option('-x', '--loop', type="string", default=False, dest='loop',
 
83
                  help="nr times to loop")
 
84
parser.add_option('-b', '--bork', action='store_true', default=False,dest='bork', 
 
85
                  help="bork (exit) on exception")
 
86
 
 
87
opts, args = parser.parse_args(cmndargs)
 
88
opts.args = args
 
89
 
 
90
v = opts.verbose
 
91
 
 
92
try:
 
93
    cmnd = args[0]
 
94
except IndexError:
 
95
    print "what command?"
 
96
    os._exit(0)
 
97
 
 
98
if opts.bork:
 
99
    import gozerbot.utils.exception
 
100
    gozerbot.utils.exception.borkonexception = True
 
101
    print "BORK ON EXCEPTION ENABLED"
 
102
 
 
103
enable_logging(int(opts.loglevel) or 1000)
 
104
periodical.start()
 
105
startmaindb()
 
106
 
 
107
print "GOZERBOT %s starting" % gozerbot.__version__
 
108
print "loading plugins ..."
 
109
plugins.regplugins()
 
110
plugins.enableall()
 
111
plugins.regplugins()
 
112
 
 
113
print "done"
 
114
queue = Queue.Queue()
 
115
end = False
 
116
 
 
117
try:
 
118
    bot = BotBase('execbot', {'nick': opts.nick or 'exec'})
 
119
    event = EventBase()
 
120
    event.nick = opts.nick or 'exec'
 
121
    event.userhost = opts.userhost or 'exec@gozerbot'
 
122
 
 
123
    try:
 
124
        users.add(event.nick, [event.userhost,], ['USER', 'OPER'])
 
125
    except Exception, ex:
 
126
        pass
 
127
 
 
128
    event.txt = "!" + " ".join(args)
 
129
 
 
130
    if botargs:
 
131
         event.txt += ' ' + ' '.join(botargs)
 
132
 
 
133
    event.origtxt = event.txt
 
134
    event.bot = bot
 
135
    event.msg = True
 
136
    event.isdcc = True
 
137
    event.channel = opts.channel or "exec@gozerbot"
 
138
    event.queues.append(Queue.Queue())
 
139
    makeoptions(event)
 
140
    
 
141
    # GO !
 
142
 
 
143
    start_new_thread(bot.serveforever, ())
 
144
 
 
145
    if opts.loop:
 
146
        loop = int(opts.loop)
 
147
    else:
 
148
        loop = 1
 
149
    for i in range(loop):
 
150
        print "STARTING LOOP #%s" % i
 
151
        start_new_thread(bot.domsg, (event, ))
 
152
 
 
153
    if opts.wait:
 
154
        resultnotused = waitforqueue(queue, timeout=int(opts.wait))
 
155
    else:
 
156
        resultnotused = waitforqueue(queue, 3600)
 
157
 
 
158
    if opts.exceptions:
 
159
        showexceptions()
 
160
 
 
161
    if v: print "elapsed time: %s seconds" % (time.time() - starttime)
 
162
    sys.stdout.flush()
 
163
    time.sleep(0.01)
 
164
    os._exit(0)
 
165
 
 
166
except KeyboardInterrupt:
 
167
    showexceptions()
 
168
except Exception, ex:
 
169
    handle_exception()
 
170
    disable_logging()
 
171
    showexceptions()
 
172
    sys.stdout.flush()
 
173
    time.sleep(0.01)
 
174
    os._exit(1)
 
175
 
 
176
teller = 0
 
177
for where, test in tests.err.iteritems():
 
178
    teller += 1
 
179
    print "ERROR %s: %s" % (teller, test)
 
180
 
 
181
teller = 0
 
182
for toolate, sec in tests.toolate.iteritems():
 
183
    teller += 1
 
184
    print "TOOLATE %s: %s (%s)" % (teller, toolate, sec)
 
185