<s> import gevent <EOL> import logging <EOL> import os <EOL> import subprocess <EOL> from ajenti . api import * <EOL> from ajenti . api . helpers import subprocess_call_background , subprocess_check_output_background <EOL> from ajenti . util import cache_value <EOL> from api import Service , ServiceManager <EOL> @ plugin <EOL> class SysVInitServiceManager ( ServiceManager ) : <EOL> platforms = [ '<STR_LIT>' ] <EOL> @ cache_value ( <NUM_LIT:1> ) <EOL> def get_all ( self ) : <EOL> r = [ ] <EOL> found_names = [ ] <EOL> for line in subprocess_check_output_background ( [ '<STR_LIT>' , '<STR_LIT>' ] ) . splitlines ( ) : <EOL> tokens = line . split ( ) <EOL> if len ( tokens ) < <NUM_LIT:3> : <EOL> continue <EOL> name = tokens [ <NUM_LIT:3> ] <EOL> status = tokens [ <NUM_LIT:1> ] <EOL> if status == '<STR_LIT:?>' : <EOL> continue <EOL> if name in found_names : <EOL> continue <EOL> s = SysVInitService ( name ) <EOL> s . running = status == '<STR_LIT:+>' <EOL> r . append ( s ) <EOL> return r <EOL> def get_one ( self , name ) : <EOL> s = SysVInitService ( name ) <EOL> if os . path . exists ( s . script ) : <EOL> s . refresh ( ) <EOL> return s <EOL> return None <EOL> class SysVInitService ( Service ) : <EOL> source = '<STR_LIT>' <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . script = '<STR_LIT>' % self . name <EOL> def refresh ( self ) : <EOL> self . running = subprocess . call ( [ self . script , '<STR_LIT:status>' ] ) == <NUM_LIT:0> <EOL> def start ( self ) : <EOL> self . command ( '<STR_LIT:start>' ) <EOL> def stop ( self ) : <EOL> self . command ( '<STR_LIT>' ) <EOL> def restart ( self ) : <EOL> self . command ( '<STR_LIT>' ) <EOL> def command ( self , cmd ) : <EOL> try : <EOL> p = subprocess . Popen ( [ self . script , cmd ] , close_fds = True ) <EOL> gevent . sleep ( <NUM_LIT:0> ) <EOL> p . wait ( ) <EOL> except OSError as e : <EOL> logging . warn ( '<STR_LIT>' % ( self . script , e ) ) </s>
