示例#1
0
    def add(self, items, tmpl_vals=None, allow_spaces=True):
        """
        Add options/arguments to command

        :param item: option/argument to add to command
        :param tmpl_vals: template values for item
        """
        if not isinstance(items, (list, tuple)):
            items = [items]

        for item in items:
            if tmpl_vals:
                item = item % tmpl_vals

            if not is_string(item):
                raise ValueError(
                    "Non-string item %s (type %s) being added to command %s" %
                    (item, type(item), self))

            if not allow_spaces and ' ' in item:
                raise ValueError(
                    "Found one or more spaces in item '%s' being added to command %s"
                    % (item, self))

            super(CmdList, self).append(item)
示例#2
0
文件: run.py 项目: hpcugent/vsc-base
    def _make_shell_command(self):
        """Convert cmd into a list of command to be sent to popen, without a shell"""
        if self.cmd is None:
            self.log.raiseException("_make_shell_command: no cmd set.")

        if is_string(self.cmd):
            self._shellcmd = shlex.split(self.cmd)
        elif isinstance(self.cmd, (list, tuple,)):
            self._shellcmd = self.cmd
        else:
            self.log.raiseException("Failed to convert cmd %s (type %s) into non shell command" %
                                    (self.cmd, type(self.cmd)))
示例#3
0
文件: run.py 项目: hpcugent/vsc-base
    def _make_shell_command(self):
        """Convert cmd into shell command"""
        self.log.warning("using potentialy unsafe shell commands, use run.run or run.RunNoShell.run \
                         instead of run.run_simple or run.Run.run")
        if self.cmd is None:
            self.log.raiseException("_make_shell_command: no cmd set.")

        if is_string(self.cmd):
            self._shellcmd = self.cmd
        elif isinstance(self.cmd, (list, tuple,)):
            self._shellcmd = " ".join([str(arg).replace(' ', '\ ') for arg in self.cmd])
        else:
            self.log.raiseException("Failed to convert cmd %s (type %s) into shell command" %
                                    (self.cmd, type(self.cmd)))
示例#4
0
文件: run.py 项目: hpcugent/vsc-base
 def process_answers(answers):
     """Construct list of newline-terminated answers (as strings)."""
     if is_string(answers):
         answers = [answers]
     elif isinstance(answers, list):
         # list is manipulated when answering matching question, so take a copy
         answers = answers[:]
     else:
         msg_tmpl = "Invalid type for answer, not a string or list: %s (%s)"
         self.log.raiseException(msg_tmpl % (type(answers), answers), exception=TypeError)
     # add optional split at the end
     if self.add_newline:
         for i in [idx for idx, a in enumerate(answers) if not a.endswith('\n')]:
             answers[i] += '\n'
     return answers
示例#5
0
    def _make_shell_command(self):
        """Convert cmd into a list of command to be sent to popen, without a shell"""
        if self.cmd is None:
            self.log.raiseException("_make_shell_command: no cmd set.")

        if is_string(self.cmd):
            self._shellcmd = shlex.split(self.cmd)
        elif isinstance(self.cmd, (
                list,
                tuple,
        )):
            self._shellcmd = self.cmd
        else:
            self.log.raiseException(
                "Failed to convert cmd %s (type %s) into non shell command" %
                (self.cmd, type(self.cmd)))
示例#6
0
 def process_answers(answers):
     """Construct list of newline-terminated answers (as strings)."""
     if is_string(answers):
         answers = [answers]
     elif isinstance(answers, list):
         # list is manipulated when answering matching question, so take a copy
         answers = answers[:]
     else:
         msg_tmpl = "Invalid type for answer, not a string or list: %s (%s)"
         self.log.raiseException(msg_tmpl % (type(answers), answers),
                                 exception=TypeError)
     # add optional split at the end
     if self.add_newline:
         for i in [
                 idx for idx, a in enumerate(answers)
                 if not a.endswith('\n')
         ]:
             answers[i] += '\n'
     return answers
示例#7
0
    def _make_shell_command(self):
        """Convert cmd into shell command"""
        self.log.warning(
            "using potentialy unsafe shell commands, use run.run or run.RunNoShell.run \
                         instead of run.run_simple or run.Run.run")
        if self.cmd is None:
            self.log.raiseException("_make_shell_command: no cmd set.")

        if is_string(self.cmd):
            self._shellcmd = self.cmd
        elif isinstance(self.cmd, (
                list,
                tuple,
        )):
            self._shellcmd = " ".join(
                [str(arg).replace(' ', '\ ') for arg in self.cmd])
        else:
            self.log.raiseException(
                "Failed to convert cmd %s (type %s) into shell command" %
                (self.cmd, type(self.cmd)))
示例#8
0
文件: run.py 项目: hpcugent/vsc-base
    def add(self, items, tmpl_vals=None, allow_spaces=True):
        """
        Add options/arguments to command

        :param item: option/argument to add to command
        :param tmpl_vals: template values for item
        """
        if not isinstance(items, (list, tuple)):
            items = [items]

        for item in items:
            if tmpl_vals:
                item = item % tmpl_vals

            if not is_string(item):
                raise ValueError("Non-string item %s (type %s) being added to command %s" % (item, type(item), self))

            if not allow_spaces and ' ' in item:
                raise ValueError("Found one or more spaces in item '%s' being added to command %s" % (item, self))

            super(CmdList, self).append(item)