cobalt.py

Wed, 31 Dec 2014 11:40:46 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 31 Dec 2014 11:40:46 -0500
changeset 113
08e9b1c1b324
parent 96
24f0b13e87d3
child 124
7b2cd8b1ba86
permissions
-rwxr-xr-x

- added page system to prevent commands from printing too much output

0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 '''
25
9fa1df6113c4 - changed the copyright line to use my proper legal name instead of my common calling name
Santeri Piippo <crimsondusk64@gmail.com>
parents: 24
diff changeset
3 Copyright 2014 Teemu Piippo
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 All rights reserved.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 Redistribution and use in source and binary forms, with or without
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 modification, are permitted provided that the following conditions
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 are met:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 1. Redistributions of source code must retain the above copyright
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 notice, this list of conditions and the following disclaimer.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 2. Redistributions in binary form must reproduce the above copyright
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in the
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 documentation and/or other materials provided with the distribution.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 3. The name of the author may not be used to endorse or promote products
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 derived from this software without specific prior written permission.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 '''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import asyncore
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 import sys
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 import traceback
5
b6d2b7de0a6d - added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
33 import os
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 63
diff changeset
34 import modulecore as ModuleCore
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
35 import configfile as ConfigFile
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 63
diff changeset
36 from configfile import Config
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
37 import hgpoll as HgPoll
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
38 import bt as Bt
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
39 import irc as Irc
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
40 import rest as Rest
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
41
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
42 if __name__ != '__main__':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
43 raise ImportError ('cobalt may not be imported as a module')
20
2603faf5f91b - don't run as root without asking first, added more startup messages
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
44
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
45 g_BotActive = False
20
2603faf5f91b - don't run as root without asking first, added more startup messages
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
46
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
47 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
48 # Exception handling
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
49 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
50 def handle_exception(excType, excValue, trace):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
51 excepterm (traceback.format_exception(excType, excValue, trace))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
52
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
53 def excepterm (data):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
54 for segment in data:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
55 for line in segment.splitlines():
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 92
diff changeset
56 #print line
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
57 Irc.broadcast (line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
58
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
59 for client in Irc.all_clients:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
60 if len(data) > 0:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
61 client.exceptdie()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
62 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
63 client.quit_irc()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
64
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
65 if g_BotActive:
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
66 restart_self()
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
67 else:
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
68 quit()
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
69
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
70 def restart_self():
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
71 os.execl (sys.executable, sys.executable, * sys.argv)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
72
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
73 def main():
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
74 global g_BotActive
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
75 sys.excepthook = handle_exception
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
76 all_clients = []
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
77 g_BotActive = False
2
1a24dd2d598e - added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
78
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
79 try:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
80 uid = os.geteuid()
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
81 except:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
82 uid = -1
3
53486417a8e5 - handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
83
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
84 if uid == 0 and raw_input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
85 quit()
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 63
diff changeset
86
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
87 ConfigFile.init()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
88 ModuleCore.init_data()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
89 Bt.init()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
90 HgPoll.init()
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
91
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
92 try:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
93 autoconnects = Config.get_value ('autoconnect', [])
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
94
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
95 if len (autoconnects) == 0:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
96 print "Nowhere to connect."
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
97 quit()
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
98
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
99 for aconn in autoconnects:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
100 for conndata in Config.get_nodelist ('connections'):
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
101 if conndata.get_value ('name') == aconn:
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
102 Irc.irc_client (conndata, 0)
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
103 break
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
104 else:
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
105 raise ValueError ("unknown autoconnect entry %s" % (aconn))
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
106
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
107 # Start the REST server
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
108 Rest.RESTServer()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
109
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
110 g_BotActive = True
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
111 asyncore.loop()
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
112 except KeyboardInterrupt:
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
113 for client in Irc.all_clients:
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
114 client.keyboardinterrupt()
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
115 quit()
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
116 except Irc.RestartError as e:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 72
diff changeset
117 excepterm (e.message)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
72
2266d6d73de3 - commit work done on splitting bt/hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 67
diff changeset
119 if __name__ == '__main__':
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
120 main()

mercurial