def __init__(self, lcm_obj, config, spawn_deputy, script_name, script_done_action): self.sheriff = Sheriff(lcm_obj) self.script_manager = ScriptManager(self.sheriff) self.spawn_deputy = spawn_deputy self.spawned_deputy = None self.config = config self.script_name = script_name self.script = None self.mainloop = None self.lcm_obj = lcm_obj self._should_exit = False if script_done_action is None: self.script_done_action = "exit" else: self.script_done_action = script_done_action
def __init__(self, lcm_obj): self.lcm_obj = lcm_obj self.cmds_update_scheduled = False self.config_filename = None self.script_done_action = None # deputy spawned by the sheriff self.spawned_deputy = None # create sheriff and subscribe to events self.sheriff = Sheriff(self.lcm_obj) self.sheriff.add_listener(self) self.script_manager = ScriptManager(self.sheriff) self.script_manager.add_listener(self) # update very soon gobject.timeout_add(100, lambda *s: self.deputies_ts.update() and False) gobject.timeout_add(100, lambda *s: self._schedule_cmds_update() and False) # and then periodically gobject.timeout_add(1000, self._check_spawned_deputy) gobject.timeout_add(1000, lambda *s: self._schedule_cmds_update() or True) # setup GUI self.builder = gtk.Builder() self.builder.add_from_file(find_procman_glade()) self.builder.connect_signals(self) self.window = self.builder.get_object("main_window") self.cmds_ts = cm.SheriffCommandModel(self.sheriff) self.cmds_tv = ctv.SheriffCommandTreeView(self.sheriff, self.script_manager, self.cmds_ts) # load save menu self.load_cfg_mi = self.builder.get_object("load_cfg_mi") self.save_cfg_mi = self.builder.get_object("save_cfg_mi") self.load_dlg = None self.save_dlg = None self.load_save_dir = None self.cfg_to_load = None if BUILD_PREFIX and os.path.exists("%s/data/procman" % BUILD_PREFIX): self.load_save_dir = "%s/data/procman" % BUILD_PREFIX # options menu self.is_observer_cmi = self.builder.get_object("is_observer_cmi") self.spawn_deputy_mi = self.builder.get_object("spawn_deputy_mi") self.terminate_spawned_deputy_mi = self.builder.get_object( "terminate_spawned_deputy_mi") self.procman_deputy_cmd = find_procman_deputy_cmd() if not self.procman_deputy_cmd: sys.stderr.write( "Can't find procman-deputy. Spawn Deputy disabled") self.spawn_deputy_mi.set_sensitive(False) # commands menu self.start_cmd_mi = self.builder.get_object("start_cmd_mi") self.stop_cmd_mi = self.builder.get_object("stop_cmd_mi") self.restart_cmd_mi = self.builder.get_object("restart_cmd_mi") self.remove_cmd_mi = self.builder.get_object("remove_cmd_mi") self.edit_cmd_mi = self.builder.get_object("edit_cmd_mi") self.new_cmd_mi = self.builder.get_object("new_cmd_mi") # scripts menu self.abort_script_mi = self.builder.get_object("abort_script_mi") self.edit_script_mi = self.builder.get_object("edit_script_mi") self.remove_script_mi = self.builder.get_object("remove_script_mi") self.scripts_menu = self.builder.get_object("scripts_menu") self.edit_scripts_menu = self.builder.get_object("edit_scripts_menu") self.remove_scripts_menu = self.builder.get_object( "remove_scripts_menu") vpane = self.builder.get_object("vpaned") sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane = self.builder.get_object("hpaned") hpane.pack1(sw, resize=True) sw.add(self.cmds_tv) cmds_sel = self.cmds_tv.get_selection() cmds_sel.connect("changed", self._on_cmds_selection_changed) # create a checkable item in the View menu for each column to toggle # its visibility in the treeview view_menu = self.builder.get_object("view_menu") for col in self.cmds_tv.get_columns(): name = col.get_title() col_cmi = gtk.CheckMenuItem(name) col_cmi.set_active(col.get_visible()) def on_activate(cmi, col_): should_be_visible = cmi.get_active() if col_.get_visible() != should_be_visible: col_.set_visible(should_be_visible) if col_ == self.cmds_tv.columns[0]: self.cmds_ts.set_populate_exec_with_group_name( not should_be_visible) self.cmds_ts.repopulate() def on_visibility_changed(col_, param, cmi_): is_visible = col_.get_visible() if is_visible != cmi_.get_active(): cmi_.set_active(is_visible) col_cmi.connect("activate", on_activate, col) col.connect("notify::visible", on_visibility_changed, col_cmi) view_menu.append(col_cmi) # setup the deputies treeview self.deputies_ts = ht.DeputyModel(self.sheriff) self.deputies_tv = ht.DeputyTreeView(self.sheriff, self.deputies_ts) sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane.pack2(sw, resize=False) sw.add(self.deputies_tv) gobject.timeout_add(1000, lambda *s: self.deputies_ts.update() or True) # stdout textview self.cmd_console = cc.SheriffCommandConsole(self.sheriff, self.lcm_obj) vpane.add2(self.cmd_console) # status bar self.statusbar = self.builder.get_object("statusbar") self.statusbar_context_script = self.statusbar.get_context_id("script") self.statusbar_context_main = self.statusbar.get_context_id("main") self.statusbar_context_script_msg = None config_dir = os.path.join(glib.get_user_config_dir(), "procman-sheriff") if not os.path.exists(config_dir): os.makedirs(config_dir) self.config_fname = os.path.join(config_dir, "config") self.load_settings() self.window.show_all()
class SheriffGtk(SheriffListener): def __init__(self, lcm_obj): self.lcm_obj = lcm_obj self.cmds_update_scheduled = False self.config_filename = None self.script_done_action = None # deputy spawned by the sheriff self.spawned_deputy = None # create sheriff and subscribe to events self.sheriff = Sheriff(self.lcm_obj) self.sheriff.add_listener(self) self.script_manager = ScriptManager(self.sheriff) self.script_manager.add_listener(self) # update very soon gobject.timeout_add(100, lambda *s: self.deputies_ts.update() and False) gobject.timeout_add(100, lambda *s: self._schedule_cmds_update() and False) # and then periodically gobject.timeout_add(1000, self._check_spawned_deputy) gobject.timeout_add(1000, lambda *s: self._schedule_cmds_update() or True) # setup GUI self.builder = gtk.Builder() self.builder.add_from_file(find_procman_glade()) self.builder.connect_signals(self) self.window = self.builder.get_object("main_window") self.cmds_ts = cm.SheriffCommandModel(self.sheriff) self.cmds_tv = ctv.SheriffCommandTreeView(self.sheriff, self.script_manager, self.cmds_ts) # load save menu self.load_cfg_mi = self.builder.get_object("load_cfg_mi") self.save_cfg_mi = self.builder.get_object("save_cfg_mi") self.load_dlg = None self.save_dlg = None self.load_save_dir = None self.cfg_to_load = None if BUILD_PREFIX and os.path.exists("%s/data/procman" % BUILD_PREFIX): self.load_save_dir = "%s/data/procman" % BUILD_PREFIX # options menu self.is_observer_cmi = self.builder.get_object("is_observer_cmi") self.spawn_deputy_mi = self.builder.get_object("spawn_deputy_mi") self.terminate_spawned_deputy_mi = self.builder.get_object( "terminate_spawned_deputy_mi") self.procman_deputy_cmd = find_procman_deputy_cmd() if not self.procman_deputy_cmd: sys.stderr.write( "Can't find procman-deputy. Spawn Deputy disabled") self.spawn_deputy_mi.set_sensitive(False) # commands menu self.start_cmd_mi = self.builder.get_object("start_cmd_mi") self.stop_cmd_mi = self.builder.get_object("stop_cmd_mi") self.restart_cmd_mi = self.builder.get_object("restart_cmd_mi") self.remove_cmd_mi = self.builder.get_object("remove_cmd_mi") self.edit_cmd_mi = self.builder.get_object("edit_cmd_mi") self.new_cmd_mi = self.builder.get_object("new_cmd_mi") # scripts menu self.abort_script_mi = self.builder.get_object("abort_script_mi") self.edit_script_mi = self.builder.get_object("edit_script_mi") self.remove_script_mi = self.builder.get_object("remove_script_mi") self.scripts_menu = self.builder.get_object("scripts_menu") self.edit_scripts_menu = self.builder.get_object("edit_scripts_menu") self.remove_scripts_menu = self.builder.get_object( "remove_scripts_menu") vpane = self.builder.get_object("vpaned") sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane = self.builder.get_object("hpaned") hpane.pack1(sw, resize=True) sw.add(self.cmds_tv) cmds_sel = self.cmds_tv.get_selection() cmds_sel.connect("changed", self._on_cmds_selection_changed) # create a checkable item in the View menu for each column to toggle # its visibility in the treeview view_menu = self.builder.get_object("view_menu") for col in self.cmds_tv.get_columns(): name = col.get_title() col_cmi = gtk.CheckMenuItem(name) col_cmi.set_active(col.get_visible()) def on_activate(cmi, col_): should_be_visible = cmi.get_active() if col_.get_visible() != should_be_visible: col_.set_visible(should_be_visible) if col_ == self.cmds_tv.columns[0]: self.cmds_ts.set_populate_exec_with_group_name( not should_be_visible) self.cmds_ts.repopulate() def on_visibility_changed(col_, param, cmi_): is_visible = col_.get_visible() if is_visible != cmi_.get_active(): cmi_.set_active(is_visible) col_cmi.connect("activate", on_activate, col) col.connect("notify::visible", on_visibility_changed, col_cmi) view_menu.append(col_cmi) # setup the deputies treeview self.deputies_ts = ht.DeputyModel(self.sheriff) self.deputies_tv = ht.DeputyTreeView(self.sheriff, self.deputies_ts) sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane.pack2(sw, resize=False) sw.add(self.deputies_tv) gobject.timeout_add(1000, lambda *s: self.deputies_ts.update() or True) # stdout textview self.cmd_console = cc.SheriffCommandConsole(self.sheriff, self.lcm_obj) vpane.add2(self.cmd_console) # status bar self.statusbar = self.builder.get_object("statusbar") self.statusbar_context_script = self.statusbar.get_context_id("script") self.statusbar_context_main = self.statusbar.get_context_id("main") self.statusbar_context_script_msg = None config_dir = os.path.join(glib.get_user_config_dir(), "procman-sheriff") if not os.path.exists(config_dir): os.makedirs(config_dir) self.config_fname = os.path.join(config_dir, "config") self.load_settings() self.window.show_all() def command_added(self, deputy_obj, cmd_obj): self._schedule_cmds_update() def command_removed(self, deputy_obj, cmd_obj): self._schedule_cmds_update() if self.cfg_to_load and not self.sheriff.get_all_commands(): glib.idle_add(self._do_load_config) def command_status_changed(self, cmd_obj, old_status, new_status): self._schedule_cmds_update() def command_group_changed(self, cmd_obj): self._schedule_cmds_update() def script_added(self, script_object): glib.idle_add(self._gtk_on_script_added, script_object) def sheriff_conflict_detected(self, other_sheriff_id): glib.idle_add(self._gtk_sheriff_conflict_detected) def _gtk_sheriff_conflict_detected(self): # detected the presence of another sheriff that is not this one. # self-demote to prevent command thrashing self.sheriff.set_observer(True) self.statusbar.push( self.statusbar.get_context_id("main"), "WARNING: multiple sheriffs detected! Switching to observer mode") gobject.timeout_add( 6000, lambda *s: self.statusbar.pop( self.statusbar.get_context_id("main"))) def observer_status_changed(self, is_observer): glib.idle_add(self._gtk_observer_status_changed, is_observer) def _gtk_observer_status_changed(self, is_observer): self._update_menu_item_sensitivities() if is_observer: self.window.set_title("Procman Observer") else: self.window.set_title("Procman Sheriff") self.is_observer_cmi.set_active(is_observer) def script_removed(self, script_object): glib.idle_add(self._gtk_on_script_removed, script_object) def script_started(self, script_object): glib.idle_add(self._gtk_on_script_started, script_object) def script_action_executing(self, script_object, action): glib.idle_add(self._gtk_on_script_action_executing, script_object, action) def script_finished(self, script_object): glib.idle_add(self._gtk_on_script_finished, script_object) def on_preferences_mi_activate(self, *args): sd.do_preferences_dialog(self, self.window) def on_quit_requested(self, *args): gtk.main_quit() def on_start_cmd_mi_activate(self, *args): self.cmds_tv._start_selected_commands() def on_stop_cmd_mi_activate(self, *args): self.cmds_tv._stop_selected_commands() def on_restart_cmd_mi_activate(self, *args): self.cmds_tv._restart_selected_commands() def on_remove_cmd_mi_activate(self, *args): self.cmds_tv._remove_selected_commands() def on_edit_cmd_mi_activate(self, *args): self.cmds_tv._edit_selected_command() def on_new_cmd_mi_activate(self, *args): sd.do_add_command_dialog(self.sheriff, self.cmds_ts, self.window) def cleanup(self, save_settings): self._terminate_spawned_deputy() self.sheriff.shutdown() self.script_manager.shutdown() if save_settings: self.save_settings() def load_settings(self): if not os.path.exists(self.config_fname): return try: d = pickle.load(open(self.config_fname, "r")) except Exception, err: print err return self.cmds_tv.load_settings(d) self.cmd_console.load_settings(d) self.deputies_tv.load_settings(d)
def __init__ (self, lcm_obj): self.lcm_obj = lcm_obj self.cmds_update_scheduled = False self.config_filename = None self.script_done_action = None # deputy spawned by the sheriff self.spawned_deputy = None # create sheriff and subscribe to events self.sheriff = Sheriff (self.lcm_obj) self.sheriff.add_listener(self) self.script_manager = ScriptManager(self.sheriff) self.script_manager.add_listener(self) # update very soon gobject.timeout_add(100, lambda *s: self.deputies_ts.update() and False) gobject.timeout_add(100, lambda *s: self._schedule_cmds_update() and False) # and then periodically gobject.timeout_add (1000, self._check_spawned_deputy) gobject.timeout_add (1000, lambda *s: self._schedule_cmds_update () or True) # setup GUI self.builder = gtk.Builder() self.builder.add_from_file(find_procman_glade()) self.builder.connect_signals(self) self.window = self.builder.get_object("main_window") self.cmds_ts = cm.SheriffCommandModel(self.sheriff) self.cmds_tv = ctv.SheriffCommandTreeView(self.sheriff, self.script_manager, self.cmds_ts) # load save menu self.load_cfg_mi = self.builder.get_object("load_cfg_mi") self.save_cfg_mi = self.builder.get_object("save_cfg_mi") self.load_dlg = None self.save_dlg = None self.load_save_dir = None self.cfg_to_load = None if BUILD_PREFIX and os.path.exists("%s/data/procman" % BUILD_PREFIX): self.load_save_dir = "%s/data/procman" % BUILD_PREFIX # options menu self.is_observer_cmi = self.builder.get_object("is_observer_cmi") self.spawn_deputy_mi = self.builder.get_object("spawn_deputy_mi") self.terminate_spawned_deputy_mi = self.builder.get_object("terminate_spawned_deputy_mi") self.procman_deputy_cmd = find_procman_deputy_cmd() if not self.procman_deputy_cmd: sys.stderr.write("Can't find procman-deputy. Spawn Deputy disabled") self.spawn_deputy_mi.set_sensitive(False) # commands menu self.start_cmd_mi = self.builder.get_object("start_cmd_mi") self.stop_cmd_mi = self.builder.get_object("stop_cmd_mi") self.restart_cmd_mi = self.builder.get_object("restart_cmd_mi") self.remove_cmd_mi = self.builder.get_object("remove_cmd_mi") self.edit_cmd_mi = self.builder.get_object("edit_cmd_mi") self.new_cmd_mi = self.builder.get_object("new_cmd_mi") # scripts menu self.abort_script_mi = self.builder.get_object("abort_script_mi") self.edit_script_mi = self.builder.get_object("edit_script_mi") self.remove_script_mi = self.builder.get_object("remove_script_mi") self.scripts_menu = self.builder.get_object("scripts_menu") self.edit_scripts_menu = self.builder.get_object("edit_scripts_menu") self.remove_scripts_menu = self.builder.get_object("remove_scripts_menu") vpane = self.builder.get_object("vpaned") sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane = self.builder.get_object("hpaned") hpane.pack1(sw, resize = True) sw.add(self.cmds_tv) cmds_sel = self.cmds_tv.get_selection() cmds_sel.connect ("changed", self._on_cmds_selection_changed) # create a checkable item in the View menu for each column to toggle # its visibility in the treeview view_menu = self.builder.get_object("view_menu") for col in self.cmds_tv.get_columns(): name = col.get_title () col_cmi = gtk.CheckMenuItem(name) col_cmi.set_active(col.get_visible()) def on_activate(cmi, col_): should_be_visible = cmi.get_active() if col_.get_visible() != should_be_visible: col_.set_visible(should_be_visible) if col_ == self.cmds_tv.columns[0]: self.cmds_ts.set_populate_exec_with_group_name(not should_be_visible) self.cmds_ts.repopulate() def on_visibility_changed(col_, param, cmi_): is_visible = col_.get_visible() if is_visible != cmi_.get_active(): cmi_.set_active(is_visible) col_cmi.connect("activate", on_activate, col) col.connect("notify::visible", on_visibility_changed, col_cmi) view_menu.append(col_cmi) # setup the deputies treeview self.deputies_ts = ht.DeputyModel(self.sheriff) self.deputies_tv = ht.DeputyTreeView(self.sheriff, self.deputies_ts) sw = gtk.ScrolledWindow () sw.set_policy (gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane.pack2 (sw, resize = False) sw.add (self.deputies_tv) gobject.timeout_add (1000, lambda *s: self.deputies_ts.update() or True) # stdout textview self.cmd_console = cc.SheriffCommandConsole(self.sheriff, self.lcm_obj) vpane.add2(self.cmd_console) # status bar self.statusbar = self.builder.get_object("statusbar") self.statusbar_context_script = self.statusbar.get_context_id("script") self.statusbar_context_main = self.statusbar.get_context_id("main") self.statusbar_context_script_msg = None config_dir = os.path.join(glib.get_user_config_dir(), "procman-sheriff") if not os.path.exists(config_dir): os.makedirs(config_dir) self.config_fname = os.path.join(config_dir, "config") self.load_settings() self.window.show_all()
class SheriffGtk(SheriffListener): def __init__ (self, lcm_obj): self.lcm_obj = lcm_obj self.cmds_update_scheduled = False self.config_filename = None self.script_done_action = None # deputy spawned by the sheriff self.spawned_deputy = None # create sheriff and subscribe to events self.sheriff = Sheriff (self.lcm_obj) self.sheriff.add_listener(self) self.script_manager = ScriptManager(self.sheriff) self.script_manager.add_listener(self) # update very soon gobject.timeout_add(100, lambda *s: self.deputies_ts.update() and False) gobject.timeout_add(100, lambda *s: self._schedule_cmds_update() and False) # and then periodically gobject.timeout_add (1000, self._check_spawned_deputy) gobject.timeout_add (1000, lambda *s: self._schedule_cmds_update () or True) # setup GUI self.builder = gtk.Builder() self.builder.add_from_file(find_procman_glade()) self.builder.connect_signals(self) self.window = self.builder.get_object("main_window") self.cmds_ts = cm.SheriffCommandModel(self.sheriff) self.cmds_tv = ctv.SheriffCommandTreeView(self.sheriff, self.script_manager, self.cmds_ts) # load save menu self.load_cfg_mi = self.builder.get_object("load_cfg_mi") self.save_cfg_mi = self.builder.get_object("save_cfg_mi") self.load_dlg = None self.save_dlg = None self.load_save_dir = None self.cfg_to_load = None if BUILD_PREFIX and os.path.exists("%s/data/procman" % BUILD_PREFIX): self.load_save_dir = "%s/data/procman" % BUILD_PREFIX # options menu self.is_observer_cmi = self.builder.get_object("is_observer_cmi") self.spawn_deputy_mi = self.builder.get_object("spawn_deputy_mi") self.terminate_spawned_deputy_mi = self.builder.get_object("terminate_spawned_deputy_mi") self.procman_deputy_cmd = find_procman_deputy_cmd() if not self.procman_deputy_cmd: sys.stderr.write("Can't find procman-deputy. Spawn Deputy disabled") self.spawn_deputy_mi.set_sensitive(False) # commands menu self.start_cmd_mi = self.builder.get_object("start_cmd_mi") self.stop_cmd_mi = self.builder.get_object("stop_cmd_mi") self.restart_cmd_mi = self.builder.get_object("restart_cmd_mi") self.remove_cmd_mi = self.builder.get_object("remove_cmd_mi") self.edit_cmd_mi = self.builder.get_object("edit_cmd_mi") self.new_cmd_mi = self.builder.get_object("new_cmd_mi") # scripts menu self.abort_script_mi = self.builder.get_object("abort_script_mi") self.edit_script_mi = self.builder.get_object("edit_script_mi") self.remove_script_mi = self.builder.get_object("remove_script_mi") self.scripts_menu = self.builder.get_object("scripts_menu") self.edit_scripts_menu = self.builder.get_object("edit_scripts_menu") self.remove_scripts_menu = self.builder.get_object("remove_scripts_menu") vpane = self.builder.get_object("vpaned") sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane = self.builder.get_object("hpaned") hpane.pack1(sw, resize = True) sw.add(self.cmds_tv) cmds_sel = self.cmds_tv.get_selection() cmds_sel.connect ("changed", self._on_cmds_selection_changed) # create a checkable item in the View menu for each column to toggle # its visibility in the treeview view_menu = self.builder.get_object("view_menu") for col in self.cmds_tv.get_columns(): name = col.get_title () col_cmi = gtk.CheckMenuItem(name) col_cmi.set_active(col.get_visible()) def on_activate(cmi, col_): should_be_visible = cmi.get_active() if col_.get_visible() != should_be_visible: col_.set_visible(should_be_visible) if col_ == self.cmds_tv.columns[0]: self.cmds_ts.set_populate_exec_with_group_name(not should_be_visible) self.cmds_ts.repopulate() def on_visibility_changed(col_, param, cmi_): is_visible = col_.get_visible() if is_visible != cmi_.get_active(): cmi_.set_active(is_visible) col_cmi.connect("activate", on_activate, col) col.connect("notify::visible", on_visibility_changed, col_cmi) view_menu.append(col_cmi) # setup the deputies treeview self.deputies_ts = ht.DeputyModel(self.sheriff) self.deputies_tv = ht.DeputyTreeView(self.sheriff, self.deputies_ts) sw = gtk.ScrolledWindow () sw.set_policy (gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) hpane.pack2 (sw, resize = False) sw.add (self.deputies_tv) gobject.timeout_add (1000, lambda *s: self.deputies_ts.update() or True) # stdout textview self.cmd_console = cc.SheriffCommandConsole(self.sheriff, self.lcm_obj) vpane.add2(self.cmd_console) # status bar self.statusbar = self.builder.get_object("statusbar") self.statusbar_context_script = self.statusbar.get_context_id("script") self.statusbar_context_main = self.statusbar.get_context_id("main") self.statusbar_context_script_msg = None config_dir = os.path.join(glib.get_user_config_dir(), "procman-sheriff") if not os.path.exists(config_dir): os.makedirs(config_dir) self.config_fname = os.path.join(config_dir, "config") self.load_settings() self.window.show_all() def command_added(self, deputy_obj, cmd_obj): self._schedule_cmds_update() def command_removed(self, deputy_obj, cmd_obj): self._schedule_cmds_update() if self.cfg_to_load and not self.sheriff.get_all_commands(): glib.idle_add(self._do_load_config) def command_status_changed(self, cmd_obj, old_status, new_status): self._schedule_cmds_update() def command_group_changed(self, cmd_obj): self._schedule_cmds_update() def script_added(self, script_object): glib.idle_add(self._gtk_on_script_added, script_object) def sheriff_conflict_detected(self, other_sheriff_id): glib.idle_add(self._gtk_sheriff_conflict_detected) def _gtk_sheriff_conflict_detected(self): # detected the presence of another sheriff that is not this one. # self-demote to prevent command thrashing self.sheriff.set_observer (True) self.statusbar.push (self.statusbar.get_context_id ("main"), "WARNING: multiple sheriffs detected! Switching to observer mode"); gobject.timeout_add (6000, lambda *s: self.statusbar.pop (self.statusbar.get_context_id ("main"))) def observer_status_changed(self, is_observer): glib.idle_add(self._gtk_observer_status_changed, is_observer) def _gtk_observer_status_changed(self, is_observer): self._update_menu_item_sensitivities() if is_observer: self.window.set_title("Procman Observer") else: self.window.set_title("Procman Sheriff") self.is_observer_cmi.set_active(is_observer) def script_removed(self, script_object): glib.idle_add(self._gtk_on_script_removed, script_object) def script_started(self, script_object): glib.idle_add(self._gtk_on_script_started, script_object) def script_action_executing(self, script_object, action): glib.idle_add(self._gtk_on_script_action_executing, script_object, action) def script_finished(self, script_object): glib.idle_add(self._gtk_on_script_finished, script_object) def on_preferences_mi_activate(self, *args): sd.do_preferences_dialog(self, self.window) def on_quit_requested(self, *args): gtk.main_quit() def on_start_cmd_mi_activate(self, *args): self.cmds_tv._start_selected_commands() def on_stop_cmd_mi_activate(self, *args): self.cmds_tv._stop_selected_commands() def on_restart_cmd_mi_activate(self, *args): self.cmds_tv._restart_selected_commands() def on_remove_cmd_mi_activate(self, *args): self.cmds_tv._remove_selected_commands() def on_edit_cmd_mi_activate(self, *args): self.cmds_tv._edit_selected_command() def on_new_cmd_mi_activate(self, *args): sd.do_add_command_dialog(self.sheriff, self.cmds_ts, self.window) def cleanup(self, save_settings): self._terminate_spawned_deputy() self.sheriff.shutdown() self.script_manager.shutdown() if save_settings: self.save_settings() def load_settings(self): if not os.path.exists(self.config_fname): return try: d = pickle.load(open(self.config_fname, "r")) except Exception, err: print err return self.cmds_tv.load_settings(d) self.cmd_console.load_settings(d) self.deputies_tv.load_settings(d)
class SheriffHeadless(ScriptListener): def __init__(self, lcm_obj, config, spawn_deputy, script_name, script_done_action): self.sheriff = Sheriff(lcm_obj) self.script_manager = ScriptManager(self.sheriff) self.spawn_deputy = spawn_deputy self.spawned_deputy = None self.config = config self.script_name = script_name self.script = None self.mainloop = None self.lcm_obj = lcm_obj self._should_exit = False if script_done_action is None: self.script_done_action = "exit" else: self.script_done_action = script_done_action def _shutdown(self): if self.spawned_deputy: print("Terminating local deputy..") try: self.spawned_deputy.terminate() except AttributeError: os.kill(self.spawned_deputy.pid, signal.SIGTERM) self.spawned_deputy.wait() self.spawned_deputy = None self.sheriff.shutdown() self.script_manager.shutdown() def _start_script(self): if not self.script: return False print("Running script %s" % self.script_name) errors = self.script_manager.execute_script(self.script) if errors: print("Script failed to run. Errors detected:\n" + "\n".join(errors)) self._shutdown() sys.exit(1) return False def script_finished(self, script_object): # Overriden from ScriptListener. Called by ScriptManager when a # script is finished. if self.script_done_action == "exit": self._request_exit() elif self.script_done_action == "observe": self.sheriff.set_observer(True) def _request_exit(self): self._should_exit = True def run(self): # parse the config file if self.config is not None: self.sheriff.load_config(self.config) self.script_manager.load_config(self.config) # start a local deputy? if self.spawn_deputy: procman_deputy_cmd = find_procman_deputy_cmd() args = [procman_deputy_cmd, "-i", "localhost"] if not procman_deputy_cmd: sys.stderr.write("Can't find procman-deputy.") sys.exit(1) self.spawned_deputy = subprocess.Popen(args) else: self.spawned_deputy = None # run a script if self.script_name: self.script = self.script_manager.get_script(self.script_name) if not self.script: print "No such script: %s" % self.script_name self._shutdown() sys.exit(1) errors = self.script_manager.check_script_for_errors(self.script) if errors: print "Unable to run script. Errors were detected:\n\n" print "\n ".join(errors) self._shutdown() sys.exit(1) self.script_manager.add_listener(self) signal.signal(signal.SIGINT, lambda *s: self._request_exit()) signal.signal(signal.SIGTERM, lambda *s: self._request_exit()) signal.signal(signal.SIGHUP, lambda *s: self._request_exit()) try: if self.script: time.sleep(0.2) self._start_script() while not self._should_exit: self.lcm_obj.handle_timeout(200) except KeyboardInterrupt: pass except IOError: pass finally: print("Sheriff terminating..") self._shutdown() return 0
class SheriffHeadless(ScriptListener): def __init__(self, lcm_obj, config, spawn_deputy, script_name, script_done_action): self.sheriff = Sheriff(lcm_obj) self.script_manager = ScriptManager(self.sheriff) self.spawn_deputy = spawn_deputy self.spawned_deputy = None self.config = config self.script_name = script_name self.script = None self.mainloop = None self.lcm_obj = lcm_obj self._should_exit = False if script_done_action is None: self.script_done_action = "exit" else: self.script_done_action = script_done_action def _shutdown(self): if self.spawned_deputy: print("Terminating local deputy..") try: self.spawned_deputy.terminate() except AttributeError: os.kill(self.spawned_deputy.pid, signal.SIGTERM) self.spawned_deputy.wait() self.spawned_deputy = None self.sheriff.shutdown() self.script_manager.shutdown() def _start_script(self): if not self.script: return False print("Running script %s" % self.script_name) errors = self.script_manager.execute_script(self.script) if errors: print("Script failed to run. Errors detected:\n" + "\n".join(errors)) self._shutdown() sys.exit(1) return False def script_finished(self, script_object): # Overriden from ScriptListener. Called by ScriptManager when a # script is finished. if self.script_done_action == "exit": self._request_exit() elif self.script_done_action == "observe": self.sheriff.set_observer(True) def _request_exit(self): self._should_exit = True def run(self): # parse the config file if self.config is not None: self.sheriff.load_config(self.config) self.script_manager.load_config(self.config) # start a local deputy? if self.spawn_deputy: procman_deputy_cmd = find_procman_deputy_cmd() args = [ procman_deputy_cmd, "-i", "localhost" ] if not procman_deputy_cmd: sys.stderr.write("Can't find procman-deputy.") sys.exit(1) self.spawned_deputy = subprocess.Popen(args) else: self.spawned_deputy = None # run a script if self.script_name: self.script = self.script_manager.get_script(self.script_name) if not self.script: print "No such script: %s" % self.script_name self._shutdown() sys.exit(1) errors = self.script_manager.check_script_for_errors(self.script) if errors: print "Unable to run script. Errors were detected:\n\n" print "\n ".join(errors) self._shutdown() sys.exit(1) self.script_manager.add_listener(self) signal.signal(signal.SIGINT, lambda *s: self._request_exit()) signal.signal(signal.SIGTERM, lambda *s: self._request_exit()) signal.signal(signal.SIGHUP, lambda *s: self._request_exit()) try: if self.script: time.sleep(0.2) self._start_script() while not self._should_exit: self.lcm_obj.handle_timeout(200) except KeyboardInterrupt: pass except IOError: pass finally: print("Sheriff terminating..") self._shutdown() return 0