<s> """<STR_LIT>""" <EOL> import functools <EOL> import girder <EOL> import imp <EOL> import json <EOL> import os <EOL> import six <EOL> import sys <EOL> import traceback <EOL> import yaml <EOL> import importlib <EOL> from pkg_resources import iter_entry_points <EOL> from girder . constants import PACKAGE_DIR , ROOT_DIR , ROOT_PLUGINS_PACKAGE , TerminalColor <EOL> from girder . models . model_base import ValidationException <EOL> from girder . utility import config as _config , mail_utils , mkdir <EOL> def loadPlugins ( plugins , root , appconf , apiRoot = None , curConfig = None , <EOL> buildDag = True ) : <EOL> """<STR_LIT>""" <EOL> if curConfig is None : <EOL> curConfig = _config . getConfig ( ) <EOL> if '<STR_LIT>' in curConfig and '<STR_LIT>' in curConfig [ '<STR_LIT>' ] : <EOL> print ( TerminalColor . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> if ROOT_PLUGINS_PACKAGE not in sys . modules : <EOL> module = imp . new_module ( ROOT_PLUGINS_PACKAGE ) <EOL> girder . plugins = module <EOL> sys . modules [ ROOT_PLUGINS_PACKAGE ] = module <EOL> print ( TerminalColor . info ( '<STR_LIT>' ) ) <EOL> if buildDag : <EOL> plugins = getToposortedPlugins ( plugins , curConfig , ignoreMissing = True ) <EOL> for plugin in plugins : <EOL> try : <EOL> root , appconf , apiRoot = loadPlugin ( <EOL> plugin , root , appconf , apiRoot , curConfig = curConfig ) <EOL> print ( TerminalColor . success ( '<STR_LIT>' % plugin ) ) <EOL> except Exception : <EOL> print ( TerminalColor . error ( <EOL> '<STR_LIT>' % plugin ) ) <EOL> girder . logger . exception ( '<STR_LIT>' % plugin ) <EOL> traceback . print_exc ( ) <EOL> return root , appconf , apiRoot <EOL> def getToposortedPlugins ( plugins , curConfig = None , ignoreMissing = False ) : <EOL> """<STR_LIT>""" <EOL> curConfig = curConfig or _config . getConfig ( ) <EOL> plugins = set ( plugins ) <EOL> allPlugins = findAllPlugins ( curConfig ) <EOL> dag = { } <EOL> visited = set ( ) <EOL> def addDeps ( plugin ) : <EOL> if plugin not in allPlugins : <EOL> message = '<STR_LIT>' % plugin <EOL> if ignoreMissing : <EOL> print ( TerminalColor . error ( message ) ) <EOL> girder . logger . error ( message ) <EOL> return <EOL> else : <EOL> raise ValidationException ( message ) <EOL> deps = allPlugins [ plugin ] [ '<STR_LIT>' ] <EOL> dag [ plugin ] = deps <EOL> for dep in deps : <EOL> if dep in visited : <EOL> return <EOL> visited . add ( dep ) <EOL> addDeps ( dep ) <EOL> for plugin in plugins : <EOL> addDeps ( plugin ) <EOL> for pset in toposort ( dag ) : <EOL> for plugin in pset : <EOL> yield plugin <EOL> def getPluginParentDir ( name , curConfig = None ) : <EOL> """<STR_LIT>""" <EOL> for potentialParentDir in getPluginDirs ( curConfig ) : <EOL> if os . path . isdir ( os . path . join ( potentialParentDir , name ) ) : <EOL> return potentialParentDir <EOL> raise Exception ( '<STR_LIT>' % name ) <EOL> def loadPlugin ( name , root , appconf , apiRoot = None , curConfig = None ) : <EOL> """<STR_LIT>""" <EOL> if apiRoot is None : <EOL> apiRoot = root . api . v1 <EOL> try : <EOL> pluginParentDir = getPluginParentDir ( name , curConfig ) <EOL> except Exception : <EOL> pluginParentDir = '<STR_LIT>' <EOL> pluginDir = os . path . join ( pluginParentDir , name ) <EOL> isPluginDir = os . path . isdir ( os . path . join ( pluginDir , '<STR_LIT>' ) ) <EOL> isPluginFile = os . path . isfile ( os . path . join ( pluginDir , '<STR_LIT>' ) ) <EOL> pluginLoadMethod = None <EOL> if not os . path . exists ( pluginDir ) : <EOL> for entry_point in iter_entry_points ( group = '<STR_LIT>' , name = name ) : <EOL> pluginLoadMethod = entry_point . load ( ) <EOL> pluginDir = os . path . dirname ( <EOL> importlib . import_module ( entry_point . module_name ) . __file__ <EOL> ) <EOL> isPluginDir = True <EOL> if not os . path . exists ( pluginDir ) : <EOL> raise Exception ( '<STR_LIT>' % pluginDir ) <EOL> if not isPluginDir and not isPluginFile : <EOL> return root , appconf , apiRoot <EOL> mailTemplatesDir = os . path . join ( pluginDir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . isdir ( mailTemplatesDir ) : <EOL> mail_utils . addTemplateDirectory ( mailTemplatesDir , prepend = True ) <EOL> moduleName = '<STR_LIT:.>' . join ( ( ROOT_PLUGINS_PACKAGE , name ) ) <EOL> if moduleName not in sys . modules : <EOL> fp = None <EOL> try : <EOL> info = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : appconf , <EOL> '<STR_LIT>' : root , <EOL> '<STR_LIT>' : apiRoot , <EOL> '<STR_LIT>' : os . path . abspath ( pluginDir ) <EOL> } <EOL> if pluginLoadMethod is None : <EOL> fp , pathname , description = imp . find_module ( <EOL> '<STR_LIT>' , [ pluginDir ] <EOL> ) <EOL> module = imp . load_module ( moduleName , fp , pathname , description ) <EOL> module . PLUGIN_ROOT_DIR = pluginDir <EOL> girder . plugins . __dict__ [ name ] = module <EOL> pluginLoadMethod = getattr ( module , '<STR_LIT>' , None ) <EOL> if pluginLoadMethod is not None : <EOL> pluginLoadMethod ( info ) <EOL> root , appconf , apiRoot = ( <EOL> info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) <EOL> finally : <EOL> if fp : <EOL> fp . close ( ) <EOL> return root , appconf , apiRoot <EOL> def defaultPluginDir ( ) : <EOL> """<STR_LIT>""" <EOL> pluginDir = None <EOL> if os . path . isdir ( os . path . join ( ROOT_DIR , '<STR_LIT>' ) ) : <EOL> pluginDir = os . path . join ( ROOT_DIR , '<STR_LIT>' ) <EOL> else : <EOL> pluginDir = os . path . join ( PACKAGE_DIR , '<STR_LIT>' ) <EOL> return pluginDir <EOL> def getPluginDirs ( curConfig = None ) : <EOL> """<STR_LIT>""" <EOL> failedPluginDirs = set ( ) <EOL> if curConfig is None : <EOL> curConfig = _config . getConfig ( ) <EOL> if '<STR_LIT>' in curConfig and '<STR_LIT>' in curConfig [ '<STR_LIT>' ] : <EOL> pluginDirs = curConfig [ '<STR_LIT>' ] [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) <EOL> else : <EOL> pluginDirs = [ defaultPluginDir ( ) ] <EOL> for pluginDir in pluginDirs : <EOL> try : <EOL> mkdir ( pluginDir ) <EOL> except OSError : <EOL> print ( TerminalColor . warning ( <EOL> '<STR_LIT>' % pluginDir ) ) <EOL> failedPluginDirs . add ( pluginDir ) <EOL> return [ dir for dir in pluginDirs if dir not in failedPluginDirs ] <EOL> def getPluginDir ( curConfig = None ) : <EOL> """<STR_LIT>""" <EOL> if curConfig is None : <EOL> curConfig = _config . getConfig ( ) <EOL> pluginDirs = getPluginDirs ( curConfig ) <EOL> if '<STR_LIT>' in curConfig and '<STR_LIT>' in curConfig [ '<STR_LIT>' ] : <EOL> pluginDir = curConfig [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> elif pluginDirs : <EOL> pluginDir = pluginDirs [ <NUM_LIT:0> ] <EOL> else : <EOL> pluginDir = None <EOL> return pluginDir <EOL> def findAllPlugins ( curConfig = None ) : <EOL> """<STR_LIT>""" <EOL> allPlugins = { } <EOL> for entry_point in iter_entry_points ( group = '<STR_LIT>' ) : <EOL> allPlugins [ entry_point . name ] = { <EOL> '<STR_LIT:name>' : entry_point . name , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : set ( ) <EOL> } <EOL> allPlugins [ entry_point . name ] . update ( <EOL> getattr ( entry_point . load ( ) , '<STR_LIT>' , { } ) <EOL> ) <EOL> pluginDirs = getPluginDirs ( curConfig ) <EOL> if not pluginDirs : <EOL> print ( TerminalColor . warning ( '<STR_LIT>' ) ) <EOL> return allPlugins <EOL> for pluginDir in pluginDirs : <EOL> dirs = [ dir for dir in os . listdir ( pluginDir ) if os . path . isdir ( <EOL> os . path . join ( pluginDir , dir ) ) ] <EOL> for plugin in dirs : <EOL> data = { } <EOL> configJson = os . path . join ( pluginDir , plugin , '<STR_LIT>' ) <EOL> configYml = os . path . join ( pluginDir , plugin , '<STR_LIT>' ) <EOL> if os . path . isfile ( configJson ) : <EOL> with open ( configJson ) as conf : <EOL> try : <EOL> data = json . load ( conf ) <EOL> except ValueError as e : <EOL> print ( <EOL> TerminalColor . error ( <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % plugin ) ) <EOL> print ( e ) <EOL> elif os . path . isfile ( configYml ) : <EOL> with open ( configYml ) as conf : <EOL> try : <EOL> data = yaml . safe_load ( conf ) <EOL> except yaml . YAMLError as e : <EOL> print ( <EOL> TerminalColor . error ( <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % plugin ) ) <EOL> print ( e ) <EOL> allPlugins [ plugin ] = { <EOL> '<STR_LIT:name>' : data . get ( '<STR_LIT:name>' , plugin ) , <EOL> '<STR_LIT:description>' : data . get ( '<STR_LIT:description>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:version>' : data . get ( '<STR_LIT:version>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : set ( data . get ( '<STR_LIT>' , [ ] ) ) <EOL> } <EOL> return allPlugins <EOL> def toposort ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> return <EOL> for k , v in six . viewitems ( data ) : <EOL> v . discard ( k ) <EOL> extra = functools . reduce ( <EOL> set . union , six . viewvalues ( data ) ) - set ( six . viewkeys ( data ) ) <EOL> data . update ( { item : set ( ) for item in extra } ) <EOL> while True : <EOL> ordered = set ( item for item , dep in six . viewitems ( data ) if not dep ) <EOL> if not ordered : <EOL> break <EOL> yield ordered <EOL> data = { item : ( dep - ordered ) <EOL> for item , dep in six . viewitems ( data ) if item not in ordered } <EOL> if data : <EOL> raise Exception ( '<STR_LIT>' % '<STR_LIT:\n>' . join ( <EOL> repr ( x ) for x in six . viewitems ( data ) ) ) <EOL> def addChildNode ( node , name , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if obj : <EOL> setattr ( node , name , obj ) <EOL> return obj <EOL> else : <EOL> hiddenNode = type ( '<STR_LIT>' , ( ) , dict ( exposed = False ) ) ( ) <EOL> setattr ( node , name , hiddenNode ) <EOL> return hiddenNode <EOL> class config ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kw ) : <EOL> self . config = kw <EOL> def __call__ ( self , func ) : <EOL> @ six . wraps ( func ) <EOL> def wrapped ( * arg , ** kw ) : <EOL> return func ( * arg , ** kw ) <EOL> wrapped . config = self . config <EOL> return wrapped </s>
