Merge commit 'wmcbrine/master'
[pyTivo/TheBayer.git] / pyTivoService.py
blobfe37d0dc75856575dc35e39edab6926340d8f0fb
1 import os
2 import select
3 import sys
4 import win32event
5 import win32service
6 import win32serviceutil
8 import beacon
9 import config
10 import httpserver
12 class PyTivoService(win32serviceutil.ServiceFramework):
13 _svc_name_ = 'pyTivo'
14 _svc_display_name_ = 'pyTivo'
16 def __init__(self, args):
17 win32serviceutil.ServiceFramework.__init__(self, args)
18 self.stop_event = win32event.CreateEvent(None, 0, 0, None)
20 def SvcDoRun(self):
21 config.init([])
22 config.init_logging()
24 p = os.path.dirname(__file__)
26 f = open(os.path.join(p, 'log.txt'), 'w')
27 sys.stdout = f
28 sys.stderr = f
30 port = config.getPort()
32 httpd = httpserver.TivoHTTPServer(('', int(port)),
33 httpserver.TivoHTTPHandler)
35 for section, settings in config.getShares():
36 httpd.add_container(section, settings)
38 b = beacon.Beacon()
39 b.add_service('TiVoMediaServer:%s/http' % port)
40 b.start()
41 if 'listen' in config.getBeaconAddresses():
42 b.listen()
44 httpd.set_beacon(b)
46 while 1:
47 sys.stdout.flush()
48 (rx, tx, er) = select.select((httpd,), (), (), 5)
49 for sck in rx:
50 sck.handle_request()
51 rc = win32event.WaitForSingleObject(self.stop_event, 5)
52 if rc == win32event.WAIT_OBJECT_0:
53 b.stop()
54 break
56 def SvcStop(self):
57 win32event.SetEvent(self.stop_event)
59 if __name__ == '__main__':
60 win32serviceutil.HandleCommandLine(PyTivoService)