- fix a mispell bug
[telepathy.git] / telepathy-butterfly~
blob8272b4ec109f602a6d93a515afb271f2c910eb75
1 #!/usr/bin/python
3 # telepathy-butterfly - an MSN connection manager for Telepathy
5 # Copyright (C) 2006-2007 Ali Sabil <ali.sabil@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program 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
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 import gobject
22 import dbus.glib
23 import signal
24 import os
25 import sys
27 import logging
29 logging.basicConfig(level=logging.DEBUG)
31 from butterfly import ButterflyConnectionManager
32 from butterfly.util.decorator import async
34 logger = logging.getLogger('Butterfly')
36 IDLE_TIMEOUT = 5000
37 PROCESS_NAME = 'telepathy-butterfly'
39 def debug_divert_messages(filename):
40 """debug_divert_messages:
41 @filename: A file to which to divert stdout and stderr or None to do
42 nothing.
44 Open the given file for writing and duplicate its file descriptor to
45 be used for stdout and stderr. This has the effect of closing the previous
46 stdout and stderr, and sending all messages that would have gone there
47 to the given file instead.
49 By default the file is truncated and hence overwritten each time the
50 process is executed.
51 If the filename is prefixed with '+' then the file is not truncated and
52 output is added at the end of the file.
53 Passing None to this function is guaranteed to have no effect. This is
54 so you can call it with the recommended usage
55 debug_divert_messages (os.getenv(MYAPP_LOGFILE))
56 and it won't do anything if the environment variable is not set. """
58 # TODO: this function should move to telepathy-python at some point
60 if filename is None:
61 print "caca"
62 return
64 try:
65 if filename.startswith('+'):
66 logfile = open(filename[1:], 'a')
67 else:
68 logfile = open(filename, 'w')
69 except IOError, e:
70 print "Can't open logfile '%s' : '%s'" %(filename, e)
71 return
73 sys.stdout = logfile
74 sys.stderr = logfile
76 if __name__ == '__main__':
77 debug_divert_messages(os.getenv('BUTTERFLY_LOGFILE'))
79 try: # change process name for killall
80 import ctypes
81 libc = ctypes.CDLL('libc.so.6')
82 libc.prctl(15, PROCESS_NAME, 0, 0, 0)
83 except Exception, e:
84 logger.warning('Unable to set processName: %s" % e')
86 @async
87 def quit():
88 manager.quit()
89 mainloop.quit()
91 if 'BUTTERFLY_PERSIST' not in os.environ:
92 def timeout_cb():
93 if len(manager._connections) == 0:
94 logger.info('No connection received - quitting')
95 quit()
96 return False
97 gobject.timeout_add(IDLE_TIMEOUT, timeout_cb)
98 shutdown_callback = quit
99 else:
100 shutdown_callback = None
102 signal.signal(signal.SIGTERM, lambda : quit)
104 manager = ButterflyConnectionManager(shutdown_callback)
105 mainloop = gobject.MainLoop(is_running=True)
107 while mainloop.is_running():
108 try:
109 mainloop.run()
110 except KeyboardInterrupt:
111 quit()