<s> import sys <EOL> import re <EOL> import glob <EOL> import operator <EOL> import os <EOL> import os . path as osp <EOL> import subprocess as sbp <EOL> import nose <EOL> import fnmatch <EOL> from cake . lib import task , path , puts <EOL> from cake . color import fore <EOL> from cake . errors import CakeError <EOL> from . . zmq import Socket <EOL> from . . utils import command , execute <EOL> from . . lib import PyscaleError <EOL> def python ( ) : <EOL> return "<STR_LIT>" % sys . executable <EOL> def shell ( cmd , exception = None ) : <EOL> return command ( cmd , shell = True , exception = exception ) <EOL> def all_modules ( regex = '<STR_LIT:*>' ) : <EOL> mods = [ ] <EOL> for path in glob . iglob ( '<STR_LIT>' % regex ) : <EOL> module = osp . basename ( osp . dirname ( path ) ) <EOL> mods . append ( ( module , path ) ) <EOL> return mods <EOL> def running_modules ( regex = '<STR_LIT:*>' ) : <EOL> mods = [ ] <EOL> for pidfile in glob . iglob ( '<STR_LIT>' % regex ) : <EOL> module = osp . splitext ( osp . basename ( pidfile ) ) [ <NUM_LIT:0> ] <EOL> pid = open ( pidfile ) . read ( ) <EOL> mods . append ( ( module , pid , pidfile ) ) <EOL> return mods <EOL> @ task ( '<STR_LIT>' ) <EOL> def start ( module = '<STR_LIT:*>' , env = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> os . environ [ '<STR_LIT>' ] = env <EOL> for module , path in all_modules ( module ) : <EOL> try : <EOL> cmd = "<STR_LIT>" % ( python ( ) , path , module ) <EOL> shell ( "<STR_LIT>" % cmd , exception = PyscaleError ) <EOL> except PyscaleError as e : <EOL> msg = str ( e ) . strip ( '<STR_LIT:\n>' ) <EOL> msg = '<STR_LIT:U+0020>' . join ( msg . split ( '<STR_LIT:\n>' ) ) <EOL> puts ( fore . cyan ( "<STR_LIT>" % module ) + "<STR_LIT>" % msg ) <EOL> @ task ( '<STR_LIT>' ) <EOL> def stop ( module = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> for module , pid , pidfile in running_modules ( module ) : <EOL> shell ( '<STR_LIT>' % pid ) <EOL> shell ( '<STR_LIT>' % pidfile ) <EOL> shell ( '<STR_LIT>' % module ) <EOL> puts ( fore . cyan ( "<STR_LIT>" % module ) + "<STR_LIT>" % fore . red ( pid ) ) <EOL> @ task <EOL> def restart ( module = '<STR_LIT:*>' , env = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> stop ( module ) <EOL> start ( module , env ) <EOL> @ task <EOL> def clean ( module = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> shell ( '<STR_LIT>' % module ) <EOL> shell ( '<STR_LIT>' % module ) <EOL> shell ( '<STR_LIT>' % module ) <EOL> @ task <EOL> def reset ( module = '<STR_LIT:*>' , env = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> stop ( module ) <EOL> clean ( module ) <EOL> start ( module , env ) <EOL> @ task <EOL> def kill ( signal = <NUM_LIT:9> ) : <EOL> """<STR_LIT>""" <EOL> zombies = [ ] <EOL> pids = set ( m [ <NUM_LIT:1> ] for m in running_modules ( ) ) <EOL> for line in shell ( '<STR_LIT>' ) . split ( '<STR_LIT:\n>' ) : <EOL> try : pid , name = re . match ( r'<STR_LIT>' , line ) . groups ( ) <EOL> except AttributeError : continue <EOL> mob = re . search ( r'<STR_LIT>' , name ) <EOL> if mob and pid not in pids : <EOL> zombies . append ( ( mob . group ( <NUM_LIT:1> ) , pid ) ) <EOL> if not zombies : <EOL> puts ( fore . green ( '<STR_LIT>' ) ) <EOL> else : <EOL> puts ( fore . green ( '<STR_LIT>' % len ( zombies ) ) ) <EOL> for name , pid in zombies : <EOL> puts ( '<STR_LIT>' % ( fore . cyan ( name ) , fore . red ( pid ) ) ) <EOL> try : shell ( '<STR_LIT>' % ( signal , pid ) , exception = RuntimeError ) <EOL> except RuntimeError as e : <EOL> puts ( fore . magenta ( '<STR_LIT>' ) + e . args [ <NUM_LIT:0> ] ) <EOL> @ task <EOL> def status ( ) : <EOL> """<STR_LIT>""" <EOL> for module , pid , pidfile in running_modules ( ) : <EOL> if not osp . exists ( "<STR_LIT>" % pid ) : <EOL> puts ( fore . red ( "<STR_LIT>" % ( module , pid ) ) ) <EOL> pids = map ( operator . itemgetter ( <NUM_LIT:1> ) , running_modules ( ) ) <EOL> if pids : <EOL> pscomm = "<STR_LIT>" % '<STR_LIT:U+002C>' . join ( pids ) <EOL> psinfo = shell ( pscomm ) . split ( '<STR_LIT:\n>' ) <EOL> if len ( psinfo ) > <NUM_LIT:1> and psinfo [ <NUM_LIT:1> ] : <EOL> puts ( fore . green ( psinfo [ <NUM_LIT:0> ] ) ) <EOL> for ps in psinfo [ <NUM_LIT:1> : ] : <EOL> color = lambda mobj : re . sub ( mobj . group ( <NUM_LIT:1> ) , fore . cyan ( mobj . group ( <NUM_LIT:1> ) ) , mobj . group ( <NUM_LIT:0> ) ) <EOL> puts ( re . sub ( '<STR_LIT>' , color , ps ) ) <EOL> @ task <EOL> def log ( module = '<STR_LIT:*>' , lines = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> if not glob . glob ( '<STR_LIT>' ) : <EOL> raise CakeError ( '<STR_LIT>' ) <EOL> else : <EOL> try : sbp . call ( "<STR_LIT>" % ( lines , module , python ( ) ) , shell = True ) <EOL> except KeyboardInterrupt : pass <EOL> @ task <EOL> def run ( what , * args ) : <EOL> """<STR_LIT>""" <EOL> what = osp . join ( path . current , what ) <EOL> args = '<STR_LIT:U+0020>' . join ( [ what ] + list ( args ) ) <EOL> execute ( "<STR_LIT>" % ( python ( ) , args ) , env = { '<STR_LIT>' : '<STR_LIT:.>' } ) <EOL> @ task <EOL> def debug ( what , * args ) : <EOL> """<STR_LIT>""" <EOL> what = osp . join ( path . current , what ) <EOL> args = '<STR_LIT:U+0020>' . join ( [ what ] + list ( args ) ) <EOL> execute ( "<STR_LIT>" % ( python ( ) , args ) , env = { '<STR_LIT>' : '<STR_LIT:.>' } ) <EOL> @ task <EOL> def console ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> argv = list ( args ) + [ '<STR_LIT>' % ( key , val ) for key , val in kwargs . items ( ) ] <EOL> if len ( argv ) > <NUM_LIT:1> : <EOL> raise CakeError ( '<STR_LIT>' ) <EOL> if argv : <EOL> run ( '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( argv ) ) <EOL> else : <EOL> debug ( '<STR_LIT>' ) <EOL> @ task <EOL> def test ( pattern = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> files = [ ] <EOL> for root , dirs , fnames in os . walk ( '<STR_LIT>' ) : <EOL> for fname in fnmatch . filter ( fnames , '<STR_LIT>' % pattern ) : <EOL> files . append ( osp . join ( root , fname ) ) <EOL> nose . main ( argv = [ '<STR_LIT>' ] + files ) </s>
