def dict2command(cmd, options={}): """ Provides simple method to build up "/usr/bin/foo --key1=value1 --key2=value2" styled command line. @param cmd: command path to be executed. @type cmd: string @param options: dictionary which contains {key: value} to be transformed into "--key=value". Defaults to {}. If you like to pass a flag styled option(like --flag), just put None into value. @type options: dictionary @return: command line in single string. """ if is_empty(cmd): raise CommandException("command not found. - cmd=%s" % cmd) ret = "" for x in options.keys(): if options[x] is None: ret += "--%s " % x else: try: options[x].index(' ') ret += "--%s=\"%s\" " % (x, options[x]) except: ret += "--%s=%s " % (x, options[x]) return "%s %s" % (cmd.strip(), ret.strip())
def _finish(self): proc = None proc_info = [] cmd = self._m_jg.finish_command if is_empty(cmd): self.logger.debug('finish command not running!!- jobgroup_id=%d' % (self._m_jg.id)) return False # No finish Command else: try: self.logger.info( 'finish command running!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) lcmd = split_shell_command(cmd) if self.chk_whitelist(lcmd[0]): try: (proc, proc_info) = popen( lcmd, self._cf['job.popen.timeout'], self._cf['job.popen.waittime'], self._cf['job.popen.env.lang'], ) self.logger.debug('Of commands executed stdout=%s' % proc_info['stdout']) self.logger.debug('Of commands executed stderr=%s' % proc_info['stderr']) except OSError, oe: self.logger.info( 'finish command system failed!! jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) raise oe if proc_info['r_code'] == 0: self.logger.info( 'finish command successful!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) else: self.logger.info( 'finish command failed!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) return True else:
def chk_whitelist(self, cmd): flag = self._cf['job.whitelist.flag'].strip() if is_empty(flag) is True: self.logger.debug("Whitelist feature [OFF] - empty") return True # Unconditional if flag != "1": self.logger.debug("Whitelist feature [OFF]") return True # Unconditional self.logger.debug("Whitelist feature [ON]") fp = open(self._cf['job.whitelist.path'], 'r') try: for line in fp.readlines(): if cmd.strip() == line.strip(): return True finally: fp.close() return False
def _finish(self): proc = None proc_info = [] cmd = self._m_jg.finish_command if is_empty(cmd): self.logger.debug('finish command not running!!- jobgroup_id=%d' % (self._m_jg.id)) return False # No finish Command else: try: self.logger.info('finish command running!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) lcmd = split_shell_command(cmd) if self.chk_whitelist(lcmd[0]): try: (proc, proc_info) = popen(lcmd, self._cf['job.popen.timeout'], self._cf['job.popen.waittime'], self._cf['job.popen.env.lang'], ) self.logger.debug('Of commands executed stdout=%s' % proc_info['stdout']) self.logger.debug('Of commands executed stderr=%s' % proc_info['stderr']) except OSError, oe: self.logger.info('finish command system failed!! jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) raise oe if proc_info['r_code'] == 0: self.logger.info('finish command successful!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) else: self.logger.info('finish command failed!! - jobgroup_id=%d : cmd=%s' % (self._m_jg.id, cmd)) return True else:
def is_rollback(self): return not is_empty(self.rollback_command)
def test_is_empty_0(self): self.assertFalse(target.is_empty('cmd')) self.assertFalse(target.is_empty(' cmd ')) self.assertFalse(target.is_empty(' cmd')) self.assertFalse(target.is_empty('cmd -a ')) self.assertFalse(target.is_empty('cmd -a ')) self.assertFalse(target.is_empty('cmd -a /hoge')) self.assertFalse(target.is_empty('cmd -a -as')) self.assertFalse(target.is_empty('cmd a -as')) self.assertTrue(target.is_empty('')) self.assertTrue(target.is_empty(' ')) self.assertTrue(target.is_empty(None))