Restart fails on my Mac, unless I do this.
[pyTivo/wmcbrine.git] / pyTivo.py
blobb9f6d57d84835d0f786b06734e64fffcf85e2045
1 #!/usr/bin/env python
3 import logging
4 import os
5 import platform
6 import sys
7 import time
9 if sys.version_info[0] != 2 or sys.version_info[1] < 4:
10 print ('ERROR: pyTivo requires Python >= 2.4, < 3.0.\n')
11 sys.exit(1)
13 import beacon
14 import config
15 import httpserver
17 def exceptionLogger(*args):
18 sys.excepthook = sys.__excepthook__
19 logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
21 def last_date():
22 lasttime = -1
23 path = os.path.dirname(__file__)
24 if not path:
25 path = '.'
26 for root, dirs, files in os.walk(path):
27 for name in files:
28 if name.endswith('.py'):
29 tm = os.stat(os.path.join(root, name)).st_mtime
30 if tm > lasttime:
31 lasttime = tm
33 return time.asctime(time.localtime(lasttime))
35 def setup(in_service=False):
36 config.init(sys.argv[1:])
37 config.init_logging()
38 sys.excepthook = exceptionLogger
40 port = config.getPort()
42 httpd = httpserver.TivoHTTPServer(('', int(port)),
43 httpserver.TivoHTTPHandler)
45 logger = logging.getLogger('pyTivo')
46 logger.info('Last modified: ' + last_date())
47 logger.info('Python: ' + platform.python_version())
48 logger.info('System: ' + platform.platform())
50 for section, settings in config.getShares():
51 httpd.add_container(section, settings)
53 b = beacon.Beacon()
54 b.add_service('TiVoMediaServer:%s/http' % port)
55 b.start()
56 if 'listen' in config.getBeaconAddresses():
57 b.listen()
59 httpd.set_beacon(b)
60 httpd.set_service_status(in_service)
62 logger.info('pyTivo is ready.')
63 return httpd
65 def serve(httpd):
66 try:
67 httpd.serve_forever()
68 except KeyboardInterrupt:
69 pass
71 def mainloop():
72 httpd = setup()
73 serve(httpd)
74 httpd.beacon.stop()
75 return httpd.restart
77 if __name__ == '__main__':
78 while mainloop():
79 time.sleep(5)