<s> """<STR_LIT>""" <EOL> import os <EOL> from tornado import web <EOL> from traitlets . config . configurable import LoggingConfigurable <EOL> from traitlets import Dict , Instance , Float <EOL> from ipython_genutils import py3compat <EOL> class ClusterManager ( LoggingConfigurable ) : <EOL> profiles = Dict ( ) <EOL> delay = Float ( <NUM_LIT:1.> , config = True , <EOL> help = "<STR_LIT>" ) <EOL> loop = Instance ( '<STR_LIT>' ) <EOL> def _loop_default ( self ) : <EOL> from zmq . eventloop . ioloop import IOLoop <EOL> return IOLoop . instance ( ) <EOL> def build_launchers ( self , profile_dir ) : <EOL> from ipyparallel . apps . ipclusterapp import IPClusterStart <EOL> class DummyIPClusterStart ( IPClusterStart ) : <EOL> """<STR_LIT>""" <EOL> def init_signal ( self ) : <EOL> pass <EOL> def reinit_logging ( self ) : <EOL> pass <EOL> starter = DummyIPClusterStart ( log = self . log ) <EOL> starter . initialize ( [ '<STR_LIT>' , profile_dir ] ) <EOL> cl = starter . controller_launcher <EOL> esl = starter . engine_launcher <EOL> n = starter . n <EOL> return cl , esl , n <EOL> def get_profile_dir ( self , name , path ) : <EOL> from IPython . core . profiledir import ProfileDir <EOL> p = ProfileDir . find_profile_dir_by_name ( path , name = name ) <EOL> return p . location <EOL> def update_profiles ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from IPython . paths import get_ipython_dir <EOL> from IPython . core . profileapp import list_profiles_in <EOL> except ImportError as e : <EOL> self . log . info ( "<STR_LIT>" , e ) <EOL> return <EOL> stale = set ( self . profiles ) <EOL> for path in [ get_ipython_dir ( ) , py3compat . getcwd ( ) ] : <EOL> if not os . path . isdir ( path ) : <EOL> continue <EOL> for profile in list_profiles_in ( path ) : <EOL> if profile in stale : <EOL> stale . remove ( profile ) <EOL> pd = self . get_profile_dir ( profile , path ) <EOL> if profile not in self . profiles : <EOL> self . log . debug ( "<STR_LIT>" , profile ) <EOL> self . profiles [ profile ] = { <EOL> '<STR_LIT>' : profile , <EOL> '<STR_LIT>' : pd , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } <EOL> for profile in stale : <EOL> self . log . debug ( "<STR_LIT>" , profile ) <EOL> self . profiles . pop ( profile ) <EOL> def list_profiles ( self ) : <EOL> try : <EOL> self . update_profiles ( ) <EOL> except ImportError : <EOL> return [ ] <EOL> default_first = lambda name : name if name != '<STR_LIT:default>' else '<STR_LIT>' <EOL> result = [ self . profile_info ( p ) for p in sorted ( self . profiles , key = default_first ) ] <EOL> return result <EOL> def check_profile ( self , profile ) : <EOL> if profile not in self . profiles : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' ) <EOL> def profile_info ( self , profile ) : <EOL> self . check_profile ( profile ) <EOL> result = { } <EOL> data = self . profiles . get ( profile ) <EOL> result [ '<STR_LIT>' ] = profile <EOL> result [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> result [ '<STR_LIT:status>' ] = data [ '<STR_LIT:status>' ] <EOL> if '<STR_LIT:n>' in data : <EOL> result [ '<STR_LIT:n>' ] = data [ '<STR_LIT:n>' ] <EOL> return result <EOL> def start_cluster ( self , profile , n = None ) : <EOL> """<STR_LIT>""" <EOL> self . check_profile ( profile ) <EOL> data = self . profiles [ profile ] <EOL> if data [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' ) <EOL> cl , esl , default_n = self . build_launchers ( data [ '<STR_LIT>' ] ) <EOL> n = n if n is not None else default_n <EOL> def clean_data ( ) : <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT:n>' , None ) <EOL> data [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> def engines_stopped ( r ) : <EOL> self . log . debug ( '<STR_LIT>' ) <EOL> if cl . running : <EOL> cl . stop ( ) <EOL> clean_data ( ) <EOL> esl . on_stop ( engines_stopped ) <EOL> def controller_stopped ( r ) : <EOL> self . log . debug ( '<STR_LIT>' ) <EOL> if esl . running : <EOL> esl . stop ( ) <EOL> clean_data ( ) <EOL> cl . on_stop ( controller_stopped ) <EOL> loop = self . loop <EOL> def start ( ) : <EOL> """<STR_LIT>""" <EOL> cl . start ( ) <EOL> loop . add_timeout ( self . loop . time ( ) + self . delay , lambda : esl . start ( n ) ) <EOL> self . loop . add_callback ( start ) <EOL> self . log . debug ( '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = cl <EOL> data [ '<STR_LIT>' ] = esl <EOL> data [ '<STR_LIT:n>' ] = n <EOL> data [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> return self . profile_info ( profile ) <EOL> def stop_cluster ( self , profile ) : <EOL> """<STR_LIT>""" <EOL> self . check_profile ( profile ) <EOL> data = self . profiles [ profile ] <EOL> if data [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' ) <EOL> data = self . profiles [ profile ] <EOL> cl = data [ '<STR_LIT>' ] <EOL> esl = data [ '<STR_LIT>' ] <EOL> if cl . running : <EOL> cl . stop ( ) <EOL> if esl . running : <EOL> esl . stop ( ) <EOL> result = { <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } <EOL> return result <EOL> def stop_all_clusters ( self ) : <EOL> for p in self . profiles . keys ( ) : <EOL> self . stop_cluster ( p ) </s>
