<s> import dbus <EOL> import logging <EOL> import subprocess <EOL> from ajenti . api import * <EOL> from ajenti . util import cache_value <EOL> from api import Service , ServiceManager <EOL> class UpstartService ( Service ) : <EOL> source = '<STR_LIT>' <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def refresh ( self ) : <EOL> self . running = '<STR_LIT>' in subprocess . check_output ( [ '<STR_LIT:status>' , self . name ] ) <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> subprocess . Popen ( [ '<STR_LIT>' , cmd , self . name ] , close_fds = True ) . wait ( ) <EOL> @ plugin <EOL> class UpstartServiceManager ( ServiceManager ) : <EOL> def init ( self ) : <EOL> self . bus = dbus . SystemBus ( ) <EOL> self . upstart = self . bus . get_object ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def verify ( cls ) : <EOL> try : <EOL> c = cls ( ) <EOL> c . init ( ) <EOL> c . get_all ( ) <EOL> return True <EOL> except Exception , e : <EOL> logging . info ( '<STR_LIT>' % str ( e ) ) <EOL> return False <EOL> @ cache_value ( <NUM_LIT:1> ) <EOL> def get_all ( self ) : <EOL> jobs = self . upstart . GetAllJobs ( dbus_interface = "<STR_LIT>" ) <EOL> r = [ ] <EOL> for job in jobs : <EOL> obj = self . bus . get_object ( "<STR_LIT>" , job ) <EOL> jprops = obj . GetAll ( "<STR_LIT>" , dbus_interface = dbus . PROPERTIES_IFACE ) <EOL> s = UpstartService ( str ( jprops [ '<STR_LIT:name>' ] ) ) <EOL> paths = obj . GetAllInstances ( dbus_interface = "<STR_LIT>" ) <EOL> if len ( paths ) > <NUM_LIT:0> : <EOL> instance = self . bus . get_object ( "<STR_LIT>" , paths [ <NUM_LIT:0> ] ) <EOL> iprops = instance . GetAll ( "<STR_LIT>" , dbus_interface = dbus . PROPERTIES_IFACE ) <EOL> s . running = str ( iprops [ '<STR_LIT:state>' ] ) == '<STR_LIT>' <EOL> else : <EOL> s . running = False <EOL> r . append ( s ) <EOL> return r </s>
