forked from wmcbrine/pytivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyTivo.py
executable file
·99 lines (80 loc) · 2.72 KB
/
pyTivo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
import logging
import os
import platform
import sys
import time
if sys.version_info[0] != 2 or sys.version_info[1] < 4:
print ('ERROR: pyTivo requires Python >= 2.4, < 3.0.\n')
sys.exit(1)
import beacon
import config
import httpserver
from plugin import GetPlugin
def exceptionLogger(*args):
sys.excepthook = sys.__excepthook__
logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
def last_date():
lasttime = -1
path = os.path.dirname(__file__)
if not path:
path = '.'
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith('.py'):
tm = os.stat(os.path.join(root, name)).st_mtime
if tm > lasttime:
lasttime = tm
return time.asctime(time.localtime(lasttime))
def setup(in_service=False):
config.init(sys.argv[1:])
config.init_logging()
sys.excepthook = exceptionLogger
port = config.getPort()
httpd = httpserver.TivoHTTPServer(('', int(port)),
httpserver.TivoHTTPHandler)
logger = logging.getLogger('pyTivo')
logger.info('Last modified: ' + last_date())
logger.info('Python: ' + platform.python_version())
logger.info('System: ' + platform.platform())
for section, settings in config.getShares():
httpd.add_container(section, settings)
# Precaching of files: does a recursive list of base path
if settings.get('precache', 'False').lower() == 'true':
plugin = GetPlugin(settings.get('type'))
if hasattr(plugin, 'pre_cache'):
logger.info('Pre-caching the ' + section + ' share.')
pre_cache_filter = getattr(plugin, 'pre_cache')
def build_recursive_list(path):
try:
for f in os.listdir(path):
f = os.path.join(path, f)
if os.path.isdir(f):
build_recursive_list(f)
else:
pre_cache_filter(f)
except:
pass
build_recursive_list(settings.get('path'))
b = beacon.Beacon()
b.add_service('TiVoMediaServer:%s/http' % port)
b.start()
if 'listen' in config.getBeaconAddresses():
b.listen()
httpd.set_beacon(b)
httpd.set_service_status(in_service)
logger.info('pyTivo is ready.')
return httpd
def serve(httpd):
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
def mainloop():
httpd = setup()
serve(httpd)
httpd.beacon.stop()
return httpd.restart
if __name__ == '__main__':
while mainloop():
time.sleep(5)