2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2014-01-09 22:37:19 +01:00
|
|
|
from __future__ import absolute_import
|
2016-03-10 17:15:34 +01:00
|
|
|
from __future__ import print_function
|
2014-01-09 22:37:19 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import glob
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2014-01-09 23:14:47 +01:00
|
|
|
import time
|
2014-01-09 22:37:19 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
|
|
|
|
from django.conf import settings
|
|
|
|
|
2016-07-24 17:32:04 +02:00
|
|
|
from typing import Dict, List
|
|
|
|
|
2014-01-09 22:37:19 +01:00
|
|
|
os.chdir(settings.DEPLOY_ROOT)
|
|
|
|
STATIC_PATH = 'static/'
|
|
|
|
|
2014-01-09 23:14:47 +01:00
|
|
|
def get_templates():
|
2016-07-24 17:32:04 +02:00
|
|
|
# type: () -> List[str]
|
2016-10-05 01:36:26 +02:00
|
|
|
return (glob.glob(os.path.join(STATIC_PATH, 'templates/*.handlebars')) +
|
2016-12-11 14:30:45 +01:00
|
|
|
glob.glob(os.path.join(STATIC_PATH, 'templates/settings/*.handlebars')))
|
2014-01-09 23:14:47 +01:00
|
|
|
|
2014-01-09 22:37:19 +01:00
|
|
|
def run():
|
2016-07-24 17:32:04 +02:00
|
|
|
# type: () -> None
|
2016-11-25 09:08:38 +01:00
|
|
|
subprocess.check_call(['node', 'node_modules/.bin/handlebars']
|
2016-12-11 14:30:45 +01:00
|
|
|
+ get_templates()
|
|
|
|
+ ['--output', os.path.join(STATIC_PATH, 'templates/compiled.js'),
|
|
|
|
'--known', 'if,unless,each,with'])
|
2014-01-09 22:37:19 +01:00
|
|
|
|
2014-01-09 23:14:47 +01:00
|
|
|
def run_forever():
|
2016-07-24 17:32:04 +02:00
|
|
|
# type: () -> None
|
2014-01-09 23:14:47 +01:00
|
|
|
# Keep polling for file changes, similar to how Django does it in
|
|
|
|
# django/utils/autoreload.py. If any of our templates change, rebuild
|
|
|
|
# compiled.js
|
2016-07-24 17:32:04 +02:00
|
|
|
mtimes = {} # type: Dict[str, float]
|
2014-01-09 23:14:47 +01:00
|
|
|
while True:
|
|
|
|
changed = False
|
|
|
|
for fn in get_templates():
|
|
|
|
new_mtime = os.stat(fn).st_mtime
|
|
|
|
if new_mtime != mtimes.get(fn, None):
|
|
|
|
changed = True
|
|
|
|
mtimes[fn] = new_mtime
|
|
|
|
if changed:
|
2016-03-10 17:15:34 +01:00
|
|
|
print('Recompiling templates')
|
2014-01-09 23:14:47 +01:00
|
|
|
try:
|
|
|
|
run()
|
2016-03-10 17:15:34 +01:00
|
|
|
print('done')
|
2014-01-09 23:14:47 +01:00
|
|
|
except:
|
2016-03-10 17:15:34 +01:00
|
|
|
print('\n\n\nPLEASE FIX!!\n\n')
|
2014-01-09 23:14:47 +01:00
|
|
|
time.sleep(0.200)
|
|
|
|
|
2014-01-09 22:37:19 +01:00
|
|
|
if __name__ == '__main__':
|
2014-01-09 23:14:47 +01:00
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == 'forever':
|
2016-05-24 17:20:48 +02:00
|
|
|
try:
|
|
|
|
run_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print(sys.argv[0], "exited after receiving KeyboardInterrupt")
|
|
|
|
else:
|
|
|
|
run()
|