def test_updating_launcher_importance_works(context, single_launcher_file, config_dir): launcher = Mklauncher(context, launcher_dirs=single_launcher_file, config_dir=config_dir) from machinetalk.protobuf.config_pb2 import Launcher msg = Launcher() msg.index = 0 msg.importance = 5 launcher._update_importance(msg) launcher._update_launcher_status() launchers = launcher.container.launcher assert len(launchers) == 1 assert launchers[0].importance == 5
def _update_launcher_status(self): tx_launcher = Launcher() # new pb message for tx has_update = False for launcher in self.container.launcher: modified = False index = launcher.index importance = self._importances[self._launcher_ids[launcher.index]] if importance is not launcher.importance: tx_launcher.importance = importance modified = True terminating = False if index in self.terminating: terminating = True self.terminating.remove(index) if index in self.processes: process = self.processes[index] process.poll() returncode = process.returncode if returncode is None: if not launcher.running: # update running value if len(launcher.output) > 0: launcher.ClearField( 'output') # clear output for new processes self.launcher_full_update = True # request a full update tx_launcher.running = True tx_launcher.returncode = 0 modified = True # read stdout stdout_index = len(launcher.output) while True: try: line = process.stdout.read() stdoutLine = StdoutLine() stdoutLine.index = stdout_index stdoutLine.line = line tx_launcher.output.add().MergeFrom(stdoutLine) stdout_index += 1 modified = True except IOError: # process has no new line break # send termination status if terminating: tx_launcher.terminating = True else: tx_launcher.returncode = returncode tx_launcher.running = False tx_launcher.terminating = False modified = True self.processes.pop(index, None) # remove from watchlist if modified: launcher.MergeFrom(tx_launcher) tx_launcher.index = index self.tx_container.launcher.add().MergeFrom(tx_launcher) tx_launcher.Clear() has_update = True if self.launcher_full_update: self._add_pparams_to_message() self.tx_container.CopyFrom(self.container) self._send_launcher_message(pb.MT_LAUNCHER_FULL_UPDATE) self.launcher_full_update = False elif has_update: self._send_launcher_message(pb.MT_LAUNCHER_INCREMENTAL_UPDATE)
def _search_launchers(self, directories): INI_NAME = 'launcher.ini' CONFIG_DEFAULTS = { 'name': 'Launcher', 'command': '', 'description': '', 'image': '', 'shell': 'false', 'workdir': '.', 'type': '', 'manufacturer': '', 'model': '', 'variant': '', 'priority': '0' } launchers = [] ids = {} index = 0 for root_dir in directories: for root, _, files in os.walk(root_dir): if INI_NAME not in files: continue root = os.path.abspath(os.path.expanduser(root)) iniFile = os.path.join(root, INI_NAME) cfg = configparser.ConfigParser(CONFIG_DEFAULTS) cfg.read(iniFile) for section in cfg.sections(): launcher = Launcher() # descriptive data launcher.name = cfg.get(section, 'name') launcher.description = cfg.get(section, 'description') info = MachineInfo() info.type = cfg.get(section, 'type') info.manufacturer = cfg.get(section, 'manufacturer') info.model = cfg.get(section, 'model') info.variant = cfg.get(section, 'variant') launcher.priority = cfg.getint(section, 'priority') launcher.importance = 0 launcher.info.MergeFrom(info) # command data launcher.command = cfg.get(section, 'command') launcher.shell = cfg.getboolean(section, 'shell') workdir = cfg.get(section, 'workdir') if not os.path.isabs(workdir): workdir = os.path.join(root, workdir) launcher.workdir = os.path.normpath(workdir) launcher.returncode = 0 launcher.running = False launcher.terminating = False # storing the image file image_file = cfg.get(section, 'image') if image_file is not '': if not os.path.isabs(image_file): image_file = os.path.join(root, image_file) fileBuffer = open(image_file, 'rb').read() image = File() image.name = os.path.basename(image_file) image.encoding = CLEARTEXT image.blob = fileBuffer launcher.image.MergeFrom(image) launcher.index = index index += 1 launchers.append(launcher) ids[launcher.index] = '%s:%s' % (root, section) # sort using the priority attribute before distribution return sorted(launchers, key=attrgetter('priority'), reverse=True), ids
def _update_launcher_status(self): tx_launcher = Launcher() # new pb message for tx has_update = False for launcher in self.container.launcher: modified = False index = launcher.index importance = self._importances[self._launcher_ids[launcher.index]] if importance is not launcher.importance: tx_launcher.importance = importance modified = True terminating = False if index in self.terminating: terminating = True self.terminating.remove(index) if index in self.processes: process = self.processes[index] process.poll() returncode = process.returncode if returncode is None: if not launcher.running: # update running value if len(launcher.output) > 0: launcher.ClearField('output') # clear output for new processes self.launcher_full_update = True # request a full update tx_launcher.running = True tx_launcher.returncode = 0 modified = True # read stdout stdout_index = len(launcher.output) while True: try: line = process.stdout.read() stdoutLine = StdoutLine() stdoutLine.index = stdout_index stdoutLine.line = line tx_launcher.output.add().MergeFrom(stdoutLine) stdout_index += 1 modified = True except IOError: # process has no new line break # send termination status if terminating: tx_launcher.terminating = True else: tx_launcher.returncode = returncode tx_launcher.running = False tx_launcher.terminating = False modified = True self.processes.pop(index, None) # remove from watchlist if modified: launcher.MergeFrom(tx_launcher) tx_launcher.index = index self.tx_container.launcher.add().MergeFrom(tx_launcher) tx_launcher.Clear() has_update = True if self.launcher_full_update: self._add_pparams_to_message() self.tx_container.CopyFrom(self.container) self._send_launcher_message(pb.MT_LAUNCHER_FULL_UPDATE) self.launcher_full_update = False elif has_update: self._send_launcher_message(pb.MT_LAUNCHER_INCREMENTAL_UPDATE)
def _search_launchers(directories): ini_name = "launcher.ini" config_defaults = { "name": "Launcher", "command": "", "description": "", "image": "", "shell": "false", "workdir": ".", "type": "", "manufacturer": "", "model": "", "variant": "", "priority": "0", } launchers = [] ids = {} index = 0 for root_dir in directories: for root, _, files in os.walk(root_dir): if ini_name not in files: continue root = os.path.abspath(os.path.expanduser(root)) ini_file = os.path.join(root, ini_name) cfg = configparser.ConfigParser(config_defaults) cfg.read(ini_file) for section in cfg.sections(): launcher = Launcher() # descriptive data launcher.name = cfg.get(section, "name") launcher.description = cfg.get(section, "description") info = MachineInfo() info.type = cfg.get(section, "type") info.manufacturer = cfg.get(section, "manufacturer") info.model = cfg.get(section, "model") info.variant = cfg.get(section, "variant") launcher.priority = cfg.getint(section, "priority") launcher.importance = 0 launcher.info.MergeFrom(info) # command data launcher.command = cfg.get(section, "command") launcher.shell = cfg.getboolean(section, "shell") workdir = cfg.get(section, "workdir") if not os.path.isabs(workdir): workdir = os.path.join(root, workdir) launcher.workdir = os.path.normpath(workdir) launcher.returncode = 0 launcher.running = False launcher.terminating = False # storing the image file image_file = cfg.get(section, "image") if image_file != "": if not os.path.isabs(image_file): image_file = os.path.join(root, image_file) file_buffer = open(image_file, "rb").read() image = File() image.name = os.path.basename(image_file) image.encoding = CLEARTEXT image.blob = file_buffer launcher.image.MergeFrom(image) launcher.index = index index += 1 launchers.append(launcher) ids[launcher.index] = "%s:%s" % (root, section) # sort using the priority attribute before distribution return sorted(launchers, key=attrgetter("priority"), reverse=True), ids