forked from futurice/futurice-ldap-user-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py
executable file
·87 lines (74 loc) · 3 KB
/
watcher.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
#! /usr/bin/env python
from django.conf import settings
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import datetime
import time, logging, subprocess, os
logging.basicConfig(level=logging.INFO)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fum.settings.base")
BLACKLIST = ['/.git','.pyc','dutils.conf.urls.js','.git','.log','dist/','logs/','/tests','.db', settings.MEDIA_ROOT.rstrip('/'), settings.STATIC_ROOT.rstrip('/'),]
def blacklisted(path):
return any(black in path for black in BLACKLIST)
def update_urls():
command = 'python manage.py js_urls'
try:
output = subprocess.check_output(command.split())
except Exception, e:
print e
LAST_RUN = datetime.datetime.now()
def is_time_to_run_again(interval=2):
global LAST_RUN
now = datetime.datetime.now()
diff = (now-LAST_RUN).total_seconds()
r = diff > interval
if r:
LAST_RUN = now
return r
class CollectStaticHandler(FileSystemEventHandler):
def prepare_system(self, event):
if event.src_path and not blacklisted(event.src_path):
if not is_time_to_run_again() and 'WARMUP' not in event.src_path:
return
print ":>", event.src_path
update_urls()
command = 'assetgen --profile dev assetgen.yaml'
try:
output = subprocess.check_output(command.split())
except Exception, e:
print e
def on_moved(self, event):
what = 'directory' if event.is_directory else 'file'
self.prepare_system(event)
def on_created(self, event):
what = 'directory' if event.is_directory else 'file'
self.prepare_system(event)
def on_deleted(self, event):
what = 'directory' if event.is_directory else 'file'
self.prepare_system(event)
def on_modified(self, event):
what = 'directory' if event.is_directory else 'file'
self.prepare_system(event)
if __name__ == "__main__":
print "Watcher Online."
# Ensure we're not in an invalid state. assetgen keeps state in /tmp
# which tells it not to generate e.g. assets.json next time you run it.
# If you deleted assets.json, the werserver will serve errors and you
# won't be able to fix this even by cleaning and rebuilding the entire
# repository: because assetgen's /tmp state tells it not to rebuild
# assets.json.
#
# 'assetgen --nuke' would be better but it also deletes the work done by
# 'manage.py collectstatic'.
subprocess.check_call(['assetgen', '--force',
'--profile', 'dev', 'assetgen.yaml'])
observer = Observer()
observer.schedule(CollectStaticHandler(), path='.', recursive=True)
# run once, to catchup on any changes while offline
CollectStaticHandler().prepare_system( type('Event', (object,), {'src_path': 'INITIAL/WARMUP/REQUEST'}) )
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()