Files
tryton/trytond/monitor.py
T

68 lines
1.9 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
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
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
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
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',
'import trytond.modules.%s' % module)):
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