Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
837 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
|
||
import sys | ||
import os | ||
import types | ||
import threading | ||
import time | ||
|
||
from DIRAC import gLogger | ||
from DIRAC.Core.Utilities.DIRACSingleton import DIRACSingleton | ||
from DIRAC.ConfigurationSystem.Client.Helpers.CSGlobals import Extensions | ||
|
||
class Devloader( object ): | ||
__metaclass__ = DIRACSingleton | ||
|
||
def __init__( self ): | ||
self.__log = gLogger.getSubLogger( "Devloader" ) | ||
self.__observers = [] | ||
self.__exts = Extensions() | ||
self.__reloaded = False | ||
self.__enabled = True | ||
self.__reloadTask = False | ||
self.__stuffToClose = [] | ||
self.__watchedFiles = [] | ||
self.__modifyTimes = {} | ||
|
||
def addStuffToClose( self, stuff ): | ||
self.__stuffToClose.append( stuff ) | ||
|
||
@property | ||
def enabled( self ): | ||
return self.__enabled | ||
|
||
def watchFile( self, fp ): | ||
if os.path.isfile( fd ): | ||
self.__watchedFiles.append( fd ) | ||
return True | ||
return False | ||
|
||
def __restart( self ): | ||
self.__reloaded = True | ||
|
||
for stuff in self.__stuffToClose: | ||
try: | ||
self.__log.always( "Closing %s" % stuff ) | ||
sys.stdout.flush() | ||
stuff.close() | ||
except: | ||
gLogger.exception( "Could not close %s" % stuff ) | ||
|
||
python = sys.executable | ||
os.execl(python, python, * sys.argv) | ||
|
||
def bootstrap( self ): | ||
if not self.__enabled: | ||
return False | ||
if self.__reloadTask: | ||
return True | ||
|
||
self.__reloadTask = threading.Thread( target = self.__reloadOnUpdate ) | ||
self.__reloadTask.setDaemon(1) | ||
self.__reloadTask.start() | ||
|
||
def __reloadOnUpdate( self ): | ||
while True: | ||
time.sleep(1) | ||
if self.__reloaded: | ||
return | ||
for modName in sys.modules: | ||
modObj = sys.modules[ modName ] | ||
if not isinstance( modObj, types.ModuleType ): | ||
continue | ||
path = getattr( modObj, "__file__", None ) | ||
if not path: | ||
continue | ||
if path.endswith( ".pyc" ) or path.endswith( ".pyo" ): | ||
path = path[:-1] | ||
self.__checkFile( path ) | ||
for path in self.__watchedFiles: | ||
self.__checkFile( path ) | ||
|
||
def __checkFile( self, path ): | ||
try: | ||
modified = os.stat(path).st_mtime | ||
except Exception: | ||
return | ||
if path not in self.__modifyTimes: | ||
self.__modifyTimes[path] = modified | ||
return | ||
if self.__modifyTimes[path] != modified: | ||
self.__log.always( "File system changed (%s). Restarting..." % ( path ) ) | ||
self.__restart() |
Oops, something went wrong.