cobalt.py

changeset 152
1b734faab67a
parent 146
c17b82b1f573
child 156
5747c959c293
equal deleted inserted replaced
151:e24793fae424 152:1b734faab67a
36 import configfile as ConfigFile 36 import configfile as ConfigFile
37 from configfile import Config 37 from configfile import Config
38 import hgpoll as HgPoll 38 import hgpoll as HgPoll
39 import bt as Bt 39 import bt as Bt
40 import irc as Irc 40 import irc as Irc
41 import rest as Rest 41 import rest
42 42
43 if __name__ != '__main__': 43 if __name__ != '__main__':
44 raise ImportError ('cobalt may not be imported as a module') 44 raise ImportError ('cobalt may not be imported as a module')
45
46 if sys.version_info < (3, 0):
47 print ('''cobalt requires Python 3 to run''', file=sys.stderr)
48 quit (1)
45 49
46 g_BotActive = False 50 g_BotActive = False
47 51
48 # 52 #
49 # Exception handling 53 # Exception handling
50 # 54 #
51 def handle_exception(excType, excValue, trace): 55 def handle_exception (excType, excValue, trace):
52 excepterm (traceback.format_exception(excType, excValue, trace)) 56 excepterm (traceback.format_exception(excType, excValue, trace))
53 57
54 def excepterm (data): 58 def excepterm (data):
55 for segment in data: 59 for segment in data:
56 for line in segment.splitlines(): 60 for line in segment.splitlines():
57 #print line 61 #print line
58 Irc.broadcast (line) 62 Irc.broadcast (line)
59 63
60 for client in Irc.all_clients: 64 for client in Irc.all_clients:
61 if len(data) > 0: 65 if data:
62 client.exceptdie() 66 client.exceptdie()
63 else: 67 else:
64 client.quit_irc() 68 client.quit_irc()
65 69
66 if g_BotActive: 70 if g_BotActive:
76 sys.excepthook = handle_exception 80 sys.excepthook = handle_exception
77 all_clients = [] 81 all_clients = []
78 g_BotActive = False 82 g_BotActive = False
79 83
80 try: 84 try:
81 uid = os.geteuid() 85 if os.geteuid() == 0 and input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
86 quit()
82 except: 87 except:
83 uid = -1 88 pass
84
85 if uid == 0 and raw_input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
86 quit()
87 89
88 ConfigFile.init() 90 ConfigFile.init()
89 ModuleCore.init_data() 91 ModuleCore.init_data()
90 Bt.init() 92 Bt.init()
91 HgPoll.init() 93 HgPoll.init()
102 if conndata.get_value ('name') == aconn: 104 if conndata.get_value ('name') == aconn:
103 Irc.irc_client (conndata, 0) 105 Irc.irc_client (conndata, 0)
104 break 106 break
105 else: 107 else:
106 raise ValueError ('unknown autoconnect entry "%s"' % (aconn)) 108 raise ValueError ('unknown autoconnect entry "%s"' % (aconn))
107 109
108 # Start the REST server
109 if Config.get_node ('rest').get_value ('active', True): 110 if Config.get_node ('rest').get_value ('active', True):
110 Rest.RESTServer() 111 rest.RestServer()
111 112
112 g_BotActive = True 113 g_BotActive = True
113 asyncore.loop() 114 asyncore.loop()
114 except KeyboardInterrupt: 115 except KeyboardInterrupt:
115 for client in Irc.all_clients: 116 for client in Irc.all_clients:

mercurial