Exemplo n.º 1
0
    def _execute(self, cmd, *args):
        modulecmd = self.modulecmd(cmd, *args)
        completed = osext.run_command(modulecmd)
        if re.search(r'\bERROR\b', completed.stderr) is not None:
            raise SpawnedProcessError(modulecmd, completed.stdout,
                                      completed.stderr, completed.returncode)

        exec(self.process(completed.stdout))
        return completed.stderr
Exemplo n.º 2
0
    def _execute(self, cmd, *args):
        modulecmd = self.modulecmd(cmd, *args)
        completed = osext.run_command(modulecmd, check=False)
        namespace = {}
        exec(self.process(completed.stdout), {}, namespace)

        # _mlstatus is set by the TMod4 only if the command was unsuccessful,
        # but Lmod sets it always
        if not namespace.get('_mlstatus', True):
            raise SpawnedProcessError(modulecmd, completed.stdout,
                                      completed.stderr, completed.returncode)

        return completed.stderr
Exemplo n.º 3
0
    def _run_module_command(self, *args, msg=None):
        command = ' '.join([self._command, *args])
        try:
            completed = os_ext.run_command(command, check=True)
        except SpawnedProcessError as e:
            raise EnvironError(msg) from e

        if self._module_command_failed(completed):
            err = SpawnedProcessError(command, completed.stdout,
                                      completed.stderr, completed.returncode)
            raise EnvironError(msg) from err

        return completed
Exemplo n.º 4
0
    def _execute(self, cmd, *args):
        modulecmd = self.modulecmd(cmd, *args)
        completed = osext.run_command(modulecmd)
        if re.search(r'\bERROR\b', completed.stderr) is not None:
            raise SpawnedProcessError(modulecmd, completed.stdout,
                                      completed.stderr, completed.returncode)

        exec_match = re.search(r"^exec\s'(\S+)'", completed.stdout,
                               re.MULTILINE)
        if exec_match is None:
            raise ConfigError('could not use the python bindings')

        with open(exec_match.group(1), 'r') as content_file:
            cmd = content_file.read()

        exec(self.process(cmd))
        return completed.stderr
Exemplo n.º 5
0
def run_command(cmd, check=False, timeout=None, shell=False, log=True):
    '''Run command synchronously.

    This function will block until the command executes or the timeout is
    reached. It essentially calls :func:`run_command_async` and waits for the
    command's completion.

    :arg cmd: The command to execute as a string or a sequence. See
        :func:`run_command_async` for more details.
    :arg check: Raise an error if the command exits with a non-zero exit code.
    :arg timeout: Timeout in seconds.
    :arg shell: Spawn a new shell to execute the command.
    :arg log: Log the execution of the command through ReFrame's logging
        facility.
    :returns: A :py:class:`subprocess.CompletedProcess` object with
        information about the command's outcome.
    :raises reframe.core.exceptions.SpawnedProcessError: If ``check``
        is :class:`True` and the command fails.
    :raises reframe.core.exceptions.SpawnedProcessTimeout: If the command
        times out.

    '''

    try:
        proc = run_command_async(cmd,
                                 shell=shell,
                                 start_new_session=True,
                                 log=log)
        proc_stdout, proc_stderr = proc.communicate(timeout=timeout)
    except subprocess.TimeoutExpired as e:
        os.killpg(proc.pid, signal.SIGKILL)
        raise SpawnedProcessTimeout(e.cmd, proc.stdout.read(),
                                    proc.stderr.read(), timeout) from None

    completed = subprocess.CompletedProcess(cmd,
                                            returncode=proc.returncode,
                                            stdout=proc_stdout,
                                            stderr=proc_stderr)

    if check and proc.returncode != 0:
        raise SpawnedProcessError(completed.args, completed.stdout,
                                  completed.stderr, completed.returncode)

    return completed
Exemplo n.º 6
0
def run_command(cmd, check=False, timeout=None, shell=False):
    try:
        proc = run_command_async(cmd, shell=shell, start_new_session=True)
        proc_stdout, proc_stderr = proc.communicate(timeout=timeout)
    except subprocess.TimeoutExpired as e:
        os.killpg(proc.pid, signal.SIGKILL)
        raise SpawnedProcessTimeout(e.cmd, proc.stdout.read(),
                                    proc.stderr.read(), timeout) from None

    completed = subprocess.CompletedProcess(args=shlex.split(cmd),
                                            returncode=proc.returncode,
                                            stdout=proc_stdout,
                                            stderr=proc_stderr)

    if check and proc.returncode != 0:
        raise SpawnedProcessError(completed.args, completed.stdout,
                                  completed.stderr, completed.returncode)

    return completed