<s> import dbus <EOL> BUS_NAME = '<STR_LIT>' <EOL> BASE_PATH = '<STR_LIT>' <EOL> UPSTART_IFACE = '<STR_LIT>' <EOL> UPSTARTJOB_IFACE = '<STR_LIT>' <EOL> UPSTARTINST_IFACE = '<STR_LIT>' <EOL> UNKNOWNJOB_EXCEPT = '<STR_LIT>' <EOL> _sysbus = dbus . SystemBus ( ) <EOL> _upstart_proxy = _sysbus . get_object ( BUS_NAME , BASE_PATH ) <EOL> def is_job ( name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _upstart_proxy . GetJobByName ( name , dbus_interface = UPSTART_IFACE ) <EOL> except dbus . exceptions . DBusException as e : <EOL> if e . get_dbus_name ( ) == UNKNOWNJOB_EXCEPT : <EOL> return False <EOL> else : <EOL> raise e <EOL> return True <EOL> def has_instances ( name ) : <EOL> job = _get_job_proxy ( name ) <EOL> if job : <EOL> instances = job . GetAllInstances ( dbus_interface = UPSTARTJOB_IFACE ) <EOL> return True if instances else False <EOL> else : <EOL> return False <EOL> def get_jobs ( jobs , names = [ ] ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> for job , name in map ( None , jobs , names ) : <EOL> job_details = get_job_details ( job ) <EOL> display_name = { '<STR_LIT>' : name or job } <EOL> if job_details : <EOL> job_details . update ( display_name ) <EOL> output . append ( job_details ) <EOL> else : <EOL> output . append ( display_name ) <EOL> return output <EOL> def get_job_details ( name ) : <EOL> """<STR_LIT>""" <EOL> if is_job ( name ) : <EOL> props = get_job_properties ( name ) <EOL> props . update ( { '<STR_LIT>' : get_instance_properties ( name ) } ) <EOL> return props <EOL> else : <EOL> return None <EOL> def get_job_properties ( name , keys = [ '<STR_LIT:name>' , '<STR_LIT:description>' ] ) : <EOL> """<STR_LIT>""" <EOL> job = _get_job_proxy ( name ) <EOL> if job : <EOL> props = job . GetAll ( UPSTARTJOB_IFACE , <EOL> dbus_interface = dbus . PROPERTIES_IFACE ) <EOL> for key in props . keys ( ) : <EOL> if key not in keys : <EOL> del props [ key ] <EOL> return props <EOL> else : <EOL> return None <EOL> def get_instance_properties ( name , keys = [ '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT:name>' ] ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> for path in _get_instances_path ( name ) or [ ] : <EOL> instance = _sysbus . get_object ( BUS_NAME , path ) <EOL> props = instance . GetAll ( UPSTARTINST_IFACE , <EOL> dbus_interface = dbus . PROPERTIES_IFACE ) <EOL> for key in props . keys ( ) : <EOL> if key not in keys : <EOL> del props [ key ] <EOL> out . append ( props ) <EOL> return out <EOL> def _get_job_proxy ( name ) : <EOL> if is_job ( name ) : <EOL> path = _upstart_proxy . GetJobByName ( name , dbus_interface = UPSTART_IFACE ) <EOL> return _sysbus . get_object ( BUS_NAME , path ) <EOL> else : <EOL> return None <EOL> def _get_instances_path ( name ) : <EOL> job = _get_job_proxy ( name ) <EOL> if job : <EOL> return job . GetAllInstances ( dbus_interface = UPSTARTJOB_IFACE ) <EOL> else : <EOL> return None </s>
