Of course I can't leave well enough alone.
[pyTivo/wmcbrine.git] / pyTivo.py
blob38a071457dcc500bf3507399d1da7888b5ed5cf4
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
16 from plugin import GetPlugin
18 def exceptionLogger(*args):
19 sys.excepthook = sys.__excepthook__
20 logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
22 def last_date():
23 lasttime = -1
24 path = os.path.dirname(__file__)
25 if not path:
26 path = '.'
27 for root, dirs, files in os.walk(path):
28 for name in files:
29 if name.endswith('.py'):
30 tm = os.stat(os.path.join(root, name)).st_mtime
31 if tm > lasttime:
32 lasttime = tm
34 return time.asctime(time.localtime(lasttime))
36 def setup(in_service=False):
37 config.init(sys.argv[1:])
38 config.init_logging()
39 sys.excepthook = exceptionLogger
41 port = config.getPort()
43 httpd = httpserver.TivoHTTPServer(('', int(port)),
44 httpserver.TivoHTTPHandler)
46 logger = logging.getLogger('pyTivo')
47 logger.info('Last modified: ' + last_date())
48 logger.info('Python: ' + platform.python_version())
49 logger.info('System: ' + platform.platform())
51 for section, settings in config.getShares():
52 httpd.add_container(section, settings)
54 b = beacon.Beacon()
55 b.add_service('TiVoMediaServer:%s/http' % port)
56 b.start()
57 if 'listen' in config.getBeaconAddresses():
58 b.listen()
60 httpd.set_beacon(b)
61 httpd.set_service_status(in_service)
63 logger.info('pyTivo is ready.')
64 return httpd
66 def serve(httpd):
67 try:
68 httpd.serve_forever()
69 except KeyboardInterrupt:
70 pass
72 def mainloop():
73 httpd = setup()
74 serve(httpd)
75 httpd.beacon.stop()
76 return httpd.restart
78 if __name__ == '__main__':
79 while mainloop():
80 time.sleep(5)