Bump to 0.8.24
[theonering] / src / theonering.py
1 #!/usr/bin/env python
2
3 """
4 Telepathy-TheOneRing - Telepathy plugin for GoogleVoice
5 Copyright (C) 2009  Ed Page eopage AT byu DOT net
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 """
21
22 import os
23 import sys
24 import signal
25 import logging
26 import logging.handlers
27 import gobject
28
29 import dbus.glib
30 import telepathy.utils as telepathy_utils
31
32 import util.linux as linux_utils
33 import util.go_utils as gobject_utils
34 import constants
35 import connection_manager
36
37
38 def run_theonering(persist):
39         linux_utils.set_process_name(constants.__app_name__)
40
41         try:
42                 os.makedirs(constants._data_path_)
43         except OSError, e:
44                 if e.errno != 17:
45                         raise
46
47         @gobject_utils.async
48         def on_quit():
49                 manager.quit()
50                 mainloop.quit()
51
52         def timeout_cb():
53                 if len(manager._connections) == 0:
54                         logging.info('No connection received - quitting')
55                         on_quit()
56                 return False
57
58         if persist:
59                 shutdown_callback = None
60         else:
61                 gobject_utils.timeout_add_seconds(
62                         connection_manager.TheOneRingConnectionManager.IDLE_TIMEOUT,
63                         timeout_cb
64                 )
65                 shutdown_callback = on_quit
66
67         signal.signal(signal.SIGTERM, lambda: on_quit)
68
69         try:
70                 manager = connection_manager.TheOneRingConnectionManager(shutdown_func=shutdown_callback)
71         except dbus.exceptions.NameExistsException:
72                 logging.warning('Failed to acquire bus name, connection manager already running?')
73                 sys.exit(1)
74
75         mainloop = gobject.MainLoop(is_running=True)
76
77         gobject.threads_init()
78         dbus.glib.init_threads()
79         while mainloop.is_running():
80                 try:
81                         mainloop.run()
82                 except KeyboardInterrupt:
83                         quit()
84
85
86 def main(logToFile):
87         try:
88                 os.makedirs(constants._data_path_)
89         except OSError, e:
90                 if e.errno != 17:
91                         raise
92
93         telepathy_utils.debug_divert_messages(os.getenv('THEONERING_LOGFILE'))
94         logFormat = '(%(asctime)s) %(levelname)-5s %(threadName)s.%(name)s: %(message)s'
95         logging.basicConfig(
96                 level=logging.DEBUG,
97                 format=logFormat,
98                 datefmt='%H:%M:%S',
99         )
100         logging.raiseExceptions = True # Getting funky shutdown behavior, checking it out
101         rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1)
102         rotating.setFormatter(logging.Formatter(logFormat))
103         root = logging.getLogger()
104         root.addHandler(rotating)
105         logging.info("telepathy-theonering %s-%s" % (constants.__version__, constants.__build__))
106         logging.debug("OS: %s" % (os.uname()[0], ))
107         logging.debug("Kernel: %s (%s) for %s" % os.uname()[2:])
108         logging.debug("Hostname: %s" % os.uname()[1])
109
110         persist = 'THEONERING_PERSIST' in os.environ
111
112         try:
113                 run_theonering(persist)
114         finally:
115                 logging.shutdown()
116
117
118 if __name__ == "__main__":
119         main(False)