Merge branch 'master' of git://repo.or.cz/pyTivo/wmcbrine
[pyTivo/wgw.git] / pyTivoService.py
blob058c253bdb3017216ebdfc4d5bf54e82b0d98551
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
31 port = config.getPort()
33 httpd = httpserver.TivoHTTPServer(('', int(port)),
34 httpserver.TivoHTTPHandler)
36 for section, settings in config.getShares():
37 httpd.add_container(section, settings)
39 b = beacon.Beacon()
40 b.add_service('TiVoMediaServer:%s/http' % port)
41 b.start()
42 if 'listen' in config.getBeaconAddresses():
43 b.listen()
45 httpd.set_beacon(b)
47 while 1:
48 sys.stdout.flush()
49 (rx, tx, er) = select.select((httpd,), (), (), 5)
50 for sck in rx:
51 sck.handle_request()
52 rc = win32event.WaitForSingleObject(self.stop_event, 5)
53 if rc == win32event.WAIT_OBJECT_0:
54 b.stop()
55 break
57 def SvcStop(self):
58 win32event.SetEvent(self.stop_event)
60 if __name__ == '__main__':
61 win32serviceutil.HandleCommandLine(PyTivoService)