Latest version of Cheetah -- 2.0.1.
[pyTivo/wmcbrine.git] / pyTivoService.py
blob761176b434849df6ce894207a18f8c5ed6e7e839
1 import select
2 import sys
3 import win32event
4 import win32service
5 import win32serviceutil
7 import beacon
8 import config
9 import httpserver
11 class PyTivoService(win32serviceutil.ServiceFramework):
12 _svc_name_ = 'pyTivo'
13 _svc_display_name_ = 'pyTivo'
15 def __init__(self, args):
16 win32serviceutil.ServiceFramework.__init__(self, args)
17 self.stop_event = win32event.CreateEvent(None, 0, 0, None)
19 def SvcDoRun(self):
21 import sys, os
23 config.init_logging()
25 p = os.path.dirname(__file__)
27 f = open(os.path.join(p, 'log.txt'), 'w')
28 sys.stdout = f
29 sys.stderr = f
32 port = config.getPort()
34 httpd = httpserver.TivoHTTPServer(('', int(port)),
35 httpserver.TivoHTTPHandler)
37 for section, settings in config.getShares():
38 httpd.add_container(section, settings)
40 b = beacon.Beacon()
41 b.add_service('TiVoMediaServer:%s/http' % port)
42 b.start()
43 if 'listen' in config.getBeaconAddresses():
44 b.listen()
46 httpd.set_beacon(b)
48 while 1:
49 sys.stdout.flush()
50 (rx, tx, er) = select.select((httpd,), (), (), 5)
51 for sck in rx:
52 sck.handle_request()
53 rc = win32event.WaitForSingleObject(self.stop_event, 5)
54 if rc == win32event.WAIT_OBJECT_0:
55 b.stop()
56 break
58 def SvcStop(self):
59 win32event.SetEvent(self.stop_event)
61 if __name__ == '__main__':
62 win32serviceutil.HandleCommandLine(PyTivoService)