Files
tryton/trytond/monitor.py
T

84 lines
2.4 KiB
Python
Raw Normal View History

2009-08-07 10:42:23 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import sys
import os
import subprocess
from threading import Lock
from trytond.modules import get_module_list
2010-02-18 18:50:35 +01:00
_LOCK = Lock()
_TIMES = {}
_MODULES = None
2009-08-07 10:42:23 +02:00
2012-04-01 23:08:15 +02:00
2009-08-07 10:42:23 +02:00
def _modified(path):
2010-02-18 18:50:35 +01:00
_LOCK.acquire()
2009-08-07 10:42:23 +02:00
try:
try:
if not os.path.isfile(path):
2010-02-18 18:50:35 +01:00
return path in _TIMES
2009-08-07 10:42:23 +02:00
mtime = os.stat(path).st_mtime
2010-02-18 18:50:35 +01:00
if path not in _TIMES:
_TIMES[path] = mtime
2009-08-07 10:42:23 +02:00
2010-02-18 18:50:35 +01:00
if mtime != _TIMES[path]:
_TIMES[path] = mtime
2009-08-07 10:42:23 +02:00
return True
2010-07-08 19:53:39 +02:00
except Exception:
2009-08-07 10:42:23 +02:00
return True
finally:
2010-02-18 18:50:35 +01:00
_LOCK.release()
2009-08-07 10:42:23 +02:00
return False
2012-04-01 23:08:15 +02:00
2009-08-07 10:42:23 +02:00
def monitor():
'''
Monitor module files for change
:return: True if at least one file has changed
'''
2010-02-18 18:50:35 +01:00
global _MODULES
2009-08-07 10:42:23 +02:00
modified = False
2012-09-26 19:38:20 +02:00
directories = set()
2009-08-07 10:42:23 +02:00
for module in sys.modules.keys():
if not module.startswith('trytond.'):
continue
if not hasattr(sys.modules[module], '__file__'):
continue
path = getattr(sys.modules[module], '__file__')
if not path:
continue
if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
path = path[:-1]
if _modified(path):
2009-10-10 13:44:21 +02:00
if subprocess.call((sys.executable, '-c', 'import %s' % module),
cwd=os.path.dirname(os.path.abspath(os.path.normpath(
os.path.join(__file__, '..'))))):
2009-08-07 10:42:23 +02:00
modified = False
break
modified = True
2012-09-26 19:38:20 +02:00
# Check view XML
directory = os.path.dirname(path)
if directory not in directories:
directories.add(directory)
view_dir = os.path.join(directory, 'view')
if os.path.isdir(view_dir):
for view in os.listdir(view_dir):
view = os.path.join(view_dir, view)
if os.path.splitext(view)[1] == '.xml':
if _modified(view):
modified = True
2009-08-07 10:42:23 +02:00
modules = set(get_module_list())
2010-02-18 18:50:35 +01:00
if _MODULES is None:
_MODULES = modules
for module in modules.difference(_MODULES):
2009-08-07 10:42:23 +02:00
if subprocess.call((sys.executable, '-c',
2013-02-27 22:50:31 +01:00
'import trytond.modules.%s' % module)):
2009-08-07 10:42:23 +02:00
modified = False
break
modified = True
2010-02-18 18:50:35 +01:00
_MODULES = modules
2009-08-07 10:42:23 +02:00
return modified