-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
114 lines (97 loc) · 3.86 KB
/
monitor.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Python Module Reloader
#
# Copyright (c) 2009, 2010 Jon Parise <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import imp
import os
try:
import queue
except ImportError:
#python 2.x
import Queue as queue
import reloader
import sys
import threading
import time
_win32 = (sys.platform == 'win32')
def _normalize_filename(filename):
if filename is not None:
if filename.endswith('.pyc') or filename.endswith('.pyo'):
filename = filename[:-1]
elif filename.endswith('$py.class'):
filename = filename[:-9] + '.py'
return filename
class ModuleMonitor(threading.Thread):
"""Monitor module source file changes"""
def __init__(self, interval=1):
threading.Thread.__init__(self)
self.daemon = True
self.mtimes = {}
self.queue = queue.Queue()
self.interval = interval
def run(self):
while True:
self._scan()
time.sleep(self.interval)
def _scan(self):
# We're only interested in file-based modules (not C extensions).
modules = [m.__file__ for m in sys.modules.values()
if m and '__file__' in m.__dict__]
for filename in modules:
# We're only interested in the source .py files.
filename = _normalize_filename(filename)
# stat() the file. This might fail if the module is part of a
# bundle (.egg). We simply skip those modules because they're
# not really reloadable anyway.
try:
stat = os.stat(filename)
except OSError:
continue
# Check the modification time. We need to adjust on Windows.
mtime = stat.st_mtime
if _win32:
mtime -= stat.st_ctime
# Check if we've seen this file before. We don't need to do
# anything for new files.
if filename in self.mtimes:
# If this file's mtime has changed, queue it for reload.
if mtime != self.mtimes[filename]:
self.queue.put(filename)
# Record this filename's current mtime.
self.mtimes[filename] = mtime
class Reloader(object):
def __init__(self, interval=1):
self.monitor = ModuleMonitor(interval=interval)
self.monitor.start()
def poll(self):
filenames = set()
while not self.monitor.queue.empty():
try:
filenames.add(self.monitor.queue.get_nowait())
except queue.Empty:
break
if filenames:
print("reloading %s" %filenames)
self._reload(filenames)
def _reload(self, filenames):
modules = [m for m in sys.modules.values()
if _normalize_filename(getattr(m, '__file__', None)) in filenames]
for mod in modules:
reloader.reload(mod)